2

好吧,所以我正在制作一个计算器,我得到一个堆栈溢出错误,我猜是因为它试图处理大量数据。

import java.awt.*;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Size extends JPanel implements ActionListener {

double base,size;
int shoesize;
String race;

JButton calc = new JButton("Calculate");

JTextField textsize = new JTextField(20);

public Size() {
    //JButton calc;
    System.out.println("Started the adding");

    calc.addActionListener(this);
    textsize.addActionListener(this);

    calc.setBounds(135, 200, 120, 40);
    textsize.setBounds(15,40,70,20);

    add(calc);
    add(textsize);

    setPreferredSize(new Dimension(400, 300));
    setLayout(null);
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Size calc");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new Size());
    frame.pack();
    frame.setVisible(true);
}

@Override
public void paint(Graphics g){
    DrawStats(g);
}

public void DrawStats(Graphics g) {
    g.setFont(new Font(null, Font.PLAIN, 12));
    g.setColor(Color.red);
    g.drawString("Aprrox Size: " + size, 135, 15);
    paint(g);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == calc) {
        try {
            String ShoeSize = textsize.getText();

            shoesize = Integer.parseInt(ShoeSize);  
            size = shoesize/2;
        } catch (Exception j) {
            System.out.println("Nothing inside of the text field");
        } 
    }
    }
}

当我注释掉 paint(g) 时,我不再收到错误消息:

 Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.lang.Character.toLowerCase(Unknown Source)
at java.lang.Character.toLowerCase(Unknown Source)
at java.lang.String.toLowerCase(Unknown Source)
at sun.font.SunFontManager.findFont2D(Unknown Source)
at java.awt.Font.getFont2D(Unknown Source)
at java.awt.Font.access$000(Unknown Source)
at java.awt.Font$FontAccessImpl.getFont2D(Unknown Source)
at sun.font.FontUtilities.getFont2D(Unknown Source)
at sun.java2d.SunGraphics2D.checkFontInfo(Unknown Source)
at sun.java2d.SunGraphics2D.getFontInfo(Unknown Source)
at sun.java2d.pipe.GlyphListPipe.drawString(Unknown Source)
at sun.java2d.SunGraphics2D.drawString(Unknown Source)

我希望它实时更新“Aproox 尺寸”

4

2 回答 2

5

paint和之间存在循环依赖关系DrawStats——每个都调用另一个。不要直接调用paint。而是调用repaint. 也覆盖paintComponent而不是paint调用super.paintComponent(g)

删除方法paintDrawStats替换为

@Override
public void paintComponent(Graphics g) {

   super.paintComponent(g));
   g.setFont(new Font("SansSerif", Font.PLAIN, 12));
   g.setColor(Color.red);
   g.drawString("Aprrox Size: " + size, 135, 15);
}

如果需要定期重绘,请使用Swing Timer调用。repaint

旁白:在命名方法名称时使用 Java 命名约定,例如drawStats.

于 2013-05-17T13:04:34.010 回答
1

哎哟……无限递归

@Override
public void paint(Graphics g){
    DrawStats(g); // infinite recursion
}
public void DrawStats(Graphics g) {
    g.setFont(new Font(null, Font.PLAIN, 12));
    g.setColor(Color.red);
    g.drawString("Aprrox Size: " + size, 135, 15);
    paint(g); // infinite recursion
}

我想您在堆栈跟踪中看到了这一点:

...
at Size.paint
at Size.DrawStats
at Size.paint
at Size.DrawStats
at Size.paint
at Size.DrawStats
(a lot more)...

删除paint(g);_DrawStats

于 2013-05-17T13:06:47.963 回答