Kann kein ActionListener hinzufügen, bekomme immer eine Fehlermedung warum?

Code46

Erfahrenes Mitglied
Hallo ich schreibe gerade ein code,jedoch bekomme ich einen Fehler das man kein ActionListener hinzufügen kann.Dies hat mit jetzt verwirrt.Könntet ihr mir vielleicht weiter helfen.

Das ist mein Code und die Fehlermeldung ist dadrunter:

Code:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;


class Product implements Comparable {
    private int cost;
    private String name;
	private String Typ;
public Product(int c, String n, String t) {
    cost = c;
    name = n;
	Typ = t;

}
public Product() {
    this (0, "default","default");
}
public int getCost() {
    return cost;
}
public String getName() {
    return name;
}

public String getTyp() {
    return Typ;
}


public int compareTo(Object o) {
    Product temp = (Product) o;

// Strings already implement compareTo() so just use that
    return (name.compareTo(temp.name));
}
}
public class GUI1 extends JFrame {

// A container for the Product instances
  ArrayList products = new ArrayList();

// GUI bits and bobs
  JPanel panInput = new JPanel(new GridLayout(2,1)),
         panAdd = new JPanel(),
         panDelSort = new JPanel();

  JTextField txtName = new JTextField(10),
             txtCost = new JTextField(5),
             txtType = new JTextField(10);

  JButton btnAdd = new JButton("Add"),
          btnDelete = new JButton("Delete"),
          btnSort = new JButton("Sort Products");

  JComboBox type = new  JComboBox(); 

  JTable tab = new JTable();


  DefaultTableModel tabMod = new DefaultTableModel();

  Product [] initialProducts = {new Product(12, "Harry Potter","Hans"),
                                new Product(),
                                new Product()};

  public GUI1() {
    super("DVD List");
    tab.setModel(tabMod);
    tabMod.addColumn("Title");
	tabMod.addColumn("Price");
	tabMod.addColumn("Type");
	tabMod.addColumn("Notes");

    btnAdd.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          addProduct();
      }
    });



    btnDelete.setToolTipText("Select the records to delete first");
    btnDelete.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          deleteProduct();
      }
    });

    btnSort.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          sortProducts();
      }
    });

 

    
    panAdd.add(new JLabel("DVD Title:"));
    panAdd.add(txtName);
    panAdd.add(new JLabel("DVD Price:"));
    panAdd.add(txtCost);
	panAdd.add(new JLabel("DVD Type:"));
    panAdd.add(type);
	type.addActionListener(this);
	String [] typeStr = { "Film", "Music"};
	for (int i = 0; i < typeStr.length; ++i) {
	
    type.addItem(typeStr[i]);
	}

    panDelSort.add(btnAdd);
    panDelSort.add(btnDelete);
    panDelSort.add(btnSort);

    panInput.add(panAdd);
    panInput.add(panDelSort);
    getContentPane().add(panInput, BorderLayout.WEST);
    getContentPane().add(new JScrollPane(tab));

    loadInitialProducts();
}


    


// Delete a product from the ArrayList and from the JTable
public void deleteProduct() {
    int [] rows = tab.getSelectedRows();
    for (int i = 0; i < rows.length; i++) {
        tabMod.removeRow(rows[i] - i); // delete from JTable
        products.remove(rows[i] - i); // delete from ArrayList
    }
}

// Sort the products in the ArrayList and then
// reload them into the JTable in the sorted order

public void sortProducts() {
    Collections.sort(products); // sorts into name order
    loadProductsIntoTable();
}

// Add a new products to the ArrayList and the JTable
public void addProduct() {
    String [] row = new String [3];
    Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText(),txtType.getText());

    products.add(newProd); // add to the ArrayList
    row[0] = newProd.getName();
    row[1] = Integer.toString(newProd.getCost());
	row[2] = newProd.getTyp();	

    tabMod.addRow(row); // add to the JTable
}
public void loadInitialProducts() {
    for (int i = 0; i < initialProducts.length; i++) {
        products.add(initialProducts[i]);
    }
    loadProductsIntoTable();
}
public void loadProductsIntoTable() {
   tabMod.setRowCount(0);

   String [] row = new String [3];
    for (Iterator i = products.iterator(); i.hasNext();) {
       Product temp = (Product) i.next();
       row[0] = temp.getName();
       row[1] = Integer.toString(temp.getCost());
	   row[2] = temp.getTyp();	
      tabMod.addRow(row);
   }
}
public static void main(String [] a){
    GUI1 me = new GUI1();
    me.addWindowListener(new WindowAdapter () {
       public void windowClosing(WindowEvent e) {
          System.exit(0) ;
        }
      });
    me.pack();
    me.setVisible(true);
  }
}

---------- Javac ----------
GUI1.java:108: addActionListener(java.awt.event.ActionListener) in javax.swing.JComboBox cannot be applied to (GUI1)
type.addActionListener(this);
^
Note: GUI1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Output completed (0 sec consumed) - Normal Termination
 
Hi

du musst bei

Java:
public class GUI1 extends JFrame

noch das Interface mit angeben

Also

Java:
public class GUI1 extends JFrame implements Actionlistener

Gruß
 
Das ist meine Fehlermeldung.Funktioniert trotzdem nicht

---------- Javac ----------
GUI1.java:46: GUI1 is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
public class GUI1 extends JFrame implements ActionListener {
^
Note: GUI1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

Output completed (0 sec consumed) - Normal Termination
 
Ja, du musst schon eine Methode actionPerformed in die Klasse einbauen.

Was soll denn deiner Meinungs nach sonst beim Actionlistener passieren?
Der will ein actionPerformed, das er aufrufen kann.
 
Habe doch eine andere Frage, ich habe dort ein ComboBox eingefügt jedoch fügt er dies nit zu dem JTabel hinzu woran kann das liegen****
 
Ist es wirklich so schwierig den entsprechend Codeausschnitt zu kopieren? Mehr als raten können wir sonst nicht.
 
Code:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;


class Product implements Comparable {
    private int cost;
    private String name;
	private String Typo;
	private String notes;
public Product(int c, String n, String t,String nts) {
    cost = c;
    name = n;
	Typo = t;
	notes = nts;

}
public Product() {
    this (0, "default","default","default");
}
public int getCost() {
    return cost;
}
public String getName() {
    return name;
}

public String getTypo() {
    return Typo;
}

public String getNotes() {
    return notes;
}


public int compareTo(Object o) {
    Product temp = (Product) o;

// Strings already implement compareTo() so just use that
    return (name.compareTo(temp.name));
}
}
public class GUI1 extends JFrame implements ActionListener {

// A container for the Product instances
  ArrayList products = new ArrayList();

// GUI bits and bobs
  JPanel panInput = new JPanel(new GridLayout(2,1)),
         panAdd = new JPanel(),
         panDelSort = new JPanel();

  JTextField txtName = new JTextField(10),
             txtCost = new JTextField(5),
             txtTypo = new JTextField(10);

  JTextArea notes = new JTextArea(3,30);

  JButton btnAdd = new JButton("Add"),
          btnDelete = new JButton("Delete"),
          btnSort = new JButton("Sort Products");

  JComboBox Typo = new  JComboBox(); 

  JTable tab = new JTable();


  DefaultTableModel tabMod = new DefaultTableModel();

  Product [] initialProducts = {new Product(12, "Harry Potter","Film","4 Oscars")};

  public GUI1() {
    super("DVD List");
    tab.setModel(tabMod);
    tabMod.addColumn("Title");
	tabMod.addColumn("Price");
	tabMod.addColumn("Type");
	tabMod.addColumn("Notes");

    btnAdd.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          addProduct();
      }
    });



    btnDelete.setToolTipText("Select the records to delete first");
    btnDelete.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          deleteProduct();
      }
    });

    btnSort.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          sortProducts();
      }
    });

 

    
    panAdd.add(new JLabel("DVD Title:"));
    panAdd.add(txtName);
    panAdd.add(new JLabel("DVD Price:"));
    panAdd.add(txtCost);
	panAdd.add(new JLabel("DVD Type:"));
    panAdd.add(Typo);
	Typo.addActionListener(this);
	String [] typeStr = { "Film", "Music"};
	for (int i = 0; i < typeStr.length; ++i) {
	
    Typo.addItem(typeStr[i]);
	}
    panAdd.add(new JLabel("Notes:"));
    panAdd.add(notes);

    panDelSort.add(btnAdd);
    panDelSort.add(btnDelete);
    panDelSort.add(btnSort);

    panInput.add(panAdd);
    panInput.add(panDelSort);
    getContentPane().add(panInput, BorderLayout.WEST);
    getContentPane().add(new JScrollPane(tab));

    loadInitialProducts();
}


    


// Delete a product from the ArrayList and from the JTable
public void deleteProduct() {
    int [] rows = tab.getSelectedRows();
    for (int i = 0; i < rows.length; i++) {
        tabMod.removeRow(rows[i] - i); // delete from JTable
        products.remove(rows[i] - i); // delete from ArrayList
    }
}

// Sort the products in the ArrayList and then
// reload them into the JTable in the sorted order

public void sortProducts() {
    Collections.sort(products); // sorts into name order
    loadProductsIntoTable();
}

// Add a new products to the ArrayList and the JTable
public void addProduct() {
    String [] row = new String [4];
    Product newProd = new Product(Integer.parseInt(txtCost.getText()), txtName.getText(),txtTypo.getText(),notes.getText());

    products.add(newProd); // add to the ArrayList
    row[0] = newProd.getName();
    row[1] = Integer.toString(newProd.getCost());
	row[2] = newProd.getTypo();	
	row[3] = newProd.getNotes();

    tabMod.addRow(row); // add to the JTable
}
public void loadInitialProducts() {
    for (int i = 0; i < initialProducts.length; i++) {
        products.add(initialProducts[i]);
    }
    loadProductsIntoTable();
}
public void loadProductsIntoTable() {
   tabMod.setRowCount(0);

   String [] row = new String [4];
    for (Iterator i = products.iterator(); i.hasNext();) {
       Product temp = (Product) i.next();
       row[0] = temp.getName();
       row[1] = Integer.toString(temp.getCost());
	   row[2] = temp.getTypo();	
	   row[3] = temp.getNotes();
      tabMod.addRow(row);
   }
}
public static void main(String [] a){
    GUI1 me = new GUI1();
    me.addWindowListener(new WindowAdapter () {
       public void windowClosing(WindowEvent e) {
          System.exit(0) ;
        }
      });
    me.pack();
    me.setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {

}
}
 
Zurück