2013-05-17 66 views
2

好吧,所以我正在做一個計算器,我得到一個堆棧溢出錯誤,我猜是因爲它試圖處理大量的數據。堆棧溢出錯誤與GUI

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"); 
     } 
    } 
    } 
} 

當我註釋掉漆(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大小」

回答

5

有一個循環依賴在paintDrawStats之間—每個人都叫另一個。不要直接調用油漆。而不是調用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

+0

刪除paint(g);怎麼做呢? – user2393948

+1

@ user2393948並且不要使用'null' LayoutManager。始終使用適當的。 –

+0

@ user2393948當您可以管理它時,您當然可以使用絕對定位('null' LayoutManager)。 – johnchen902

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)... 

DrawStats

+0

是的,我想,如何每次點擊calc按鈕時調用它?我試着去做程序並做Draw​​Stats(g);但它表示「g」不能解析爲變量。 – user2393948

+0

@ user2393948只需調用'repaint()'。重繪管理器最終將用適當的Graphics對象調用'paintComponent' –