使用 JLabel 和 JPanel 时遇到问题

Trouble using JLabel and JPanel

今天是我在 Java 中使用 Swing 的第一天,我不明白为什么会这样。问题是我使用的是 GridBagLayout,有两列,然后在右侧面板中我使用了两行,上面一行是 JLabel。但是当我尝试它时,我发现 JLabel 不遵守网格大小和比例,而且它不是 50/50。我知道有更好的选择来做到这一点,但关键是我不需要 50/50,我需要 30/70 之类的东西,但我认为 50/50 的例子更能看出问题。

这是我的(凌乱的)代码:

    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);

结果如下:

让我们看看标签和 sesion 面板之间的关系...

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);

因此,标签使用 0.4weighty,面板使用 1.0weighty,这表明您需要 140%可用 space

所以,您真正想要做的是平衡两个组件的 weighty 值,例如 weighty = 0.5,例如...

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);

因此,如果我们修改现有代码,删除 insets,例如...

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);

我们最终可以得到一个看起来更像...

的屏幕