我最近发现这个有用的代码来创建一个标题边框,其中包含Santhosh Kumar T标题的组件:ComponentTitledBorder。我使用复选框成功使用它,但当我决定使用组合框时,它只显示组合框的文本部分,但不显示下拉按钮:
当我点击组合框时,我得到以下异常:
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);
}
});
}
}
请帮我解决这个问题并希望了解错误的本质。
答案 0 :(得分:2)
使用JPanel
定义BorderLayout
并将JComboBox
添加到面板。然后将ComponentTitledBorder
分配给面板,而不是直接分配给JComboBox
。
更新:
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(cb, BorderLayout.CENTER);
area.setBorder(new ComponentTitledBorder(p, area,
UIManager.getBorder("TitledBorder.border")));