【问题标题】:How can I display the text in the text field in the text area when I clicked the button?单击按钮时,如何在文本区域的文本字段中显示文本?
【发布时间】:2019-04-24 19:47:28
【问题描述】:

这是我迄今为止尝试过的:

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

    }

【问题讨论】:

标签: java swing user-interface


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2020-08-16
    • 2016-10-06
    相关资源
    最近更新 更多