下面是我的Java程序的簡化版本。 它工作正常,直到我添加行 JComboBox comboBox = new JComboBox(options);無法讓JComboBox顯示
添加該行後,窗口上不再顯示任何內容(無按鈕,無標籤,無顏色等)。
有人可以幫我弄清楚這行代碼有什麼問題(它沒有顯示語法錯誤)。
import java.awt.*;
import javax.swing.*;
public class JavaApplication23 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setTitle("Test program");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
JLabel label = new JLabel("Hello");
JButton button = new JButton("Click");
String[] options = new String[] {"Cat", "Dog"};
JComboBox comboBox = new JComboBox(options); //It goes wrong when I add this line
JPanel topPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel bottomPanel = new JPanel();
topPanel.add(label);
bottomPanel.add(button);
centerPanel.add(comboBox);
frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(bottomPanel, BorderLayout.PAGE_END);
frame.add(centerPanel, BorderLayout.CENTER);
}
}
所有組件添加後調用'setVisible' – Reimeus 2014-10-04 22:01:10
非常感謝您的幫助。這解決了問題。 – user2939293 2014-10-04 22:11:41