如何将文本添加到JTextArea

问题描述

我使用Java创建程序。我希望用户输入一些文本,然后按按钮,以便输入的文本显示在标签中。但是,我有两个问题。首先,执行应用程序时不会显示文本。其次,我不知道如何允许用户在该区域中键入。我是Java的新用户,所以这就是我问的原因。这是代码。谢谢。

import javax.swing.*;
import java.awt.event.*;



class Boton extends JFrame implements ActionListener {
    JButton boton;
    JTextArea textArea = new JTextArea();
    JLabel etiqueta = new JLabel();


    public Boton() {
        setLayout(null);
        boton = new JButton("Escribir");
        boton.setBounds(100,150,100,30);
        boton.addActionListener(this);
        add(boton);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == boton) {
            try {
                String texto = textArea.getText();
                etiqueta.setText(texto);
                Thread.sleep(3000);
                System.exit(0);
            } catch (Exception excep) {
                System.exit(0);
            }
        }
    }
}

public class Main{
    public static void main(String[] ar) {
        Boton boton1 =new Boton();
        boton1.setBounds(0,450,350);
        boton1.setVisible(true);
        boton1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)