java JComboBox导致运行时错误
我正在用Java进行一些GUI开发,当我添加以下代码时,我在初始化类时遇到了一个运行时错误:
public class Search_Album_Main_Tab extends JPanel{
JComboBox search_list;
JTextArea searched_data;
JButton search_button;
Results_Main_Tab rmt;
Search_Action_Listener listener;
public Search_Album_Main_Tab(Results_Main_Tab results_main_tab)
{
String[] search_options = {"Album", "Artist", "Genre", "ID", "Year"};
setLayout(new GridLayout(3,1));
rmt = results_main_tab;
listener = new Search_Action_Listener();
/* Searched data */
searched_data = new JTextArea();
/* Search button */
search_button = new JButton("Search Album");
search_button.addActionListener(listener);
/* Drop down menu */
search_list = new JComboBox(search_options);
add(search_list);
add(searched_data);
add(search_button);
}
private class Search_Action_Listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == search_button)
{
}
}
}
}
我得到的错误是:
dcm_gui.GUI at localhost:53806
Thread [main] (Suspended (exception NullPointerException))
CUIAquaComboBox.applySizeFor(JComponent, CoreUIConstants$Size) line: 454
CUIAquaUtilControlSize.applyUISizing(JComponent, CoreUIConstants$Size) line: 99
CUIAquaUtilControlSize.access$200(JComponent, CoreUIConstants$Size) line: 13
CUIAquaUtilControlSize$PropertySizeListener.applyComponentSize(JComponent, Object) line: 121
CUIAquaUtilControlSize.addSizePropertyListener(JComponent) line: 25
CUIAquaComboBox.installListeners() line: 47
CUIAquaComboBox(BasicComboBoxUI).installUI(JComponent) line: 229
CUIAquaComboBox.installUI(JComponent) line: 30
JComboBox(JComponent).setUI(ComponentUI) line: 653
JComboBox.setUI(ComboBoxUI) line: 238
JComboBox.updateUI() line: 247
JComboBox.init() line: 212
JComboBox.<init>(Object[]) line: 178
Search_Album_Main_Tab.<init>(Results_Main_Tab) line: 36
GUI.main(String[]) line: 28
Daemon Thread [AWT-AppKit] (Running)
/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java (Oct 10, 2010 11:55:27 PM)
有人知道为什么吗
宾雅明
# 1 楼答案
我认为艾丽卡关于非标准外观的看法是对的;感觉
CUIAquaComboBox
听起来不像是标准元素如果你不确定要看什么&;如果感觉到你正在使用LAF,请致电
System.out.println(UIManager.getLookAndFeel());
进行检查更重要的是,在GUI启动之前,试着在主方法中调用它,看看它是否会产生影响:
这将为您当前的操作系统使用本机LAF(如果可以找到的话——如果您使用的是Windows、Mac、Linux、Solaris等,这应该可以正常工作)
# 2 楼答案
这似乎与您的定制外观和感觉有关。我以前在使用定制外观时也遇到过类似的问题。这里描述:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4711700
这是针对JFileChooser小部件的,不是一个组合框,但它可能是相关的
你应该尝试的第一件事是恢复默认的“金属”外观。如果这是可行的,那么这可能是特定L&;F.我没有使用CUIAqua L&;F之前,但我认为这是官方的苹果Java L&;F.你也可以尝试将其切换为QuaQua。它是像L&;F、 这可能(取决于实际的bug)避免了问题