简体   繁体   中英

how do you customise position of a JPanel within a Jpanel

The class extends JPanel,

 public void createDisplay(){
    frame = new JFrame();
    frame.setTitle(title);
    frame.setSize(new Dimension(width, height));
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setPreferredSize(new Dimension(width, height));
    this.setMaximumSize(new Dimension(width, height));
    this.setMinimumSize(new Dimension(width, height));

    this.setLayout(null); //have tried default and BorderLayout
    this.setSize(new Dimension(width, height));
    this.setBounds(0, 0, width, height);

    //basically trying everything

    frame.add(this);
    frame.pack();

}

on startup this code works fine and the JPanel completely covers the size of the Parent frame

However my program later tries to add a new JPanel to the class's extend JPanel with:

    public void gameOverWindow(){ ;
    JPanel panel = new JPanel(new BorderLayout());
    panel.setPreferredSize(new Dimension(200, 100));
    JLabel label = new JLabel("Game Over");
    label.setPreferredSize(new Dimension(40, 15));

   //trying setPosition also doesn't work with BorderLayout or FlowLayout

    JButton button_01 = new JButton("new");
    button_01.setPreferredSize(new Dimension(100, 10));
    JButton button_02 = new JButton("exit");
    button_02.setPreferredSize(new Dimension(100, 10));


    panel.add(label, BorderLayout.NORTH);
    panel.add(button_01, BorderLayout.WEST);
    panel.add(button_02, BorderLayout.EAST);



    this.add(panel);
    this.revalidate();


}

This new JPanel appears with the contents within the correct BorderLayout format, however the JPanel itself will remain at the top center of the extended JPanel, I know this is because the default Layout is set to FlowLayout, however setting this to BorderLayout will just cause the panel to take up the entire screen. Setting the Layout to null completely breaks the frame and nothing appears but the Minimize and Close buttons of the Frame. Trying to set the position or Bounds of this JPanel doesn't work with any Layout either. I have read a lot of other post online about this but they all seem to differ and become confusing, how do I gain control of the position of my new JPanel?

Normally I'd recommend using a CardLayout for switching between different views, but it's difficult to ascertain from the available information if that would help or not

Instead, you could make use of compounding layouts. That is wrap one container in another using different layouts.

In this example, I simply use a combination of BorderLayout and GridBagLayout

游戏结束 游戏真的超过了男人

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JButton gameOver = new JButton("Game over man");
            gameOver.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel(new BorderLayout());
                    panel.add(new JLabel("Game Over Man", JLabel.CENTER), BorderLayout.NORTH);
                    panel.add(new JButton("New"), BorderLayout.WEST);
                    panel.add(new JButton("Exit"), BorderLayout.EAST);

                    removeAll();
                    JPanel inner = new JPanel(new GridBagLayout());
                    inner.add(panel);
                    add(inner);
                    revalidate();
                    repaint();
                }
            });
            JPanel inner = new JPanel(new GridBagLayout());
            inner.add(gameOver);
            add(inner);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

what is the purpose of removing the components of a different panel instead of just directly adding it to GridBagLayout?

Because they interfere with the layout of other components.

i then want a small Jpanel to popup within and be unobtrusive

You could make use of the frame's glassPane or use a OverlayLayout

For example:

Much of this information should have been in your original question, it would have wasted less of each other's time

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