2013-02-28 80 views
0

看起來好像我在代碼中發現了一個新問題。我的代碼沒有(可見)錯誤,但我仍然沒有框架。請幫忙線程「main」中的異常java.lang.ClassCastException:hello無法轉換爲java.awt.event.ActionListener

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) 

這可能是一個新手的錯誤,但能找到如何解決它罷了。

+0

什麼是83行? – 2013-02-28 22:27:44

+0

作爲一名讀者,您可能想要閱讀[如何編寫動作偵聽器](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer 2013-02-28 22:28:40

回答

2
optionlist.addActionListener((ActionListener) this); 

只因爲你會有某種東西並不意味着它是可能的。在這種情況下,hello不是ActionListener,所以這會失敗。

如果您發現自己正在投射,您可能有問題。

+0

非常感謝! – user1507910 2013-02-28 22:29:04

4

你的程序確實有一個明顯的錯誤,恰恰是它的失敗:

optionlist.addActionListener((ActionListener) this); 

this是的hello實例的引用。 hello類沒有實現ActionListener - 那麼您會如何期待投球成功?

您給出的ActionListener的唯一實現是OptionButtonHandler。也許你的意思是:

optionlist.addActionListener(new OptionButtonHandler()); 

0

你必須實現ActionListener。

public class Hello implements ActionListener{ 

} 
相關問題