javax.swing.JComboBox 无法转换为 javax.swing.JButton
javax.swing.JComboBox cannot be cast to javax.swing.JButton
这是我使用的代码:
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;
}
}
当我尝试将 JComboBox 从 "Phone Mode" 更改为 "Email Mode" 时抛出此错误:
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.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
提前感谢您的帮助!
您有:
JButton buttonPressed = (JButton) arg0.getSource();
JComboBox selectedOption = (JComboBox) arg0.getSource();
所以在同一个调用中,您试图将接收到的值转换为 JButton
和 JComboBox
.
如果您确实将相同的 ActionListener
分配给了两个不同的控件,那么您需要有逻辑来区分它们,方法是在目标上使用 typeof
或 getClass()
之类的东西.
您可能还想完成 How to Write an Action Listener 教程。
你可以...
为按钮和组合框使用单独的 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
}
这是我使用的代码:
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;
}
}
当我尝试将 JComboBox 从 "Phone Mode" 更改为 "Email Mode" 时抛出此错误:
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.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.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
提前感谢您的帮助!
您有:
JButton buttonPressed = (JButton) arg0.getSource();
JComboBox selectedOption = (JComboBox) arg0.getSource();
所以在同一个调用中,您试图将接收到的值转换为 JButton
和 JComboBox
.
如果您确实将相同的 ActionListener
分配给了两个不同的控件,那么您需要有逻辑来区分它们,方法是在目标上使用 typeof
或 getClass()
之类的东西.
您可能还想完成 How to Write an Action Listener 教程。
你可以...
为按钮和组合框使用单独的 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
}