2009-09-09 76 views
-1

我有一個用於創建此GUI的巨大文件,並且我通過使用數組縮短了它。我想添加一個ActionListener但正在加入陣列時,我收到以下錯誤:將ActionListener添加到Array創建的GUI時編譯錯誤

/tmp/jc_3531/GUI.java:60: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (GUI) 
      nButtons[c].addActionListener(this); 
        ^
/tmp/jc_3531/GUI.java:69: cannot find symbol 
symbol : method addActionListener(GUI) 
location: class javax.swing.JLabel 
      labels[c].addActionListener(this); 
        ^
/tmp/jc_3531/GUI.java:78: addActionListener(java.awt.event.ActionListener) in javax.swing.JTextField cannot be applied to (GUI) 
      fields[c].addActionListener(this); 
        ^
/tmp/jc_3531/GUI.java:90: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (GUI) 
      sButtons[c].addActionListener(this); 
        ^
4 errors 

我在做什麼錯?是否有一種更簡單的自我診斷方式(如帶有集成語法指南的編輯器)?

這裏是我的代碼

// GUI with navigation amd file manipulation buttons 
import javax.swing.*; // provides basic window features 
import java.awt.event.*; 
import java.awt.GridLayout; 
import java.awt.BorderLayout; 
import java.text.NumberFormat; 

public class GUI extends JFrame 
{ 
    private Inventory inv;  
    private int currentDisplay = 0; 

    private JPanel nButtonJPanel; 
    private String nNames[]={"Load","Add","Modify","Delete","Search","Save"}; 
    private JButton nButtons[]; 

    private JPanel labelJPanel; 
    private String labeled[]={"Item ID","Name","Rating","# in Stock","Price","Inventory Value", 
    "Restocking Fee (5%)","Inventory Value with Fee","Total Value with Fee (all DVD's)"}; 
    private JLabel labels[]; 

    private JPanel fieldJPanel; 
    private String fieldsBlank[]={"","","","","","","","",""}; 
    private JTextField fields[]; 

    private JPanel sButtonJPanel; 
    private String sNames[]={"First","Prev","","Next","Last"}; 
    private JButton sButtons[]; 

    // LabelFrame constructor adds JLabels to JFrame 
    public GUI() 
    { 
     super("Inventory Program v.5"); 
     setLayout(new BorderLayout()); // set frame layout 

     // initialize values 
     RatedDVD d1 = new RatedDVD(1, "One Flew Over The Cuckoo's Nest", 3, 9.99, "PG"); 
     RatedDVD d2 = new RatedDVD(2, "The Matrix", 1, 13.01, "PG13"); 
     RatedDVD d3 = new RatedDVD(3, "Se7en", 7, 11.11, "R"); 
     RatedDVD d4 = new RatedDVD(4, "Oceans Eleven", 11, 9.02, "PG13"); 
     RatedDVD d5 = new RatedDVD(5, "Hitch Hikers Guide to the Galaxy", 42, 10.00, "G");  
     RatedDVD d6 = new RatedDVD(6, "The Invisible Man", 0, 4999.59, "G"); 

     // create inv and enter values 
     inv = new Inventory(); 
     inv.add(d1); 
     inv.add(d2); 
     inv.add(d3); 
     inv.add(d4); 
     inv.add(d5); 
     inv.add(d6); 

     inv.sort(); 

     //make nButtons 
     nButtonJPanel.setLayout(new GridLayout(1, nNames.length, 5, 5)); 
     nButtons=new JButton[nNames.length]; 
     for(int c=0;c<nButtons.length;c++){ 
      nButtons[c]=new JButton(nNames[c]); 
      nButtons[c].addActionListener(this); 
      nButtonJPanel.add(nButtons[c]); } 
     add(nButtonJPanel, BorderLayout.NORTH); 

     //make labels 
     labelJPanel.setLayout(new GridLayout(labeled.length, 1)); 
     labels=new JLabel[labeled.length]; 
     for(int c=0;c<labels.length;c++){ 
      labels[c]=new JLabel(labeled[c]); 
      labels[c].addActionListener(this); 
      labelJPanel.add(labels[c]);} 
     add(labelJPanel, BorderLayout.WEST); 

     //make fields 
     fieldJPanel.setLayout(new GridLayout(fieldsBlank.length, 1)); 
     fields=new JTextField[fieldsBlank.length]; 
     for(int c=0;c<fields.length;c++){ 
      fields[c]=new JTextField(fieldsBlank[c],28); 
      fields[c].addActionListener(this); 
      fields[c].setEditable(false); 
      fieldJPanel.add(fields[c]); } 
      populate(currentDisplay); 
     add(fieldJPanel, BorderLayout.EAST); 

     //make sButtons 
     sButtonJPanel.setLayout(new GridLayout(1, sNames.length, 5, 5)); 
     sButtons=new JButton[sNames.length];  
     Icon dvd50 = new ImageIcon(getClass().getResource("dvd50.jpg")); 
     for(int c=0;c<sButtons.length;c++){ 
      sButtons[c]=new JButton(sNames[c]); 
      sButtons[c].addActionListener(this); 
      sButtonJPanel.add(sButtons[c]);} 
     sButtons[2].setIcon(dvd50);  
     add(sButtonJPanel, BorderLayout.SOUTH); 
    }// end method 

    public void populate(int currentDisplay) 
    { 
    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    fields[0].setText(String.valueOf(inv.get(currentDisplay).getItem())); 
    fields[1].setText(inv.get(currentDisplay).getName()); 
    fields[2].setText(inv.get(currentDisplay).getRating()); 
    fields[3].setText(String.valueOf(inv.get(currentDisplay).getUnits())); 
    fields[4].setText(String.valueOf(nf.format(inv.get(currentDisplay).getPrice()))); 
    fields[5].setText(String.valueOf(nf.format(inv.get(currentDisplay).value()))); 
    fields[6].setText(String.valueOf(nf.format(inv.get(currentDisplay).fee()))); 
    fields[7].setText(String.valueOf(nf.format(inv.get(currentDisplay).feeValue()))); 
    fields[8].setText(String.valueOf(nf.format(inv.value()))); 
    } 

    public void actionPerformed(ActionEvent e) { 


    } 

回答

1

你需要讓你的GUI類實現ActionListener通過改變

public class GUI extends JFrame 

public class GUI extends JFrame implements ActionListener 
+0

當我剛剛在最後一次谷歌搜索發現。涼!謝謝! – gooddadmike 2009-09-09 00:32:48

+0

或者更好一些,請使用匿名內部類。 – 2009-09-09 09:09:48

1

你只是需要導入的頭文件。 ...

import java.awt.event.ActionListener; 
+0

這是古老的。 – gooddadmike 2013-03-11 15:33:13

相關問題