这是我正在使用的代码:
public void actionPerformed(ActionEvent arg0) {
JButton buttonPressed = (JButton) arg0.getSource();
JComboBox selectedOption = (JComboBox) arg0.getSource();
if (buttonPressed.getText() == "Spam!") {
if(emailModeBoolean)
sendSpam(sendBox.getText(), "Not Spam", messageBox.getText());
else
sendTextMessage(sendBox.getText(), messageBox.getText());
}
if(selectedOption.getSelectedItem().toString() == "Phone Mode") {
emailModeBoolean = false;
} else if(selectedOption.getSelectedItem().toString() == "Email Mode"){
emailModeBoolean = true;
}
}
当我尝试从#34;电话模式"更改JComboBox时,它会抛出此错误到"电子邮件模式":
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to javax.swing.JButton
at Spammer.actionPerformed(Spammer.java:77)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.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$400(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)
提前感谢您的帮助!
答案 0 :(得分:1)
对按钮和组合框使用单独的ActionListener
...
aButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// Button specific code
}
});
aComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// Combobox specific code
}
});
使用instanceof
确定您正在处理的对象类型......
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source instanceof JButton) {
// Button specific code
} else if (soruce instanceof JComboBox) {
// Combobox specific code
}
答案 1 :(得分:0)
你有:
JButton buttonPressed = (JButton) arg0.getSource();
JComboBox selectedOption = (JComboBox) arg0.getSource();
因此,在同一个电话中,您尝试将收到的值转换为 JButton
和JComboBox
。
如果您确实为两个不同的控件分配了相同的ActionListener
,则需要使用逻辑来通过在目标上使用typeof
或getClass()
之类的内容来区分它们。
您可能还想学习How to Write an Action Listener教程。