我的程序中有三個按鈕和一個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中使用它。 –