ActionListener Java问题
我有一个GUI程序,根据按下的按钮添加一个文本字段。 我在实现我的动作侦听器时遇到问题。无论我在ActionPerformed方法中输入了什么代码,我总是会得到相同的错误。有太多错误,我无法列出它们,但它们似乎表明actionListener没有启动。我不知道我是否没有正确地为按钮添加ActionListener或者其他什么。我在ActionPerformed方法中的代码目前只是一个测试,看看它是否能对我按下的任何按钮起作用,但它仍然不起作用。提前谢谢 *编辑:好的,我更改了ActionPerformed方法中的代码,因此它会根据按下的按钮添加文本区域,我再次得到相同的错误 **编辑:没有更多的错误,只是我的输出有问题。我的回车键和空格键的空格比正常的要大。更改了ActionPerformed方法,因此它现在可以适当地为每个按钮添加附件ActionListener Java问题,java,user-interface,actionlistener,Java,User Interface,Actionlistener,我有一个GUI程序,根据按下的按钮添加一个文本字段。 我在实现我的动作侦听器时遇到问题。无论我在ActionPerformed方法中输入了什么代码,我总是会得到相同的错误。有太多错误,我无法列出它们,但它们似乎表明actionListener没有启动。我不知道我是否没有正确地为按钮添加ActionListener或者其他什么。我在ActionPerformed方法中的代码目前只是一个测试,看看它是否能对我按下的任何按钮起作用,但它仍然不起作用。提前谢谢 *编辑:好的,我更改了ActionPerf
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
/**
* GUI contains a grid of buttons and a text area. As buttons are pressed,
* corresponding text is displayed in the text area.
*
* This class serves as the program driver, the GUI frame, and the
* ActionListener for JButton-generated events.
*
**/
public class TextButtonsHW extends JFrame implements ActionListener {
private JButton[] buttons;// Do not change
private String[] labels;
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[12];
String[] labels = { "A", "B", "C", "1", "2", "3", "X", "Y", "Z",
"ENTER", "SPACE", "CLEAR" };
// sets the value of the ENTER SPACE and CLEAR ints to indicate their
// index in the array
ENTER = 9;
SPACE = 10;
CLEAR = 11;
// TODO: instantiate all JButtons, add them to the buttons array,
// and register "this" as the ActionListener for each button.
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(labels[i]);
buttons[i].addActionListener(this);
}
// TODO: create the JTextArea textArea
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setText("");
// Create a TextButtonsHWPanel to display the buttons and textArea
TextButtonsHWPanel mainPanel = new TextButtonsHWPanel(buttons, textArea);
this.getContentPane().add(mainPanel);
this.pack();
}
/*
* (non-Javadoc)
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO: update the text of textArea according to which
// button generated the ActionEvent.
for (int i = 0; i < buttons.length; i++) {
if (i < ENTER && e.getSource() == buttons[i])
textArea.append(buttons[i].getText());
else if (e.getSource() == buttons[ENTER])
textArea.append("\n");
else if (e.getSource() == buttons[SPACE])
textArea.append(" ");
else if (e.getSource() == buttons[CLEAR])
textArea.setText("");
}
}
/**
* Create this JFrame
*
* @param args
* not used
*/
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);
}
}
您是textArea
变量。替换
JTextArea textArea = new JTextArea();
与
这同样适用于按钮
数组:
JButton[] buttons = new JButton[12];
取代
buttons = new JButton[12];
也发布堆栈跟踪,以便我们可以看到您实际遇到的错误。如果你为TextButtonshPanel发布代码,我们实际上可以运行你的代码,这非常有帮助。我也希望看到你
main()
方法。main()方法在TextButtonshShow代码的底部。好吧,这很有效,谢谢你,我对所有事情都很满意。嗯,这适用于我之前在ActionPerformed中的代码,但我将其更改为根据按下的按钮添加textArea,我仍然得到相同的错误:/问题不在于JTextArea
append
方法。JButton
arraybutton
也有同样的阴影问题。见updateWoop!真是太好了。现在我只是想知道为什么我的换行符和空格在文本字段中这么大。你是说为什么它们没有换行。尝试添加新行字符textArea.append(按钮[i].getText()+“\n”)
textArea = new JTextArea();
JButton[] buttons = new JButton[12];
buttons = new JButton[12];