简体   繁体   English

按下按钮后java在jframe中不显示jpanel

[英]java not displaying jpanel in jframe after button press

I am simply making a user interface and all i want it to do after the button is pressed is display thanks... I am pretty new to this but from what i see there are no errors? 我只是在制作一个用户界面,按下按钮后我想要它做的就是显示谢谢...我对此很陌生,但是从我看来没有错误吗? I have tried playing around with the set visible and to no avail...Any help is great thanks 我尝试过在可见的情况下玩耍,但无济于事...任何帮助都非常感谢

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;





public class GuiApp1 {
  public static void main(String args[]) {
    String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Pizza Toppings");
    panel.setBorder(border);
    JLabel label1 = new JLabel("Enter name below:");
    panel.add(label1);
    JTextField field = new JTextField(20);
    panel.add(field);
    JCheckBox check = new JCheckBox("Car0");
    panel.add(check);
    check = new JCheckBox("Car1");
    panel.add(check);
    check = new JCheckBox("Car2");
    panel.add(check);
    check = new JCheckBox("Car3");
    panel.add(check);
    check = new JCheckBox("Car4");
    panel.add(check);
    JButton button = new JButton("Submit");


    final JPanel listPanel = new JPanel();
    listPanel.setVisible(false);
    JLabel listLbl = new JLabel("Vegetables:");

    listPanel.add(listLbl);



    button.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent event)
        {
            listPanel.setVisible(!listPanel.isVisible());
            panel.setVisible(!panel.isVisible());


        }
    });
    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 300);
    frame.setResizable(true);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
  }
}

The reason for the vegetables panel not appearing is simple: Xou never add ist to the contentPane . 蔬菜面板不出现的原因很简单:Xou从未将ist添加到contentPane

For the code to function properly you need to add/remove the panels in the ActionListener of the button: 为了使代码正常运行,您需要在按钮的ActionListener中添加/删除面板:

button.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent event)
    {
        listPanel.setVisible(!listPanel.isVisible());
        panel.setVisible(!panel.isVisible());

        if (listPanel.isVisible()) {
            contentPane.remove(panel); // Vegetables are visible, so remove the Cars
            contentPane.add(listPanel, BorderLayout.CENTER); // And add the Vegetables
        } else {
            contentPane.remove(listPanel); // Vice versa
            contentPane.add(panel, BorderLayout.CENTER);
        }
    }
});

Then, you need to move the ActionListener below the contentPane declaration and make it final . 然后,您需要将ActionListener移到contentPane声明下方,并将其设置为final

Also you should consider putting the different checkboxes is different variables, so you can read the state of them. 另外,您应该考虑将不同的复选框放入不同的变量,以便可以读取它们的状态。 If you don't want to have so many variables hanging you could put them into an array. 如果不想挂那么多变量,可以将它们放入数组中。

JCheckBox[] checks = new JCheckbox[5];
checks[0] = new JCheckBox("Car0");
panel.add(checks[0]);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM