我有一个包含大约26种不同标签的GUI,但是希望能够在单击时为所有标签执行相同的代码,所以我想我会使用MouseListener和mouseClicked方法来执行代码。我遇到的问题是当我点击任何标签时,我收到此错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JRootPane cannot be cast to javax.swing.JLabel
at dealornodeal.ui.DealOrNoDealUI.mouseClicked(DealOrNoDealUI.java:628)
at java.awt.Component.processMouseEvent(Component.java:6528)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我可以看到mouseClicked()方法返回一个JRootPane组件而不是一个JLabel组件,并且JRootPane显然无法转换为JLabel。我的问题是,如何在使用mouseClicked()方法而不是JRootPane时获取JLabel组件?
这是我的代码,我将MouseListener添加到所有JLabel,然后尝试在mouseClicked()上执行代码:
public DealOrNoDealUI() {
initComponents();
this.setLocationRelativeTo(null);
this.components = this.getComponents();
for(Component component : this.components){
component.addMouseListener(this);
}
}
public void mouseClicked(MouseEvent e) {
Component clickedComponent = e.getComponent();
JLabel clickedLabel = (JLabel) clickedComponent;
if(this.game.getPlayer().getPlayerCase() == null){
for(Case gameCase : this.game.getCases()){
if(gameCase.getCaseNumber() == Integer.parseInt(clickedLabel.getText())){
this.game.getPlayer().setPlayerCase(gameCase);
}
}
}
}
我使用了GUI的空布局。
答案 0 :(得分:3)
for(Component component : this.components){
这是您问题的根源,我假设您正在添加"标签"到JFrame
,这将解释类投射异常。
JFrame
(以及大多数顶级Swing容器)包含JRootPane
,其中包含contentPane
,mennuBar
和glassPane
...
基本上,您已经选择了JRootPane
(JFrame
中唯一真正的直接孩子),并将MouseListener
添加到其中。
如果不了解您如何建立用户界面,则无法做出准确的建议,但在大多数情况下,您可能会有一个工厂方法创建标签,用于执行基本初始化和配置,返回JLabel
protected JLabel makeLabel(String text) {
JLabel label = new JLabel(text);
label.addMouseListener(someInstanceOfAMouseListener);
return label;
}
然后你可以调用这个方法,并且标签基本上是自动生成的。
另一种方法是将所有标签放入数组或列表中,并使用for-loop
来管理它们
private JLabel[] listOfLabels;
//...
listOfLabels = new JLabel[count];
for (int index = 0; index < count; index++) {
listOfLabels[index] = new JLabel(...);
listOfLabels[index].addMouseListener(someInstanceOfAMouseListener);
}
或其组合......