简体   繁体   English

添加的按钮不可见

[英]Added button isn't visible

I try to add an extra panel to already existing one, switchable with button1, but for some reason any added elements (button1) aren't showing up.我尝试在已经存在的面板上添加一个额外的面板,可以使用 button1 进行切换,但由于某种原因,任何添加的元素 (button1) 都没有显示出来。 Here is the fragment of used code:这是使用的代码片段:

    JFrame frame = new JFrame("App");
    frame.setSize(1200, 800);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JPanel panel = new JPanel(new GridBagLayout());

    JPanel mainPanel = new JPanel(new CardLayout());
    JPanel menu = new JPanel(new GridBagLayout());
    mainPanel.add(menu);
    GridBagConstraints e =  new GridBagConstraints();
    e.gridy = 0;
    e.gridx = 0;
    JButton button1 = new JButton("Catalogue");
    menu.add(button1, e);

    frame.add(mainPanel);
    mainPanel.add(panel);

    (...code....)

    frame.setVisible(true);

Add the following line, after creating a frame在创建框架后添加以下行

frame.setVisible(true);

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

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