我的程序中有三个按钮和一个JTextArea。我想要做的是,当用户按下按钮时,我希望JTextArea有文本说按钮1被按下,按钮2被按下等等。例如。如何将文本添加到JTextArea? java
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JTextArea text = new JTextArea();
JFrame frame = new JFrame();
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(text);
frame.setVisible(true);
我想要做的是,当用户按下按钮1,我想JTextArea中有短信说扣1是按,然后如果用户按下按钮2,我想JTextArea中有以前的文本和按钮2的文本。所以它应该说类似的东西;
button 1 was pressed
button 2 was pressed
编辑:
等有文字像这样,
button 1 was pressed button 2 was pressed
button 3 was pressed
,如果我有更多的按钮,它看起来像这样
button 1 was pressed button 2 was pressed
button 3 was pressed button 4 was pressed
button 5 was pressed button 6 was pressed
等。
你看过@文档吗? http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html - 它扩展了JTextComponent(http://docs.oracle.com/javase/7/docs/api/javax/ swing/text/JTextComponent.html),它有一个'setText'方法。 –
使您的文本变量成为实例字段,而不是本地字段,然后在您的ActionListener中使用它。 –