2013-04-22 98 views
1

對於我的課,我需要使用我的ActionListener。我無法將我的ActionListener添加到我的按鈕。我必須使用「this」這個詞,像this.buttons來添加我的ActionListener,但是Im很難做到這一點。稍後我會到達actionPerformed方法,但我的主要問題是我的ActionListener。Actionlistener java

public class TextButtonsHW extends JFrame implements ActionListener { 
private JButton[] buttons; //Do not change 
private JTextArea textArea; //Do not change 
//Assign values for these constants in the constructor 
private final int ENTER; //Index of Enter button in buttons 
private final int SPACE; //Index of Space button in buttons 
private final int CLEAR; //Index of Clear button in buttons 

/** 
* Set up this frame and its contents. 
* @param title Title to appear in JFrame title bar 
*/ 
public TextButtonsHW(String title) { 

    super(title); //call parent JFrame constructor to set the title 

    buttons = new JButton[] { new JButton("A"), new JButton("B"), new JButton("C"), 
        new JButton("1"), new JButton("2"), new JButton("3"), 
        new JButton("X"), new JButton("Y"), new JButton("Z"), 
        new JButton("Enter"), new JButton("Space"), new JButton("Clear")}; 

    ENTER = buttons.length-3; 
    SPACE = buttons.length-2; 
    CLEAR = buttons.length-1; 

    textArea = new JTextArea(15, 5); 
    textArea.setEditable(false); 

    this.buttons[0].addActionListener(I dont know what to put right here); 

    //TODO: instantiate all JButtons, add them to the buttons array, 
    // and register "this" as the ActionListener for each button. 
    //DONE 
    //TODO: assign values to ENTER, SPACE, and CLEAR constants to 
    // indicate the indexes of those buttons in the buttons array 
    //DONE 
    //TODO: create the JTextArea textArea 
    //TODO: set its "editable" property to false 
    //DONE 
    //Create a TextButtonsHWPanel to display the buttons and textArea 

    TextButtonsHWPanel mainPanel = new TextButtonsHWPanel(buttons, textArea); 
    this.getContentPane().add(mainPanel); 
    this.pack(); 
} 


public void actionPerformed(ActionEvent e) { 


    //TODO: update the text of textArea according to which 
    // button generated the ActionEvent. 

} 


public static void main(String[] args) { 
    final TextButtonsHW f = new TextButtonsHW("Text Buttons"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setLocationRelativeTo(null); //centers frame on screen 
    f.setVisible(true); 
} 

}

+0

如果你輸入ENTER會更好。 SPACE,CLEAR類型枚舉而不是int。 – Abraham 2013-04-22 04:12:22

+0

我看到它可能是如何,但我的任務想要它那樣 – Chris 2013-04-22 04:13:58

回答

5
buttons[0].addActionListener(this); 
+0

啊謝謝你!你真棒! – Chris 2013-04-22 04:11:26

+0

記住addActionListener的參數是一個實現了actionPerformed方法的類,在這種情況下,該方法是定義addActionListner並因此定義「this」的相同類。 – Abraham 2013-04-22 04:14:52

2

既然你有12個按鈕,你也可以去一個for循環來的ActionListener添加到您的所有按鈕,即

for(int i=0; i<12; i++) 
{ 
    buttons[i].addActionListener(this); 
} 

取而代之的是長碼即

buttons[0].addActionListener(this); 
buttons[1].addActionListener(this); 
........................ 
0

雖然buttons[0].addActionListener(this);將工作得很好,從設計的角度來看,我會建議有單獨的類(可能是內部類),它實現了ActionListner並將其對象引用傳遞給該方法。

面向對象編程的重點是將不同的職責委託給不同的對象(類),因此代碼變得更易維護和可重用。