简体   繁体   中英

JButtons only appear on JFrame if in BorderLayout.CENTER, not SOUTH or NORTH

So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!

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

do not use Label for grouping elements, use JPanel instead I have tried replace all

Label

with

Panel

it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM