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:
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