2011-04-09 110 views
1

CountingNumbers時不是抽象和java.awt.event.ActionListener不重寫抽象方法的actionPerformed(java.awt.event.ActionEvent中)試圖建立一個可點擊的按鈕,但拋出的錯誤編譯

是錯誤我明白了。

我的代碼:

//Create an application that counts the number of each letter in a given word/phrase 
//have a box that has a text box a button and output box 

import javax.swing.*; 

import java.awt.*; 

import java.awt.event.*; 

public class CountingNumbers extends JFrame implements ActionListener 
{ 

    public void count() 

{ 

    JFrame frame = new JFrame("Counting Letters Application"); 
    JTextField textEntry = new JTextField("enter your word/phrase here", 1); 
    JButton count = new JButton("Count Letters"); 
    count.addActionListener(this); 

    frame.setPreferredSize(new Dimension(400, 300)); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Color teal = new Color(0, 128, 128); 
    frame.getContentPane().setBackground(teal); 
    frame.getContentPane().add(textEntry, BorderLayout.NORTH); 
    frame.getContentPane().add(count, BorderLayout.EAST); 

    frame.pack(); 
    frame.setVisible(true); 

    } 

    public void actionPerformed(ActionEvent e, JFrame frame) 
    { 

     JTextField output = new JTextField("output box", 4); 
     frame.getContentPane().add(output, BorderLayout.WEST); 
     output.setText("button was clicked"); 
    } 

}  

我不知道爲什麼會這樣,我是相當新的java和從我校一類剛剛學會。

回答

4

當一個類實現一個接口,它必須爲它們在接口定義實現該接口的方法完全相同。你的類實現了ActionListener,當然,它有一個actionPerformed方法,但方法簽名並不完全匹配接口的方法,因爲這個方法應該只有一個參數,ActionEvent參數,而你的方法有2個,一個ActionEvent參數和一個JFrame參數。由於這改變了方法,編譯器會抱怨你沒有實現接口的所有方法。

幾點建議:

  • 得到您的actionPerformed方法擺脫JFrame的參數(見下文),使您的actionPerformed方法的簽名相匹配的接口。
  • 使您的JFrame變量,框架類字段,以便它在整個類中可見。這樣你的actionPerformed方法不需要參數。
  • 不要在count方法(或類構造函數)中重新聲明JFrame,而是使用class字段(請參見下文)。
  • 沒有類擴展JFrame,因爲它不像JFrame那樣工作。
  • 修復您的代碼格式,特別是通過使您的縮進符合標準。這將使您和我們更容易理解您的代碼並更好地進行調試。
  • 此外,每次按下按鈕時都不要將JTextField添加到gui中,因爲我非常懷疑這是您想要執行的操作...如果多次按下該按鈕,會添加幾個JTextFields。
  • 相反,您需要將輸出JTextField設置爲類字段,以便它在整個應用程序中都可見,您需要將其添加到JFrame或JFrame所持有的容器中您將其他組件添加到GUI。然後,您可以簡單地從actionPerformed方法中設置JTextField的文本,而不是創建一個新字段。

所以不是這個:

public void actionPerformed(ActionEvent e, JFrame frame) 

但這:

public void actionPerformed(ActionEvent e) 

而不是這個:

public class CountingNumbers extends JFrame implements ActionListener { 

    public void count() { 
     JFrame frame = new JFrame("Counting Letters Application"); 
     JTextField textEntry = new JTextField("enter your word/phrase here", 1); 

但這

public class CountingNumbers implements ActionListener { 
    private JFrame frame = new JFrame("Counting Letters Application"); 
    private JTextField outputField = new JTextField(10); 

    public void count() { 
     JTextField textEntry = new JTextField("enter your word/phrase here", 1); 

     // add your outputField to the GUI from somewhere in this method 

稍後我們可以談談爲什麼讓您的GUI類實現一個監聽器界面通常是一個糟糕的主意,但是讓我們不要一次性處理太多的更改和建議。 :)

+0

@Cody:查看有關輸出的建議更改JTextField – 2011-04-09 14:17:55

+1

很好的答案!我也建議使jframe和jtextfields最終。 – extraneon 2011-04-09 14:27:26

+0

@extraneon:謝謝!我第二次提出你的建議。我自己,我會使輸出組件成爲JLabel或不可編輯的JTextField。 – 2011-04-09 14:40:25

1

氣墊船是正確的,但我只是想補充一點,你可能意味着這樣的:

public void actionPerformed(ActionEvent e) 
    { 
     JTextField output = new JTextField("output box", 4); 
     this.getContentPane().add(output, BorderLayout.WEST); 
     output.setText("button was clicked"); 
    } 

因爲你的「CountingNumbers」類擴展JFrame的,它本身實際上是一個JFrame。所以這就是您可以調用「this.getContentPane()」而不是「frame.getContentPane()」的原因。但是,如果你想讓你的「CountingNumbers」對象繼續向自己添加文本字段,情況就是如此。如果你想文本框添加到另一個框架,你就必須到該幀存儲爲CountingNumbers的私有字段(你可以在施工期間初始化):

public class CountingNumbers extends JFrame implements ActionListener 
{ 

    private JFrame frameToAddTextFieldsTo; 

    public CountingNumbers(JFrame frameToAddTextFieldsTo){ 
     this.frameToAddTextFieldsTo = frameToAddTextFiedsTo; 
    } 

    public void count() 

{ 

    JFrame frame = new JFrame("Counting Letters Application"); 
    JTextField textEntry = new JTextField("enter your word/phrase here", 1); 
    JButton count = new JButton("Count Letters"); 
    count.addActionListener(this); 

    frame.setPreferredSize(new Dimension(400, 300)); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Color teal = new Color(0, 128, 128); 
    frame.getContentPane().setBackground(teal); 
    frame.getContentPane().add(textEntry, BorderLayout.NORTH); 
    frame.getContentPane().add(count, BorderLayout.EAST); 

    frame.pack(); 
    frame.setVisible(true); 

    } 

    public void actionPerformed(ActionEvent e) 
    { 

     JTextField output = new JTextField("output box", 4); 
     frameToAddTextFieldsTo.getContentPane().add(output, BorderLayout.WEST); 
     output.setText("button was clicked"); 
    } 

}  

因爲「CountingNumbers」也實現了接口的ActionListener,它也是一個ActionListener,所以這意味着它需要具有與ActionListener接口中定義的方法相同的方法。 (如前所述)

+1

您是否建議原始海報在每次按下按鈕時向某個GUI添加新的JTextField(如上面的代碼所示)?將輸出JTextField作爲類字段並因此在整個類中可見並不是更好嗎,只需在actionPerformed方法內的此字段上調用setText(...)? – 2011-04-09 14:19:15

相關問題