Java.swing.ComboBox无法在Button classException

时间:2016-04-05 11:47:04

标签: java swing classcastexception

出于某种原因,我不知道为什么我的ComboBox无法在按钮上显示。请帮忙:

代码:

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);
    }
  }
}

错误:

  

线程中的异常" AWT-EventQueue-0" java.lang.ClassCastException:   javax.swing.JButton无法强制转换为javax.swing.JComboBox   LocalBank.actionPerformed(LocalBank.java:124)at   javax.swing.AbstractButton.fireActionPerformed(未知来源)at   javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at   javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at   javax.swing.DefaultButtonModel.setPressed(未知来源)at   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知   来自)java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Window.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 400(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

1 个答案:

答案 0 :(得分:5)

您已将ActionListseners添加到两个组件:Button和JComboBox。

所以,

  • 当JComboBox发生更改时,您的代码可以正常工作。
  • 但是当点击按钮时,event.getSource()指向Button,这不是ComboBox

我建议您将ItemListener添加到组合框中,而不是动作侦听器(请参阅thisthis link about ItemListener