Java.swing.ComboBox不能转换为Button classException

出于某种原因,我不知道为什么我的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.AbstractButton.fireActionPerformed被转换为在javax.swing.JComboBox中LocalBank.actionPerformed(LocalBank.java:124)(来源不明)在javax.swing.AbstractButton中$ Handler.actionPerformed(来源不明)在javax.swing.DefaultButtonModel.fireActionPerformed(来源不明)在javax.swing.DefaultButtonModel.setPressed(来源不明)在javax.swing.plaf.basic中。在java.awt.Container的java.awt.Component.processEvent(Unknown Source)上的javax.swing.JComponent.processMouseEvent(Unknown Source)上的java.awt.Component.processMouseEvent(Unknown Source)上的BasicButtonListener.mouseReleased(Unknown Source)。的processEvent(未知来源)在java.awt.Component.dispatchEventImpl(未知来源)在java.awt.Container.dispatchEventImpl(未知来源)在java.awt.Component.dispatchEvent(未知来源)在java.awt.LightweightDispatcher.retargetMouseEvent(未知源)在java.awt.LightweightDispa tcher.processMouseEvent(未知来源)在java.awt.LightweightDispatcher.dispatchEvent(未知来源)在java.awt.Container.dispatchEventImpl(未知来源)在java.awt.Window.dispatchEventImpl(未知来源)在java.awt.Component中。 java.awt.EventQueue上的java.awt.EventQueue.dispatchEventImpl(Unknown Source)上的dispatchEvent(Unknown Source)java.awt.EventQueue上的$ 400(Unknown Source)java.awt.EventQueue上的$ 3.run(Unknown Source) .RUN(来源不明)在java.security.AccessController.doPrivileged(本机方法)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(来源不明)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(来源不明)在java.awt.EventQueue中java.awt.EventQueue上的$ 4.run(Unknown Source)在java.security.ProtectionDomain上的java.security.AccessController.doPrivileged(Native Method)处获得$ 4.run(Unknown Source)。java.awt处的$ 1.doIntersectionPrivilege(Unknown Source)。 EventQueue.dispatchEvent(Unknown Source)at java.awt.EventDispatchThread.pumpOneEven tForFilters(未知来源)在java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)在java.awt.EventDispatchThread.pumpEvents(未知来源)在java.awt.EventDispatchThread.pumpEvents(未知源)在java.awt.EventDispatchThread.run(未知源)


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

所以,

  • 当JComboBox发生更改时,您的代码正常工作。
  • 但是当按钮被点击时, event.getSource()指向Button,它不是ComboBox
  • 我建议你添加一个ItemListener到你的组合框,而不是一个Action Listener(参见这个和关于ItemListener的链接)

    链接地址: http://www.djcxy.com/p/73441.html

    上一篇: Java.swing.ComboBox cant be cast on Button classException

    下一篇: Java JTextfield gives me only null data