JFrame.setVisible(true)继续迭代它的父方法,直到发生stackOverflowError

adn*_*252 0 java swing

我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)

Rus*_*ser 6

您的show()方法覆盖了现有方法JFrame.使用不同的名称,或者更好的是,JFrame除非您确实需要更改帧的行为方式,否则不要扩展名称.