0

好像我在我的代码中发现了一个新问题。我的代码没有(可见的)错误,但我仍然没有框架。请帮忙

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class hello{
//Int's and things
static JButton Play = new JButton("<PLAY>");
static JFrame pane = new JFrame("CIrCUT 0.0.2");
static JLabel Title = new JLabel("CIrCUT");
static JLabel none = new JLabel(" ");
static JPanel panel = new JPanel(new GridLayout(10,10,10,10));
static JButton Options = new JButton("<OPTIONS>");
static JPanel panel2 = new JPanel(new GridLayout(10,10,10,10));
static String b[]  = new String[3];
static int panelLoct =1;
JComboBox optionlist = new JComboBox();


    void initialize(){
    b[0] = "High";
    b[1] = "Medium";
    b[2] = "Low";

    //title
    pane.setTitle("CIrCUT 0.0.2");
    //drop down

    optionlist .setModel(new DefaultComboBoxModel(new String[] {"Option", "High", "Medium",  "Low"}));
    optionlist.setSelectedIndex(3);
    optionlist.addActionListener((ActionListener) this);
    //other pane-related things
    if(panelLoct==1){
    pane.setLayout(new GridLayout(10,10));
    panel.setMaximumSize(new Dimension(500,500));
    pane.setSize(500,500);
    pane.setMaximumSize(new Dimension(500,500));
    panel.add(Title);
    panel.add(none);
    panel.add(Play);
    panel.add(Options);
    panel2.add(optionlist);
    Play.setSize(new Dimension(500,450));
    pane.setLocation(500,50);
    pane.setBackground(Color.lightGray);
    pane.setContentPane(panel);
    pane.pack();
    pane.setMinimumSize(new Dimension(500,500));
    pane.setContentPane(panel);
    OptionButtonHandler cbHandler = new OptionButtonHandler();
    Options.addActionListener(cbHandler);
    pane.setVisible(true);
    }
}
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
     private static class OptionButtonHandler implements ActionListener

         {
             public void actionPerformed(ActionEvent e){
                 pane.remove(panel);
                 pane.add(panel2);
             }
         }
     public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            cb.getSelectedItem();
     }

public static void main(String args[])
{
    hello a = new hello();
    a.initialize();
}
}

任何帮助都将不胜感激。这是我尝试运行它时遇到的错误

Exception in thread "main" java.lang.ClassCastException: hello cannot be cast to java.awt.event.ActionListener
    at hello.initialize(hello.java:39)
    at hello.main(hello.java:83)

这可能是一个新手错误,但找不到如何解决它。

4

3 回答 3

3

您的程序确实有一个明显的错误,正是它失败的地方:

optionlist.addActionListener((ActionListener) this);

this是对 的实例的引用hello。该类hello未实现ActionListener- 那么您如何期望该演员成功?

ActionListener你给出的唯一实现是OptionButtonHandler. 也许你的意思是:

optionlist.addActionListener(new OptionButtonHandler());

?

于 2013-02-28T22:26:06.687 回答
2
    optionlist.addActionListener((ActionListener) this);

仅仅因为你会做某事并不意味着它是可能的。在这种情况下,hello不是一个ActionListener,所以这将失败。

如果你发现自己在铸造,你可能会遇到问题。

于 2013-02-28T22:26:16.897 回答
0

您必须实现 ActionListener。

public class Hello implements ActionListener{ 

}
于 2013-02-28T22:28:55.357 回答