Actionlistener java
对于我的类,我需要使用ActionListener。将ActionListener添加到按钮时遇到问题。我必须像这样使用“this”这个词。按钮来添加我的ActionListener,但我在这样做时遇到了麻烦。稍后我将使用actionPerformed方法,但我的主要问题是ActionListenerActionlistener java,java,actionlistener,Java,Actionlistener,对于我的类,我需要使用ActionListener。将ActionListener添加到按钮时遇到问题。我必须像这样使用“this”这个词。按钮来添加我的ActionListener,但我在这样做时遇到了麻烦。稍后我将使用actionPerformed方法,但我的主要问题是ActionListener public class TextButtonsHW extends JFrame implements ActionListener { private JButton[] buttons;
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);
}
}由于您有12个按钮,您还可以使用for循环将ActionListener添加到所有按钮,即
for(int i=0; i<12; i++)
{
buttons[i].addActionListener(this);
}
由于您有12个按钮,您还可以使用for循环将ActionListener添加到所有按钮中,即
for(int i=0; i<12; i++)
{
buttons[i].addActionListener(this);
}
通过
按钮[0].addActionListener(此)
将很好地工作,从设计角度来看,我建议使用单独的类(可能是内部类),实现ActionListner
,并将其对象引用传递给方法
面向对象编程的全部要点是将不同的职责委托给不同的对象(类),因此代码变得更易于维护和重用。尽管
按钮[0]。addActionListener(此)
将很好地工作,从设计角度来看,我建议使用单独的类(可能是内部类),实现ActionListner
,并将其对象引用传递给方法
面向对象编程的全部要点是将不同的职责委托给不同的对象(类),因此代码变得更易于维护和重用。如果您选择ENTER,效果会更好。空格,清除类型enum而不是int。我知道它可能是什么,但我的作业希望它是这样的。如果您输入ENTER,效果会更好。空格,清除类型enum,而不是int。我知道它可能是什么,但我的赋值希望它是这样的记住addActionListener的参数是一个实现actionPerformed方法的类,在本例中,它是定义addActionListner的同一个类,因此是“this”。请记住,addActionListener的参数是一个实现actionPerformed方法的类,在本例中,该方法与定义addActionListner的类相同,因此定义了“this”。
buttons[0].addActionListener(this);
buttons[1].addActionListener(this);
........................