2

我将 JLabel 和 JCombobox 附加到 JPanel。这工作正常。但是当我再添加两个按钮时,我看不到这些按钮。

下面是我的代码:

JPanel jPanel=new JPanel();
jPanel.setLayout(null);
JLabel label = new JLabel("Welcome");                       
label.setFont(new Font("Helvetica", Font.ROMAN_BASELINE, 13));          
jPanel.add(label);     
JComboBox combo = new JComboBox(comboboxbean);
combo.setPreferredSize(new Dimension(285, 20));
combo.setFont(new Font("Helvetica", Font.ROMAN_BASELINE, 13));          
jPanel.add(combo);           
startButton = new JButton("Start");
stopButton = new JButton("Stop");
startButton.addActionListener(this);
startButton.setActionCommand("enable");
jPanel.add(startButton);
stopButton.addActionListener(this);
stopButton.setActionCommand("enable");
jPanel.add(stopButton); 
Insets insets = jPanel.getInsets();              

Dimension size = label.getPreferredSize();
        label.setBounds(20 + insets.left, 30 + insets.top,
                     size.width, size.height);

Dimension size1 = combo.getPreferredSize();
     combo.setBounds(20 + insets.left, 65 + insets.top,
                     size1.width, size1.height);

Dimension size2 = startButton.getPreferredSize();
    startButton.setBounds(20 + insets.left, 100 + insets.top,
                size2.width, size2.height);

Dimension size3 = stopButton.getPreferredSize();
     stopButton.setBounds(20 + insets.left, 130 + insets.top,
             size3.width, size3.height);        

frame.add(jPanel);  
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);  

最后,我将 JPanel 添加到 JFrame。我已将 JPanel 的布局设置为空。我找不到按钮不显示的原因。任何帮助表示赞赏。

4

1 回答 1

2

如果布局为空,则意味着您必须使用该setBounds()方法来定位添加到您的JPanel. 您目前没有这样做,所以我认为按钮是在 . 之外JPanel还是在JComboBox.
无论如何,如果你想让你的按钮在你必须告诉他们的特定位置,这不会像使用Layoutnull 以外的那样是自动的。

于 2012-08-16T11:25:50.373 回答