如果在BorderLayout.CENTER中,JButton只出现在JFrame上,而不是出现在SOUTH或NORTH中

时间:2014-10-19 19:15:21

标签: java button user-interface

所以我正在尝试创建一个gui,我之前在java中修改了gui,但我还是他们的新手。所以我在这里发布的是我的JLabel(butLabel& cbLabel)充满了按钮和复选框。可悲的是,我的JFrame只显示设置为BorderLayout.CENTER的那个。北方和北方即使我只将butLabel设置为SOUTH而且甚至不使用cbLabel,SOUTH也不会显示。我在俯瞰什么?非常感谢,谢谢!

public class mainWindow
{
    JFrame frame = new JFrame("Main Window");

    JLabel butLabel = new JLabel();
    JLabel cbLabel = new JLabel();

    JButton showBut = new JButton("Show");
    JButton exitBut = new JButton("Exit");
    JButton addBut = new JButton("Add");
    JButton remBut = new JButton("Remove");

    JCheckBox aCB = new JCheckBox("Airplane");
    JCheckBox bCB = new JCheckBox("Boat");
    JCheckBox cCB = new JCheckBox("Clock");


    public mainWindow()
    {
        frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setMinimumSize(new Dimension(360, 480));


        butLabel.setLayout(new GridLayout(1,4));
        cbLabel.setLayout(new GridLayout(2, 2));

        butLabel.add(showBut);
        butLabel.add(exitBut);
        butLabel.add(addBut);
        butLabel.add(remBut);

        cbLabel.add(aCB);
        cbLabel.add(bCB);
        cbLabel.add(cCB);

        frame.add(butLabel, BorderLayout.CENTER);
        frame.add(cbLabel, BorderLayout.NORTH);
    }
    public void setVisible()
    {
        butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
        cbLabel.setVisible(true);//again I was trying things that I thought might make sense.

        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:3)

不要使用Label来分组元素,而是使用JPanel 我试过替换所有

  

标签

  

面板

它有效