简体   繁体   English

如何在java中将文本添加到TextArea

[英]How to add text to a TextArea in java

I have this code:我有这个代码:

 package com.butoane; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class App { private JButton apasaAiciButton; private JPanel Dunno; private JButton apoiaici; private JTextArea text1; public App() { String a = "H"; String b = "e"; apasaAiciButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { apoiaici.setEnabled(true); apasaAiciButton.setEnabled(false); text1.setText(toString(a)); } }); apoiaici.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { apoiaici.setEnabled(false); apasaAiciButton.setEnabled(true); apasaAiciButton.setText("Apasa iar aici"); text1.setText(toString(b)); } }); } public static void main(String[] args) { JFrame frame = new JFrame("App"); frame.setContentPane(new App().Dunno); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }

What I want to do is, when one button is pressed, the text field gets either H or E, like : Button1(apasaAici) gets pressed, the text gets H Button2(apoiAici) gets pressed, the text area gets an E after the "H", and, doing this over and over, the textarea to get every time an HeHeHeHe , you get it ?我想要做的是,当按下一个按钮时,文本字段会变成 H 或 E,例如:Button1(apasaAici) 被按下,文本被按下 Button2(apoiAici) 被按下,文本区域在“H”,而且,一遍又一遍地这样做,每次HeHeHeHe都会得到 textarea ,你明白了吗?

How can I make this possible ?我怎样才能做到这一点?

JTextArea.append(String) JTextArea.append(String)

For your "H" button:对于您的“H”按钮:

text1.append(a);

For your "e" button:对于您的“e”按钮:

text1.append(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM