2014-10-19 16 views
1

所以我試圖創建一個gui,我之前用java修改過gui,但我對它們還是陌生的。所以我在這裏發佈的是,我的JLabels(但標籤012bcbLabel)充滿了按鈕和複選框。可悲的是我的JFrame只會顯示設置爲BorderLayout.CENTER的設置。 NORTH不會顯示,即使我只將butLabel設置爲SOUTH,甚至不使用cbLabel。我忽略了什麼?非常感謝,謝謝!JButtons只出現在JFrame上,如果在BorderLayout.CENTER中,不是SOUTH或NORTH

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

不使用標籤分組元素,而是使用JPanel而不是 – maskacovnik 2014-10-19 19:18:19

+0

好天啊我真不敢相信我做了這些,我以前看過它,看起來不正確。善良,我感到很傻。謝謝你的快速反應! – 2014-10-19 19:19:59

+0

很高興 – maskacovnik 2014-10-19 19:20:33

回答

3

不使用標籤進行分組的元素,使用的JPanel,而不是 我曾嘗試更換所有

標籤

面板

它的工作

+1

1+,但也加入了你的答案的原因:佈局管理器,JLabel默認爲null,JPanel爲FlowLayout)。 – 2014-10-19 19:20:22

+0

是的,謝謝我會在9分鐘內投票。在發佈(-_-)之前應該更多地考慮它。再次感謝! – 2014-10-19 19:23:04

+0

感謝您的澄清@HovercraftFullOfEels – maskacovnik 2014-10-19 19:25:20