javaswingjlabellayout-managergridbaglayout

Trouble using JLabel and JPanel


Today is my first day using Swing in Java, and I don't understand why this is happening. The problem is that I'm using GridBagLayout, with two columns, and then in the right panel I'm using two rows, with the upper row being a JLabel. But when I try it, I see that the JLabel doesn't respect the grid size and proportions, and it's not 50/50. I know there are better options to do this, but the point is that I don't need 50/50, I need something like 30/70, but I think that the 50/50 example is better to see the problem.

Here is my (messy) code:

    JFrame ventana = new JFrame("Prueba");
    Toolkit tools = Toolkit.getDefaultToolkit();
    ventana.setLayout(new GridBagLayout());
    
    Dimension dim = tools.getScreenSize();
    
    ventana.setUndecorated(false);
    ventana.setResizable(true);
    ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ventana.setSize(1641, 1027);
    ventana.setVisible(true);
    ventana.setLocation(dim.width/2 - ventana.getWidth()/2 , dim.height/2 - ventana.getHeight()/2);
    
    JPanel left = new JPanel();
    left.setVisible(true);
    left.setBackground(new Color(242, 205, 151));
    
    JPanel right = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    right.setVisible(true);
    right.setBackground(new Color(245, 253, 198));
    
    JLabel label = new JLabel("TEXT");
    label.setFont(customFont.deriveFont(70f));
    c.weighty = 0.4;
    c.insets = new Insets(20, 20, 70, 20);
    c.anchor = GridBagConstraints.PAGE_END;
    right.add(label, c);
    
    JPanel sesion = new JPanel();
    sesion.setVisible(true);
    sesion.setBackground(new Color(174, 182, 112));
    
    c = new GridBagConstraints();
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    c.insets = new Insets(0, 100, 150, 100);
    right.add(sesion, c);
    
    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridwidth = 1;
    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    ventana.add(left, c);
    
    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridwidth = 1;
    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    ventana.add(right, c);

And here is the result:


Solution

  • Lets look at the relationship between the label and the sesion panel...

    JLabel label = new JLabel("TEXT");
    label.setFont(customFont.deriveFont(70f));
    c.weighty = 0.4;
    c.insets = new Insets(20, 20, 70, 20);
    c.anchor = GridBagConstraints.PAGE_END;
    right.add(label, c);
    
    c = new GridBagConstraints();
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    c.insets = new Insets(0, 100, 150, 100);
    right.add(sesion, c);
    

    So, the label is using a weighty of 0.4 and the panel is using weighty of 1.0, which is suggesting that you need 140% of the available space 🤨

    So, what you actually want to do, is balance the weighty value of both components, for example weighty = 0.5, for example...

    JLabel label = new JLabel("TEXT");
    label.setFont(customFont.deriveFont(70f));
    c.weighty = 0.5;
    c.insets = new Insets(20, 20, 70, 20);
    c.anchor = GridBagConstraints.PAGE_END;
    right.add(label, c);
    
    c = new GridBagConstraints();
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = 1;
    c.weighty = 0.5;
    c.fill = GridBagConstraints.BOTH;
    c.insets = new Insets(0, 100, 150, 100);
    right.add(sesion, c);
    

    So, if we modify the existing code, removing the insets, for example...

    JLabel label = new JLabel("TEXT");
    c.weighty = 0.5;
    c.anchor = GridBagConstraints.PAGE_END;
    right.add(label, c);
    
    JPanel sesion = new JPanel();
    sesion.setBackground(new Color(174, 182, 112));
    
    c = new GridBagConstraints();
    c.gridy = 1;
    c.weightx = 1;
    c.weighty = 0.5;
    c.fill = GridBagConstraints.BOTH;
    right.add(sesion, c);
    

    We can end up with a screen looking something more like...

    enter image description here