单击按钮后显示文本框-java

单击按钮后显示文本框-java,java,swing,jlabel,jtextfield,Java,Swing,Jlabel,Jtextfield,我是Java新手,我正在做一个飞机座位预定计划。我想知道如何在单击JFrame窗口中已经显示的按钮后显示JTextfield文本框和JLabel。我尝试在if语句后添加按钮,但它不起作用。我也尝试过button.setVisible(false),然后在以后将其设置为true。我有一个按钮。actionlistener(这个)用于每个按钮 public void actionPerformed(ActionEvent event){ Container contentPane = nu

我是Java新手,我正在做一个飞机座位预定计划。我想知道如何在单击JFrame窗口中已经显示的按钮后显示JTextfield文本框和JLabel。我尝试在if语句后添加按钮,但它不起作用。我也尝试过button.setVisible(false),然后在以后将其设置为true。我有一个按钮。actionlistener(这个)用于每个按钮

 public void actionPerformed(ActionEvent event){
    Container contentPane = null;
    JButton clickedButton = (JButton) event.getSource();
        if (event.getSource() == coach){
            five.setForeground(Color.green);
            eleven.setForeground(Color.green);
            six.setForeground(Color.green);
            two.setForeground(Color.black);
                if (event.getSource() == five) {
                    inputLine = new JTextField();
                    inputLine.setBounds(110, 180, 185, 22);
                    contentPane.add(inputLine);
                }

        } else if (event.getSource() == firstClass){
            two.setForeground(Color.green);
            five.setForeground(Color.black);
            eleven.setForeground(Color.black);
            six.setForeground(Color.black);

        }

我不完全清楚您的信息,您是想在“已创建”按钮上显示已创建的文本字段和标签,还是在“已创建”按钮上创建相同的文本字段和标签

但我有一些代码给你,看看是否有用

JTextField[] desc=new JTextField[20];
 JButton more=new JButton("More");
    Container c=getContentPane();
    public void actionPerformed(ActionEvent a1)
    {
    String s1=a1.getActionCommand();
    String s="";
           for(i=0;i<cnt;i++)
        s=s+desc[i].getText();

     if(s1.equals("More"))
    {
                 System.out.println("Created");
                 desc[i]=new JTextField();
                 desc[i].setBounds(50,y,150,30);
                 c.add(desc[i]);
    }
    }
JTextField[]desc=新的JTextField[20];
JButton more=新JButton(“more”);
容器c=getContentPane();
已执行的公共无效操作(操作事件a1)
{
字符串s1=a1.getActionCommand();
字符串s=“”;

对于(i=0;iAn实际上会涉及更少的猜测工作和更好的响应。首先,尝试将它们添加到您想要添加它们的容器中。您可能需要调用
revalidate
,以便让UI在一个空间中更新多个组件,请使用如图所示的。在这种情况下,我们所说的“多”是指默认的空白面板,而e第二个面板包含标签和文本字段。将两者添加到卡布局,并在需要时翻转到带有输入控件的面板。