Jpanel doesn't display well with jframe installed in gridbaglayout

the program below is to position the jpanel in the top left jframe connector using gridbaglayout, but instead a very small frame is displayed in the center of the jframe. when I set the jframe format to null, the jpanel is displayed in order. can someone tell me why jpanel is compressed to the center of the frame using gridbaglayout? I really need to use a gridbag. please, help

import java.awt.*; import javax.swing.*; //swing package public class Major { //defining the constructor public Major() { JFrame maFrame = new JFrame("The main screen"); //creating main Jframe JPanel headPanel = new JPanel(); //creating the header panel maFrame.setSize(900, 700); //setting size maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame Container container = maFrame.getContentPane(); container.setLayout(new GridBagLayout()); //setting layout of main frame GridBagConstraints cns = new GridBagConstraints(); //creating constraint cns.gridx = 0; cns.gridy = 0; maFrame.setLocationRelativeTo(null); //centering frame headPanel.setBackground(Color.WHITE); headPanel.setSize(200, 150); container.add(headPanel, cns); maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame maFrame.setVisible(true); //making the frame visible } //defining the main method public static void main(String[] args) { new Major(); //instantiating the class } } 
+4
source share
2 answers

It looks like you forgot to specify weightx and weighty for your GridBagConstraints . When you provide them, you will see your JPanel.

Here I changed the code with these restrictions.

And never use this line, headPanel.setSize(200, 150); as I commented on this since the restrictions that I mentioned will be sorted for you.

Adding a new image code:

  import java.awt.*; import javax.swing.*; //swing package public class Major { //defining the constructor public Major() { JFrame maFrame = new JFrame("The main screen"); //creating main Jframe JPanel headPanel = new JPanel(); //creating the header panel maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame Container container = maFrame.getContentPane(); container.setLayout(new GridBagLayout()); //setting layout of main frame GridBagConstraints cns = new GridBagConstraints(); //creating constraint cns.gridx = 0; cns.gridy = 0; //cns.gridwidth = 3; //cns.gridheight = 4; cns.weightx = 0.3; cns.weighty = 0.7; cns.anchor = GridBagConstraints.FIRST_LINE_START; cns.fill = GridBagConstraints.BOTH; maFrame.setLocationRelativeTo(null); //centering frame headPanel.setBackground(Color.WHITE); container.add(headPanel, cns); JPanel panel = new JPanel(); panel.setBackground(Color.BLUE); cns.gridx = 1; cns.gridy = 0; //cns.gridwidth = 7; //cns.gridheight = 4; cns.weightx = 0.7; cns.weighty = 0.7; cns.anchor = GridBagConstraints.PAGE_START; cns.fill = GridBagConstraints.BOTH; container.add(panel, cns); JPanel panel1 = new JPanel(); panel1.setBackground(Color.DARK_GRAY); cns.gridx = 0; cns.gridy = 1; cns.gridwidth = 2; //cns.gridheight = 4; cns.weightx = 1.0; cns.weighty = 0.3; cns.anchor = GridBagConstraints.LAST_LINE_START; cns.fill = GridBagConstraints.BOTH; container.add(panel1, cns); //JButton button = new JButton("BUTTON"); //headPanel.add(button); maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame maFrame.pack(); maFrame.setVisible(true); //making the frame visible } //defining the main method public static void main(String[] args) { Runnable runnable = new Runnable() { public void run() { new Major(); //instantiating the class } }; SwingUtilities.invokeLater(runnable); } } 

here is the result:

GRIDBAGLAYOUT

+6
source

You must set weightx and weighty at least one GridBagConstraint object for some value greater than 0.0!

Weight attributes are used to indicate what happens with extra space if the entire layout is less than the available space. If all weights (for one direction) are zero, the default value, the entire layout is centered. If at least one weight is greater than zero, the additional space is distributed in columns or rows in proportion to its weight, so the layout will occupy all the free space.

+4
source

Source: https://habr.com/ru/post/1400713/


All Articles