单击按钮后如何使 JTextArea 出现

How can I make a JTextArea appear after clicking button

我正在做一个简单的程序,在单击 JButton 后在 JTextArea 中显示有关特定大学的信息。

如何让点击按钮后出现 JTextArea?

这是我的代码:

package toolBar;
import javax.swing.*;
import java.awt.*;


public class ToolBar extends JFrame {
    JFrame frame=new JFrame();
    JButton uni1=new JButton("Hasheimte University");
    JButton uni2=new JButton("The University of Jordan");
    JButton uni3=new JButton("German Jordanian University");
    JButton exit=new JButton("Close");
    JToolBar tb = new JToolBar();
    JTextArea text = new JTextArea("bla bla bla");
    
    
    public ToolBar(){
        setTitle("Jordanian universities");
        setSize(600,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(tb);
        tb.add(uni1);
        tb.add(uni2);
        tb.add(uni3);
        tb.add(exit);
        
        tb.setFloatable(false);
        setLayout(new FlowLayout (FlowLayout.CENTER));
        
        uni1.addActionListener(e ->{
            
        });
        
        exit.addActionListener(e -> {
            dispose();
        });
    }
}

所有 Swing 组件都有一个 setVisible 方法供您使用。您从一个不可见的组件开始,然后在单击按钮时使其可见。

另一种方法是通过单击按钮将其添加到父容器 (Container#add),但这需要重新验证布局。因此,第一个选项更容易。