创建新 object 时 JFrame 不显示组件

JFrame not showing components when new object is created

我的程序有问题,第一个 运行 通过代码 运行 是完美的,但是当试图创建一个新的 object 然后回顾原来的方法时window 显示,window 以正确的尺寸和正确的标题显示,但未显示任何组件。

我检查过没有静态变量,所有需要的变量都在构造函数中初始化。

Class 即显示 window:

public class DifficultySelect implements ActionListener        
{
    private JPanel getInput;
    private JFrame startFrame;
    private JButton easy;
    private JButton medium;
    private JButton hard;
    private JButton custom;
    private JLabel labelRow;
    private JLabel labelColumn;
    private JLabel labelMines;
    private JTextArea textRow;
    private JTextArea textColumn;
    private JTextArea textMines;  
    public ArrayList<Integer> getArray;

    public DifficultySelect()
    {
       getInput = new JPanel(); 
       startFrame = new JFrame("Select Difficulty:");
       easy = new JButton();
       medium = new JButton();
       hard = new JButton();
       custom = new JButton();
       labelRow = new JLabel();
       labelColumn = new JLabel();
       labelMines = new JLabel();
       textRow = new JTextArea(5, 20);
       textColumn = new JTextArea(5, 20);
       textMines = new JTextArea(5, 20);
       getArray = new ArrayList<Integer>();
    }

    public void setDisplay()
    {
        getInput.setLayout(new BoxLayout(getInput, BoxLayout.PAGE_AXIS));       
        getInput.setVisible(true);

        Dimension buttonSize = new Dimension(300,40);
        easy.setText("Easy: 5x5 - 4 Mines");
        easy.setMaximumSize(buttonSize);
        easy.addActionListener(this);
        medium.setText("Medium: 10x10 - 20 Mines");
        medium.setMaximumSize(buttonSize);
        medium.addActionListener(this);
        hard.setText("Hard: 15 x 15 - 50 Mines");
        hard.setMaximumSize(buttonSize);
        hard.addActionListener(this);
        custom.setText("Custom: Enter Rows/Columns/Mines then Click");
        custom.setMaximumSize(buttonSize);
        custom.addActionListener(this);

        labelRow.setText("Enter Row Size: ");       
        labelColumn.setText("Enter Column Size: ");       
        labelMines.setText("Enter Amount of Mines:");

        textRow.setAlignmentX(0);
        textColumn.setAlignmentX(0);
        textMines.setAlignmentX(0);

        getInput.add(easy);
        getInput.add(medium);
        getInput.add(hard);
        getInput.add(custom);
        getInput.add(labelRow);
        getInput.add(textRow);
        getInput.add(labelColumn);
        getInput.add(textColumn);
        getInput.add(labelMines);
        getInput.add(textMines);

        startFrame.add(getInput);
        startFrame.setSize(310, 250);
        startFrame.setResizable(false);
        startFrame.setVisible(true);
        startFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

下一个代码块显示了我如何调用 GUI class 从 main 调用时它可以工作,但稍后在程序中调用时不工作

private DifficultySelect d;

    public StartGame()
    {

        d = new DifficultySelect();
        d.setDisplay();
    }

我一直在尝试许多不同的方法来让它工作,我目前在构造函数中调用,所以现在我用 StartGame newGame = new StartGame(); 调用 object 这只会导致空白白色屏幕,虽然它的大小正确,并且具有正确的 JFrame 名称,这意味着某些方面已正确创建。

请尝试启动您的 GUI:

public class StartGame {

    public static void main(String[] args) {
    DifficultySelect    d = new DifficultySelect();
        d.setDisplay();

    }
}

看起来:

当你想在另一个地方再次显示这个表格时,你可以使用代码:

        if (d == null) {
            d = new DifficultySelect();
            d.setVisible(true);
        } else {
            d.setVisible(true);
        }

当您想创建一个新实例时,将此代码放在您的位置:

DifficultySelect new_d = new DifficultySelect();
    new_d.setDisplay();