JFrame在创建新对象时不显示组件

时间:2017-05-17 16:57:30

标签: java swing user-interface frame

在我的程序中遇到问题,代码的第一次运行完美运行,但在尝试创建新对象然后调用使窗口显示的原始方法时,窗口显示的大小正确,正确的标题,但没有显示任何组件。

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

显示窗口的类:

  try {
        JsonResponseHolder jrh = new Gson().fromJson(responseString, 
                                            JsonResponseHolder.class);
        List<Results> results = jrh.getData().getResults(); 
         /// this will give the result objects                                                  
         /// and be sure to convert [] arrays to list for better data handling
    } catch (JSONException e) {
        e.printStackTrace();
    }

下一段代码显示了我如何调用从main调用的GUI类,但是稍后在程序中调用时它不起作用

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

我一直在尝试许多不同的方法来使这个工作,我目前在构造函数中有调用,所以现在我用private DifficultySelect d; public StartGame() { d = new DifficultySelect(); d.setDisplay(); } 调用对象,这只会导致一个空白的白色屏幕,尽管它是正确的大小,并具有正确的JFrame名称,这意味着正确地创建了一些方面。

1 个答案:

答案 0 :(得分:1)

请尝试启动GUI:

public class StartGame {

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

    }
}
它看起来像: enter image description here

如果您想再次在其他地方展示此表单,可以使用代码:

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

如果要创建新实例,请将此代码放在您的位置:

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