简体   繁体   中英

Java Swing add JPanel to JLayeredpane to Jframe dynamically change size

I am trying to get my JFrame and the content inside it to change size and position automatically when the window is re-sized etc. I have managed to do this successfully when using a JPanel inside a JFrame however when I add a JLayeredPane in-between these two It stops working.

The code for the working JPanel and JFrame is below, when the screen is re-sized the panels dynamically change size and position on the screen.

JFrame UIFrame = new JFrame("Project");
UIFrame.setSize(1100, 768);
UIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
BorderLayout mainPanelLayout = new BorderLayout();
mainPanel.setLayout(mainPanelLayout);
Footer UIFooter = new Footer(); // Creates the footer
mainPanel.add("South", UIFooter.getPanel()); 
addTabs(); // Adds all the tabs to the screen
mainPanel.add("Center", Pane); 
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
UIFrame.setLocation(dim.width/2-UIFrame.getSize().width/2, dim.height/2-UIFrame.getSize().height/2);
UIFrame.setContentPane(mainPanel); // Content placed on the frame
UIFrame.setVisible(true); // Frame set to visible

However when I add in my JLayeredPane the screen no longer re-sizes dynamically. I think this is down to the setBounds method however without this method nothing is displayed on screen.

JFrame UIFrame = new JFrame("Project");
UIFrame.setSize(1100, 768);
UIFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
BorderLayout mainPanelLayout = new BorderLayout();
mainPanel.setLayout(mainPanelLayout);
Footer UIFooter = new Footer(); 
mainPanel.add("South", UIFooter.getPanel()); 
addTabs();
mainPanel.add("Center", Pane); 
mainPanel.setBounds(0, 0, 1085, 725);
JLayeredPane desktop = new JDesktopPane();
desktop.setOpaque(false);
desktop.add(mainPanel, JLayeredPane.DEFAULT_LAYER);
UIFrame.add(desktop);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
UIFrame.setLocation(dim.width/2-UIFrame.getSize().width/2, dim.height/2-UIFrame.getSize().height/2);
UIFrame.setContentPane(desktop); // Content placed on the frame
UIFrame.setVisible(true); // Frame set to visible

I believe when you use setBounds you are permanently fixing this panel to that size, so there will be no dynamic resizing.

I personally tend to use GridBagLayout most of the time myself, perhaps you could try that? The settings in the gridbag constraints that matter to you would be the 'fill', 'weightx' and 'weighty'.

At it's basics, this bit of code should put a JLayeredPane within an already created JFrame, and a JPanel within the JLayeredPane and it will all resize with the frame:

JLayeredPane jLayeredPane1 = new JLayeredPane();
JPanel jPanel1 = new JPanel();
GridBagConstraints gBC = new GridBagConstraints();

getContentPane().setLayout(new GridBagLayout());
jLayeredPanel1.setLayout(new GridBagLayout());

gBC.fill = GridBagConstraints.BOTH;
gBC.weightx = 1.0;
gBC.weighty = 1.0;

getContentPane().add(jLayeredPane1, gBC);
jLayeredPane1().add(jPanel1, gBC);

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