tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
7
ZUGRIFFE
482
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    
    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
     

  2. #2
    Avatar von sheel
    sheel sheel ist gerade online Moderator
    tutorials.de Moderator
    Registriert seit
    Jul 2007
    Beiträge
    4.506
    Hi

    du musst bei

    Code java:
    1
    
    public class GUI1 extends JFrame

    noch das Interface mit angeben

    Also

    Code java:
    1
    
    public class GUI1 extends JFrame implements Actionlistener

    Gruß
     

  3. #3
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    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
     

  4. #4
    Avatar von sheel
    sheel sheel ist gerade online Moderator
    tutorials.de Moderator
    Registriert seit
    Jul 2007
    Beiträge
    4.506
    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.
     

  5. #5
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    AAAHH OK. Funktioniert jetzt danke schön.Vielen dank.
     

  6. #6
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    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****
     

  7. #7
    Avatar von timestamp
    timestamp timestamp ist offline Mitglied Rubin
    Registriert seit
    May 2010
    Ort
    Marburg
    Beiträge
    1.479
    Ist es wirklich so schwierig den entsprechend Codeausschnitt zu kopieren? Mehr als raten können wir sonst nicht.
     
    Bei Problemen mit Codes, postet bitte den entsprechenden Codeausschnitt und setzt den in entsprechende Tags.
    ( [cpp] [/cpp] [css] [/css] [html] [/html] [java] [/java] [javascript] [/javascript] [php] [/php] [sql] [/sql] )
    "Funktioniert nicht" ist keine Fehlermeldung. Bitte eine genaue Fehlerbeschreibung und, wenn vorhanden, Fehlermeldungen posten.
    RegEx Tutorial
    PHP Funktionsreferenz

  8. #8
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    
    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) {
     
    }
    }
     

Ähnliche Themen

  1. Antworten: 0
    Letzter Beitrag: 13.10.10, 12:10
  2. Antworten: 0
    Letzter Beitrag: 29.02.08, 22:45
  3. Warum bekomme ich keine Ausgabe?
    Von cheepy im Forum C/C++
    Antworten: 4
    Letzter Beitrag: 03.05.07, 12:45
  4. Warum bekomme ich kaum Antworten?
    Von jccTeq im Forum Smalltalk
    Antworten: 9
    Letzter Beitrag: 17.02.05, 10:03
  5. Wie kann man bei CGridCtrl, dynamisch eine Zeile hinzufügen?
    Von biu-jee im Forum VisualStudio & MFC
    Antworten: 0
    Letzter Beitrag: 12.09.04, 18:59