单击按钮后,如何在文本区域的文本字段中显示文本?

时间:2019-04-24 19:47:28

标签: java swing user-interface

这是我到目前为止尝试过的:

{
    TextField tf;
    JTextArea ta; 

    public TextListener()
    {
        gui();
    }

    public void gui()
    {
        JFrame f = new JFrame();
        JPanel p = new JPanel();
        tf = new JTextField("",20);
        ta = new JTextArea("",20,20);
        JButton b = new JButton("SHOW"); 

        f.add(p);
        p.add(tf);
        p.add(b);
        p.add(ta);

        f.setVisible(true);
        f.setSize(400,200);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent ae)
    {
        String name = tf.getText();
        ta.append(name);
    }

    public static void main(String[] arg)
    {
        new TextListner();

    }

1 个答案:

答案 0 :(得分:0)

阅读How to Use Text Areas的Swing教程中的部分,以获取工作示例。

本教程中的示例将ActionListener添加到文本字段。因此,您只需使用“ Enter”键将文本添加到文本区域即可。

不过,您可以简单地修改代码以使用JButton并将ActionListener添加到按钮以及文本字段中。

相关问题