如果单击两次,如何使 JButton 生成的文本重新出现在同一位置?

How to make the text generated by a JButton reappear at the same place if clicked on twice?

我有一个 JButton,它在点击时显示随机文本,但我的问题是,如果我点击它两次,它会再次显示随机文本,但是当我希望它显示在完全相同的地方而不是第一个(希望我说的很清楚哈哈)

我已经尝试用谷歌搜索问题并在 reddit 和 Whosebug 上搜索,但找不到我要找的东西

按钮代码:

butt = new JButton("Generate");
        butt.setFont(new Font("Tahoma", Font.BOLD, 12));
        butt.setBackground(Color.WHITE);
        butt.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                RandomString yep = new RandomString();
                String c = yep.nextString();
                text = new JTextArea("                                                                          "+c,0,0);
                text.setLineWrap(true);
                text.setBounds(10,10,100,60);
                text.setFont(new Font("Courier", Font.BOLD, 14));

                text.setEditable(false);
                p3.add(text,BorderLayout.CENTER);
                f.remove(p4);
                f.add(p3,BorderLayout.CENTER);
            }
        });

如果这是完美的,它会在第一次点击时显示文本,在第二次点击时它会再次显示文本,但这次恰好是第一个文本所在的位置,就好像它正在擦除并再次显示一样

我真的希望这是可以理解的!感谢您提供的任何帮助,就像对这个程序的最后润色 :)

您每次执行操作都在创建 JTextArea!将 JTextArea 移出 actionPerformed 并设置文本。

JTextArea text = new JTextArea();
JButton button = new JButton("Generate");
....
button.addActionListener(new ActionListener() {
    ....
    String c = yep.nextString();
    text.setText(c);
    ....
}