Java Eclipse cannot be cast to java.awt.event.ItemListener

Duardo

Mitglied
Hallo, ich habe hier ein Problem. Wenn ich versuche das Programm auszuführen bekomme ich diesen Fehler:
Code:
Exception in thread "main" java.lang.ClassCastException: java.awt.Checkbox cannot be cast to java.awt.event.ItemListener
    at Lottozahlen_Generator.main(Lottozahlen_Generator.java:76)

Hier bin ich ein bisschen ratlos. Mein Programmcode:

Java:
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;


public class Lottozahlen_Generator implements  ItemListener, ActionListener, ListDataListener  {
    
    static JFrame frameApplikation;
    
    static Container containerApplikation;
    
    static JComboBox dropDownWaehlen;
    static JComboBox dropDownNachbarLinks;
    static JComboBox dropDownNachbarRechts;
    static DefaultComboBoxModel comboBoxModel;
    static DefaultComboBoxModel comboBoxModel2;
    static DefaultComboBoxModel comboBoxModel3;
    
    static JButton ok;

    static JPanel panelLeft;
    static JPanel panelCenter;
    static JPanel panelRight;
    static JPanel panelBottom;

    static CheckboxGroup checkboxgroupRadio;

    // Checkboxen Radio
    static Checkbox checkboxLinks;
    static Checkbox checkboxRechts;


    // Label
    static JLabel labelSchritt1;
    static JLabel labelSchritt2;
    static JLabel labelSchritt3;
  
    public static void main(String[] args) {
      
    // JFrame
    frameApplikation = new JFrame();
    frameApplikation.setTitle("Lottozahlen Rechner 1.0");

    // Container
    containerApplikation = frameApplikation.getContentPane();

    // Panel
    panelLeft = new JPanel();
    panelCenter = new JPanel();
    panelRight = new JPanel();

    // Checkboxgroup Radio
    checkboxgroupRadio = new CheckboxGroup();

    // Checkboxen Radio HIER TRITT DAS PROBLEM AUF
    checkboxLinks = new Checkbox("links", checkboxgroupRadio, false);
    checkboxLinks.addItemListener( (ItemListener) checkboxLinks); //
[/SIZE][/h][h=1][SIZE=2]HIER TRITT DAS PROBLEM AUF[/SIZE][/h][h=1][SIZE=2]    checkboxRechts = new Checkbox("rechts", checkboxgroupRadio, false);
    checkboxRechts.addItemListener((ItemListener) checkboxRechts);
      
    //Knopf
    ok = new JButton("OK");
    
    // Label
    labelSchritt1 = new JLabel("Zahl wählen:");
    Font font = new Font("Lotto", Font.BOLD , 20);
    labelSchritt1.setFont(font);
    
    labelSchritt2 = new JLabel("Nachbarzahlen wählen:");
    Font font1 = new Font("Lotto", Font.BOLD , 20);
    labelSchritt2.setFont(font1);
    
    labelSchritt3 = new JLabel("Anzahl von Nachbarzahlen wählen:");
    Font font2 = new Font("Lotto", Font.BOLD , 20);
    labelSchritt3.setFont(font2);

    // panel Left
    panelLeft.setLayout(new GridLayout(1, 1));
    panelLeft.add(dropDownWaehlen);
 
    // panel Center
    panelCenter.setLayout(new GridLayout(1, 2));
    panelCenter.add(checkboxLinks);
    panelCenter.add(checkboxRechts);
    
    //panel Right
    panelRight.setLayout(new GridLayout(1, 2));
    panelRight.add(dropDownNachbarLinks);
    panelRight.add(dropDownNachbarRechts);
    
    //panel Bottom
    panelBottom.add(ok);
    

    // container
    containerApplikation.add(panelLeft, BorderLayout.WEST);
    containerApplikation.add(panelCenter, BorderLayout.CENTER);
    containerApplikation.add(panelRight, BorderLayout.EAST);
    containerApplikation.add(labelSchritt1, BorderLayout.NORTH);

    frameApplikation.pack();
    frameApplikation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frameApplikation.setVisible(true);
    
  // Comboboxen    
    dropDownWaehlen = new JComboBox();
    dropDownNachbarLinks = new JComboBox();
    dropDownNachbarRechts = new JComboBox();
    
    Vector<Integer> zahlen = new Vector<Integer>();
    zahlen.add(0);
    
    Vector<Integer> nachbarzahlenlinks = new Vector<Integer>();
    zahlen.add(1);
    
    Vector<Integer> nachbarzahlenrechts = new Vector<Integer>();
    zahlen.add(1);
    
    comboBoxModel = new DefaultComboBoxModel(zahlen);
    comboBoxModel.addListDataListener ((ListDataListener) comboBoxModel);
    
    comboBoxModel2 = new DefaultComboBoxModel(nachbarzahlenlinks);
    comboBoxModel2.addListDataListener ((ListDataListener) comboBoxModel2);
    
    comboBoxModel3 = new DefaultComboBoxModel(nachbarzahlenrechts);
    comboBoxModel3.addListDataListener ((ListDataListener) comboBoxModel3);
    
    dropDownWaehlen.setModel(comboBoxModel);
    dropDownNachbarLinks.setModel(comboBoxModel2);
    dropDownNachbarRechts.setModel(comboBoxModel3);
  }

@Override
public void contentsChanged(ListDataEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void intervalAdded(ListDataEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void intervalRemoved(ListDataEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    
}

@Override
public void itemStateChanged(ItemEvent arg0) {
    // TODO Auto-generated method stub
    
}
}
 
Zuletzt bearbeitet von einem Moderator:
Du kannst doch nicht eine Checkbox in einen ItemListener konvertieren!!!
Außerdem hättest du noch ganz viele NullPointerExceptions!!!
 
Also was kann ich machen? 'this' kann ich ja nicht benutzen weil das alles static ist.

Hier liegt auch das Problem!
Warum ist alles "static"?
"static" sollte man sinnvoll und behutsam einsetzen, nicht wie hier einfach alles static machen und in die "main"-Methode quetschen.

Mache die Sachen non-static, erzeuge in der "main"-Methode eine Instanz der Klasse Lottozahlen_Generator. Dann kannst du auch "this" benutzen.

Anmerkung: "_" sollten nicht in Klassennamen (oder Methodennamen) verwendet werden, stattdessen alles in UpperCamelCase.
 

Zurück
Oben