我有一个 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;
}
}