[英]Actionlistener java
For my class I need to use my ActionListener. 对于我的班级,我需要使用ActionListener。 I am having trouble adding my ActionListener to my buttons. 我在将ActionListener添加到按钮时遇到麻烦。 I have to use the word "this" like this.buttons to add my ActionListener but Im having trouble doing that. 我必须像this.buttons这样使用“ this”一词来添加我的ActionListener,但是Im很难做到这一点。 I will get to my actionPerformed method later but my main problem is my ActionListener. 稍后将介绍我的actionPerformed方法,但是我的主要问题是我的ActionListener。
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);
}
} }
buttons[0].addActionListener(this);
Since you have 12 buttons you could also go for a for-loop to add ActionListener to all your buttons ie 由于您有12个按钮,因此您还可以进行for循环以将ActionListener添加到所有按钮,即
for(int i=0; i<12; i++)
{
buttons[i].addActionListener(this);
}
Instead of a long code ie 代替长代码,即
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
........................
Though buttons[0].addActionListener(this);
虽然buttons[0].addActionListener(this);
will work well, From design perspective I will suggest to have separate class (may be inner class) which implements ActionListner
and pass its object reference to the method. 从设计的角度来看,我会工作的很好。我建议有一个单独的类(可能是内部类),该类实现ActionListner
并将其对象引用传递给该方法。
the whole point of Object Oriented programming is to delegate different responsibilities to different Objects(classes), so code becomes more maintainable and reusable. 面向对象编程的重点是将不同的职责委派给不同的对象(类),因此代码变得更加可维护和可重用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.