简体   繁体   English

如何将文本添加到JTextArea? java的

[英]How to add text to JTextArea? java

I have three buttons in in my program and a JTextArea. 我的程序中有三个按钮,还有一个JTextArea。 What I want to do is, when the user presses a button/s I want the JTextArea to have the text saying button 1 was pressed, button 2 was pressed and so on. 我想做的是,当用户按下一个按钮时,我希望JTextArea的文本显示按钮1被按下,按钮2被按下等等。 for example. 例如。

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);

What I want to do is, when the user presses button 1, I want the JTextArea to have the text saying Button 1 was press and then if the user presses button 2, I want the JTextArea to have the previous text and the text for button 2. so it should say something like; 我想要做的是,当用户按下按钮1时,我希望JTextArea包含说按钮1被按下的文本,然后,如果用户按下按钮2,我希望JTextArea具有先前的文本和按钮的文本。 2.所以应该说些类似的话;

button 1 was pressed
button 2 was pressed

edit: 编辑:

so have the text like so, 所以有这样的文字,

button 1 was pressed button 2 was pressed
button 3 was pressed 

and if I had more buttons, it will look like this 如果我有更多按钮,它将看起来像这样

button 1 was pressed button 2 was pressed
button 3 was pressed button 4 was pressed
button 5 was pressed button 6 was pressed

and so on. 等等。

You need to add action listeners to your buttons something like this: 您需要向按钮添加动作监听器,如下所示:

button1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        textArea.append("button 1 was pressed");

    }
});

Dont forget to declare textArea at class level. 不要忘记在类级别声明textArea。

Hope this helps 希望这可以帮助

Add actionListener to each botton which will invoke actionListener添加到将调用的每个botton

yourTextArea.append("button X was pressed\n");

Here is simple demo 这是简单的演示

JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());

final JTextArea area = new JTextArea(2,20);
frame.getContentPane().add(area);

JButton button1 = new JButton("press me");
JButton button2 = new JButton("press me");

button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        area.append("button 1 was pressed\n");
    }
});
button2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        area.append("button 2 was pressed\n");
    }
});

frame.getContentPane().add(button1);
frame.getContentPane().add(button2);

frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

You can also use 您也可以使用

try {
    area.getDocument().insertString(0,"button 1 was pressed\n", null);
} catch (BadLocationException e1) {
    e1.printStackTrace();
}

instead of 代替

yourTextArea.append("button X was pressed\n");

if you want to add new lines at start of text area. 如果要在文本区域的开头添加新行。

Before you add the button to the frame do this: 在将按钮添加到框架之前,请执行以下操作:

button.addActionListener(new ActionListener() { button.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            //Here write what you want to be execute when button is pressed
        }
    });   

For better details about this visit : http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html 有关此访问的更多详细信息,请访问: http : //docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

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

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