繁体   English   中英

为什么将我的组件添加到面板后消失了?

[英]Why do my components disappear when added to a panel?

所以我有一个jFrame对象,该对象又附加了3个JPanels。 这些JPanel放置在NORTH,CENTER,SOUTH上。 我在CENTER和SOUTH中添加了一些按钮和标签。

我在NORTH和CENTER上添加了一些字段集,以使其外观更好。 现在,当我尝试运行该程序时,它什么也没有显示。 我的按钮和标签消失了,我的字段集也找不到了。 我究竟做错了什么?

谢谢您的时间。

public General() {
    super("OmniTool");
    initComponents();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(300,100);
    setVisible(true);
    setResizable(true);
    setSize(900, 700);

    //radiobuttons ivm dependencies
    ButtonGroup dependenciesInfo = new ButtonGroup();
    dependenciesInfo.add(rdbYesDependencies);
    dependenciesInfo.add(rdbNoDependencies);
    //boolean om te controleren of alle textareas al ingevuld zijn 
    generalInfoComplete = false;

    //creating 3 panels and add them on the same frame + giving them their location 
    JPanel oudeMod = new JPanel();
    JPanel nieuweMod = new JPanel();
    JPanel generate = new JPanel();
    this.add(oudeMod,BorderLayout.NORTH);
    this.add(nieuweMod,BorderLayout.CENTER);
    this.add(generate,BorderLayout.SOUTH);
    oudeMod.setBorder(BorderFactory.createTitledBorder("Use exisiting modfolder"));
    nieuweMod.setBorder(BorderFactory.createTitledBorder("Create new modfolder"));

    //adding buttons and textfields to proper panels

    nieuweMod.add(jLabel2);
    nieuweMod.add(jLabel3);
    nieuweMod.add(jLabel4);
    nieuweMod.add(jLabel5);
    nieuweMod.add(jLabel7);
    nieuweMod.add(jLabel8);
    nieuweMod.add(jLabel9);
    nieuweMod.add(jLabel10);
    nieuweMod.add(btnBrowseMain);
    nieuweMod.add(btnBrowseMods);
    nieuweMod.add(btnDependencies);
    nieuweMod.add(rdbNoDependencies);
    nieuweMod.add(rdbYesDependencies);
    nieuweMod.add(txtDependencies);
    nieuweMod.add(txtMainDir);
    nieuweMod.add(txtModName);
    nieuweMod.add(txtModsDir);
    generate.add(btnGenerate);
}

编辑1:这些jFrame在1 jtabbedPane上。

如果“ this”是JFrame,则this.add(...)将不起作用。 您应该将组件添加到JFrame的contentPane中。

“所以我有一个jFrame对象,该对象又附加了3个JPanels。这些JPanels放在NORTH,CENTER,SOUTH上。我在CENTER和SOUTH中添加了一些按钮和标签。”

每个BorderLayout位置只能容纳一个组件。 如果将JPanel添加到CENTER则将JButton添加到CENTER仅显示最后添加的组件( JButton )。

将所需的任何组件添加到最多5个JPanel并将这些JPanel放置在不同的BorderLayout位置。 或者,您可以使用其他布局。 或者,您可以使用主JPanel容器,只需将JPanel添加到框架中。 您有很多选择。


添加所有组件后,还要执行所有JFrame内务管理。 将它们放在构造函数的末尾。

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(300,100);
setVisible(true);
setResizable(true);
setSize(900, 700);

也不要设置大小。 只是pack() setVisible()应该是您要做的最后一件事。

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);

暂无
暂无

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

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