简体   繁体   English

GUI的堆栈溢出错误

[英]Stack overflow error with GUI

Alright so I am making a calculater, and I am getting a stack overflow error, i'm guessing because it's trying to handle to much data. 好吧,所以我正在做一个计算器,并且我遇到了堆栈溢出错误,我猜是因为它试图处理大量数据。

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

when I comment out paint(g) I no longer get the error: 当我注释掉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)

I want it to update "Aproox size" in real time 我希望它实时更新“ Aproox大小”

There is a cyclic dependency between paint and DrawStats — each one calls the other. paintDrawStats之间存在循环依赖关系-每个相互调用。 Don't call paint directly. 不要直接叫油漆。 Rather invoke repaint . 而是调用repaint Also override paintComponent rather than paint and invoke super.paintComponent(g) . 还要重写paintComponent而不是paint并调用super.paintComponent(g)

Remove the methods paint and DrawStats and replace with this 删除方法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);
}

Use a Swing Timer to invoke repaint if periodic repaints are required. 如果需要定期重绘,请使用Swing计时器来调用repaint

Aside: Use Java naming conventions when naming method names such as drawStats . 另外:在命名方法名称(例如drawStats时,请使用Java命名约定。

Ouch... Infinite recursion 哎呀...无限递归

@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
}

I suppose you see this in your stack trace: 我想您会在堆栈跟踪中看到以下内容:

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

Remove paint(g); 去除paint(g); in DrawStats DrawStats

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM