简体   繁体   中英

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:

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 :

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.

Instead if I edit the first class (Gui) removing initComponents(); the JFrame is not shown but this time the JPanel does.

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. 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. Saviour Self got part of the answer as removing the layout of your Gui JFrame makes everything show fine.

I would suggest you use Netbeans as it's GUI editor is a good one. 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). From there, click Add from Project and select your project, that will show the available components that can be added. Select the ones you want.

Inside the GUI editor add a JPanel, and set the property custom creation code to:

new Gui_JTabbedPane()

Now the creation is done in initComponents and finally pack() is called, which does the layouting.


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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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