gpt4 book ai didi

java - JFrame.setVisible(true) 不断迭代其父方法,直到发生 stackOverflowError

转载 作者:行者123 更新时间:2023-12-01 06:53:03 25 4
gpt4 key购买 nike

我有一个 JFrame 子类,它在使用 setVisible(true) 方法显示 JFrame 及其包含的小部件之前等待特定的控制台输入。小部件(JPanel 的子类)使用迭代器从 LinkedList 添加到父 JFrame,并通过类中的另一个方法添加到 LinkedList。

当我运行程序时,它不断重复包含 this.setVisible(true) 的方法,并且不显示任何内容。任何帮助将不胜感激。我已粘贴下面的代码。

public class GUI extends JFrame{

class KPanel extends JPanel{ //virtual class for Panels that displayed variable name in titled border
public KPanel(String varName){
TitledBorder varTitle = new TitledBorder(varName +":");
this.setBorder(varTitle);
}
}

private LinkedList<KPanel> buffer; //list containing components to be added to GUI

public GUI(String title){
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(300,300);
buffer = new LinkedList<KPanel>(); //initializes linkedlist buffer
}

public void addBox(String var, String val){

//creates a panel containing a string, adds it to the buffer

KPanel temp = new KPanel(var);
JLabel valLabel = new JLabel(val);
temp.add(valLabel);
buffer.add(temp);
}



public void show(){

int i=0;
int wid_height;
int x = 0;
if ((x = buffer.size()) != 0)
wid_height = this.getHeight()/x; //calculates widget heights for even distribution along frame
else{
System.out.println("No variables set!");
return;
}
System.out.println("buffer: " + buffer.size() + "\nheights: " + wid_height);


Iterator<KPanel> iter = buffer.iterator();
KPanel temp = new KPanel("");

while(iter.hasNext()){
temp = iter.next();
temp.setSize(this.getWidth(), wid_height);
temp.setLocation(0, wid_height*i);
this.add(temp);
i++;
}

this.setVisible(true);
return;
}
}

最佳答案

您的 show() 方法正在覆盖 JFrame 中的现有方法。使用不同的名称,或者更好的是,不要扩展 JFrame ,除非您确实需要更改框架的行为方式。

关于java - JFrame.setVisible(true) 不断迭代其父方法,直到发生 stackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com