Java.swing.ComboBox cant be cast on Button classException
For some reason i dont know why my ComboBox cant be case on the button. Please help:
Code:
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LocalBank implements ActionListener {
JFrame frame;
JPanel contentPane;
JLabel label1;
JLabel label2;
JLabel label3;
JTextField accNumber;
JTextField withdraw;
JTextField text3;
JButton avgButton;
JLabel average;
JComboBox plantNames;
JTextField firstname;
JTextField lastname;
JTextField beggining;
JButton submit;
JLabel accNumLabel;
JLabel withdawLabel;
JLabel firstnameLabel;
JLabel lastnameLabel;
JLabel begginingLabel;
public LocalBank() {
// TODO Auto-generated constructor stub
frame = new JFrame("Semiseter");
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder
(20, 20, 20, 20));
label1 = new JLabel("Enter An Integer");
label1.setAlignmentX(JLabel.LEFT_ALIGNMENT);
contentPane.add(label1);
String[] names = {"Deposit", "Withdrawal", "Check Balance","Add Account","Remove Account"};
plantNames = new JComboBox(names);
plantNames.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
plantNames.setSelectedIndex(0);
plantNames.addActionListener(this);
contentPane.add(plantNames);
accNumLabel = new JLabel("Account Number");
contentPane.add(accNumLabel);
accNumber = new JTextField();
contentPane.add(accNumber);
withdawLabel = new JLabel("Amount of deposit/withdrawal:");
contentPane.add(withdawLabel);
withdraw = new JTextField("");
contentPane.add(withdraw);
firstnameLabel = new JLabel("First Name:");
contentPane.add(firstnameLabel);
firstname = new JTextField();
contentPane.add(firstname);
lastnameLabel = new JLabel("Last Name:");
contentPane.add(lastnameLabel);
lastname = new JTextField("");
contentPane.add(lastname);
begginingLabel = new JLabel("Beginning Balance:");
contentPane.add(begginingLabel);
beggining = new JTextField("");
contentPane.add(beggining);
submit = new JButton("Process Transaction");
submit.addActionListener(this);
submit.setActionCommand("Submit");
contentPane.add(submit);
frame.setContentPane(contentPane);
/* Size and then display the frame. */
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
LocalBank greeting = new LocalBank();
}
public static void main(String[] args) {
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
JComboBox comboBox = (JComboBox)event.getSource();
String plantName = (String)comboBox.getSelectedItem();
String eventString = event.getActionCommand();
if (plantName == "Deposit") {
accNumLabel.setVisible(true);
accNumber.setVisible(true);
withdawLabel.setVisible(true);
withdraw.setVisible(true);
firstnameLabel.setVisible(false);
firstname.setVisible(false);
lastnameLabel.setVisible(false);
lastname.setVisible(false);
begginingLabel.setVisible(false);
beggining.setVisible(false);
if(eventString.equals("Submit")){
System.out.println("Person with account number " + accNumLabel.getText().toString() + " Withdraws $" + withdawLabel.getText().toString());
}
}
else if (plantName == "Withdrawal") {
accNumLabel.setVisible(true);
accNumber.setVisible(true);
withdawLabel.setVisible(true);
withdraw.setVisible(true);
firstnameLabel.setVisible(false);
firstname.setVisible(false);
lastnameLabel.setVisible(false);
lastname.setVisible(false);
begginingLabel.setVisible(false);
beggining.setVisible(false);
}
else if (plantName == "Check Balance") {
accNumLabel.setVisible(true);
accNumber.setVisible(true);
firstnameLabel.setVisible(false);
firstname.setVisible(false);
lastnameLabel.setVisible(false);
lastname.setVisible(false);
begginingLabel.setVisible(false);
beggining.setVisible(false);
withdawLabel.setVisible(false);
withdraw.setVisible(false);
}
else if (plantName == "Add Account") {
accNumLabel.setVisible(false);
accNumber.setVisible(false);
withdawLabel.setVisible(false);
withdraw.setVisible(false);
firstnameLabel.setVisible(true);
firstname.setVisible(true);
lastnameLabel.setVisible(true);
lastname.setVisible(true);
begginingLabel.setVisible(true);
beggining.setVisible(true);
}
else if (plantName == "Remove Account") {
accNumLabel.setVisible(true);
accNumber.setVisible(true);
withdawLabel.setVisible(false);
withdraw.setVisible(false);
firstnameLabel.setVisible(false);
firstname.setVisible(false);
lastnameLabel.setVisible(false);
lastname.setVisible(false);
begginingLabel.setVisible(false);
beggining.setVisible(false);
}
else{
accNumLabel.setVisible(false);
accNumber.setVisible(false);
withdawLabel.setVisible(false);
withdraw.setVisible(false);
firstnameLabel.setVisible(false);
firstname.setVisible(false);
lastnameLabel.setVisible(false);
lastname.setVisible(false);
begginingLabel.setVisible(false);
beggining.setVisible(false);
}
}
}
Error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox at LocalBank.actionPerformed(LocalBank.java:124) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(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 java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispa tcher.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.pumpOneEven tForFilters(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)
You have added ActionListseners to both you components: The Button, And the JComboBox.
So,
event.getSource()
points to the Button, which is not the ComboBox I suggest you to add an ItemListener to your combo box, instead of an Action Listener (See this, and this link about ItemListener)
链接地址: http://www.djcxy.com/p/73442.html上一篇: 无法识别Java程序中的错误