简体   繁体   中英

Place JPanel inside of a JFrame

I have been at this for hours now, and I am stumped. I have this code, which adds a JPanel within a JFrame and the issue is when I have the Frame set to Free Design It doesn't show up in the Frame. When I set the Frame to Border Layout the code works fine. What I would like to do is get this to work with Free Design where I can place and resize the JPanel as I please. Here is the following code that places it in the Frame.

        canvas = new Canvas();
        canvas.setVisible(true);
        canvas.setImage(file);
        GroupLayout canvasLayout = new GroupLayout(canvas);
        canvas.setLayout(canvasLayout);
        canvas.setPreferredSize(new Dimension(100, 100));
        //canvas.setSize(200,200);
        //canvas.setPreferredSize(null);
        add(canvas);
        revalidate();

When I use free design mode, what am I missing? Here is the code that builds the layout:

private void initComponents() {

    jInternalFrame1 = new javax.swing.JInternalFrame();

    jInternalFrame1.setVisible(true);

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

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("PocketShop");
    setName("main");

    pack();
}

Do away with JInternalFrame

JInternalFrame is for children inside a JDesktopPane. A kind windows in windows, the so called MDI, multi-document-interface, as opposed to SDI, single-document interface.

Use JPanel instead of Canvas

Canvas is of the old native platform AWT, a Component, JPanel of Swing, a JComponent. You can there override paintComponent.

For the Free Design layout, I would use a GUI editor as in the NetBeans IDE. I do, and so cannot help you further there.

Do not use revalidate if possible.


If you manually add the Canvas, try adding a JPanel, and set as custom creation code new Canvas() or canvas .

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