Java 边框内的JComboBox导致IllegalComponentStateException
我最近发现了这段有用的代码,用于创建一个有标题的边框,其中包含一个组件作为Santhosh Kumar T:的标题。我使用复选框成功地使用了它,但当我决定使用组合框时,它只显示组合框的文本部分,而不显示下拉按钮: 单击组合框时,出现以下异常:Java 边框内的JComboBox导致IllegalComponentStateException,java,swing,border,jcombobox,Java,Swing,Border,Jcombobox,我最近发现了这段有用的代码,用于创建一个有标题的边框,其中包含一个组件作为Santhosh Kumar T:的标题。我使用复选框成功地使用了它,但当我决定使用组合框时,它只显示组合框的文本部分,而不显示下拉按钮: 单击组合框时,出现以下异常: Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to dete
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source)
at java.awt.Component.getLocationOnScreen(Unknown Source)
at javax.swing.JPopupMenu.show(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup.show(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup.togglePopup(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at test.ComponentTitledBorder.dispatchEvent(ComponentTitledBorder.java:74)
at test.ComponentTitledBorder.mousePressed(ComponentTitledBorder.java:96)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我查看了所有我能找到的谷歌搜索结果,但没有一个能帮助我理解我的具体问题。为了说明,我提供了一个sscce:
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class Problem extends JComponent {
JPanel area = new JPanel();
JComboBox<String> cb;
public Problem() {
area.setPreferredSize(new Dimension(100,100));
String[] options = {"one", "two"};
cb = new JComboBox<String>(new DefaultComboBoxModel<String>(options));
cb.setEditable(false);
cb.setSelectedIndex(0);
this.setLayout(new BorderLayout());
area.setBorder(new ComponentTitledBorder(cb, area, UIManager.getBorder("TitledBorder.border")));
this.add(area, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Problem());
frame.pack();
frame.setVisible(true);
}
});
}
}
封装测试;
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入javax.swing.DefaultComboxModel;
导入javax.swing.JComboBox;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
@抑制警告(“串行”)
公共类问题扩展JComponent{
JPanel区域=新的JPanel();
JComboBox cb;
公共问题(){
面积。设置首选尺寸(新尺寸(100100));
字符串[]选项={“一”,“二”};
cb=新的JComboBox(新的DefaultComboxModel(选项));
cb.setEditable(假);
cb.setSelectedIndex(0);
此.setLayout(新的BorderLayout());
setboorder(新组件TitledBorder(cb,area,UIManager.getBorder(“TitledBorder.border”));
添加(区域、边框布局、中心);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新问题());
frame.pack();
frame.setVisible(true);
}
});
}
}
请帮助我解决此问题,并希望了解错误的性质。使用
BorderLayout
定义JPanel
,并将JComboBox
添加到面板中。然后将ComponentTitleBorder
分配给面板,而不是直接分配给JComboBox
更新:
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(cb, BorderLayout.CENTER);
area.setBorder(new ComponentTitledBorder(p, area,
UIManager.getBorder("TitledBorder.border")));
我将代码更改为:
JPanel p=newjpanel();p、 setLayout(新的BorderLayout());p、 添加(cb,BorderLayout.CENTER);setboorder(新组件TitledBorder(p,area,UIManager.getBorder(“TitledBorder.border”))代码>但现在组合框甚至没有显示。不要在无法阅读的注释中发布代码。改为将其编辑到问题中。@AndrewThompson转到答案谢谢您格式化了仍然无用的信息。。)我发现作者自己也知道这个问题,正如你在这里看到的(隐藏在评论中),但这并不能解释为什么会发生错误。