简体   繁体   English

Netbeans - 将JPanel添加到JFrame - 问题

[英]Netbeans - add JPanel to JFrame - issue

I'm using netbeans, I have created a JFrame form with netbeans and it created the following class that I have also edited: 我正在使用netbeans,我用netbeans创建了一个JFrame表单,它创建了我编辑过的以下类:

public class Gui extends javax.swing.JFrame {

    public Gui() {
        initComponents();
        this.setVisible(false);
        this.setLocationRelativeTo(null); // finestra al centro dello schermo
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 804, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 536, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                                        
}

After that I have created a JPanel class that I want to add to previous Gui JFrame : 之后我创建了一个我要添加到以前的Gui JFrameJPanel类:

public class Gui_JTabbedPane extends JPanel {

    public Gui_JTabbedPane() {                      
        super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();

        JComponent panel1 = makeTextPanel("Try");

        tabbedPane.addTab("Try", panel1, "Does nothing");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        add(tabbedPane);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

   protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
}

So in my main class I have done this: 所以在我的主要课程中我做到了这一点:

 Gui_JTabbedPane tabbedpane = new Gui_JTabbedPane();
 Gui gui = new Gui();
 gui.add( tabbedpane );
 gui.setVisible(true);

The problem is that it just shows the JFrame (Gui) without the JPanel ('GuiJTabbedPane`) inside it, as I wanted instead. 问题是它只显示JFrame (Gui)而没有JPanel ('GuiJTabbedPane`),正如我想要的那样。

Instead if I edit the first class (Gui) removing initComponents(); 相反,如果我编辑第一个类(Gui)删除initComponents(); the JFrame is not shown but this time the JPanel does. JFrame没有显示,但这次是JPanel

How can I solve it just using/editing those two classes ? 如何使用/编辑这两个类来解决它?

Thanks 谢谢

You should either use Netbeans to do all of your GUI or do it by hands. 您应该使用Netbeans来完成所有GUI或手动完成。 Mixing both often leads to strange behavior as, by default, Netbeans use GroupLayout and if you don't also use that layout and do groups with your components in the Component made by Netbeans, they might just not show, like you just experienced. 混合两者通常会导致奇怪的行为,因为默认情况下,Netbeans使用GroupLayout ,如果您不使用该布局并使用Netbeans制作的组件中的Component进行组合,则可能不会像您刚刚体验过的那样显示。 Saviour Self got part of the answer as removing the layout of your Gui JFrame makes everything show fine. Savior Self得到了部分答案,因为删除Gui JFrame的布局会让一切都很好。

I would suggest you use Netbeans as it's GUI editor is a good one. 我建议你使用Netbeans,因为它的GUI编辑器很好。 You can do it by yourself too but that needs more testing generally. 你也可以自己做,但一般需要更多的测试。

Edit : To add your own Component to Netbeans palette, you can go under Tools -> Palette -> Swing Components , which will open the palette Editor (using netbeans 7.3). 编辑:要将自己的Component添加到Netbeans调色板,可以在Tools - > Palette - > Swing Components ,打开调色板编辑器(使用netbeans 7.3)。 From there, click Add from Project and select your project, that will show the available components that can be added. 从那里,单击Add from Project并选择您的项目,该项目将显示可添加的可用组件。 Select the ones you want. 选择你想要的。

Inside the GUI editor add a JPanel, and set the property custom creation code to: 在GUI编辑器中添加JPanel,并将属性自定义创建代码设置为:

new Gui_JTabbedPane()

Now the creation is done in initComponents and finally pack() is called, which does the layouting. 现在创建在initComponents完成,最后调用pack()进行布局。


One remark, calling in the constructor an overridable (non-final, non-private) method, makeTextPanel is bad style, as it behaves unexpectedly: why aren't fields initialized . 一句话,在构造函数中调用一个可覆盖的(非最终的,非私有的)方法, makeTextPanel是坏样式,因为它出乎意料地行为: 为什么不初始化字段

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

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