gpt4 book ai didi

java - Action 监听java

转载 作者:行者123 更新时间:2023-11-29 06:38:34 24 4
gpt4 key购买 nike

对于我的类(class),我需要使用我的 ActionListener。我在将 ActionListener 添加到按钮时遇到问题。我必须像 this.buttons 这样使用“this”这个词来添加我的 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);

关于java - Action 监听java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com