gpt4 book ai didi

java - GridBagLayout 中的 JPanel 不保持比例(weightx)

转载 作者:行者123 更新时间:2023-12-01 10:52:10 24 4
gpt4 key购买 nike

我有以下情况。我使用 GridBagLayout 创建了一个具有以下比例的 JPanel。我想弄清楚这一点,然后我将添加更多组件。现在我有类似的东西

------------------------------------
|LABEL 45% | LABEL 10% | LABEL 45%|
------------------------------------

每个标签水平和垂直各占用 1 个单元格(网格宽度/高度 = 1)

权重 (x) 为 0.45、0.1 和 0.45(从左到右)

权重 (y) 为 0.0

所有标签均已填充 GBC.HORIZONTAL, anchor 为 WES​​T、WEST、EAST。

JPanel topPanel = new JPanel(new GridBagLayout());

topPanel.add(new JLabel("1"), new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
topPanel.add(new JLabel("2"), new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
topPanel.add(new JLabel("3"), new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));

此时一切正常,第一个标签获得与右侧标签完全相同的空间(即可用宽度的 45%,另一个标签获得 10%)。完美。

但是,当我尝试添加另一个带有 GridBagLayout 的 JPanel,而不是标签(左/右)时,比例不会按照定义保持。

在左侧我想添加一个JPanel,其定义如下

----------------------
|JLabel..............|
|--------------------|
|JTextArea...........|
|....................|
----------------------

JLabel 的权重为 0.0(对于 x/y),WEST anchor ,并填充 NONE

JTextArea - 权重 1.0(x)、1.0、WEST anchor ,填充两者(以便拉伸(stretch))

myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE", new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

在右侧,我想添加一个带有 GridBagLayout 的 JPanel,其中包含另外两个带有 GridBagLaouts 的 JPanel(现在没有必要描述如何定义)。

--> 因此,当我添加左侧面板而不是标签时,它会占据屏幕的 60% 左右(这是不正确的,它应该占据 45%)。

你能发现哪里出了问题吗?我没主意了

LE:这是一个简单的例子

public class Example extends JFrame {

public Example() {
init();
}

private JPanel createPanel(Component comp) {
JPanel topPanel = new JPanel(new GridBagLayout());

topPanel.add(comp, new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
topPanel.add(new JLabel("2"), new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
topPanel.add(new JLabel("3"), new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));

return topPanel;
}

private JPanel getPanelWithLabel() {
return createPanel(new JLabel("1"));
}

private JPanel getPanelWithPanel() {
JPanel myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
return createPanel(myPanel);
}

private void init() {
setTitle("Simple example");
setSize(800, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

JPanel panel = new JPanel(new BorderLayout());
panel.add(getPanelWithLabel(), BorderLayout.CENTER);
//panel.add(getPanelWithPanel(), BorderLayout.CENTER);

add(panel);
setVisible(true);
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
Example ex = new Example();
}
});

}
}

最佳答案

问题出在 JTextArea 的行上。

由于 25 行比 45% 宽,因此它会自行扩展。

我不会使用 25,而是使用容器 (myPanel) 的 WIDTH

<小时/>

解决方案

myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
<小时/>

输出

enter image description here

<小时/>

编辑

因为用这种方法尺寸仍然不同。我尝试首先创建一个 JPanel 并向其添加 JLabel

<小时/>

解决方案

JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("3"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
// We add myPanel and two(JLabel) to topPanel first and than the below.
topPanel.add(threeCont, new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
<小时/>

输出

System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());

控制台

First Panel's Size : java.awt.Dimension[width=441,height=96]
Second Panel's Size : java.awt.Dimension[width=98,height=96]
Third Panel's Size : java.awt.Dimension[width=441,height=16]
<小时/>

完整代码

public static void main(String[]a) {
JFrame fr = new JFrame();
fr.setSize(new Dimension(1000,1000));
fr.setLocationRelativeTo(null);
JPanel topPanel = new JPanel(new GridBagLayout());
Insets INSETS_0PX = new Insets(0,0,0,0);
JLabel one = new JLabel("1");
JLabel two = new JLabel("2");
one.setBackground(Color.yellow);
two.setBackground(Color.blue);

JPanel myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));

topPanel.add(myPanel, new GridBagConstraints(
0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));

topPanel.add(two, new GridBagConstraints(
1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

topPanel.add(threeCont, new GridBagConstraints(
2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));


fr.add(topPanel);

fr.setVisible(true);

System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());
}

关于java - GridBagLayout 中的 JPanel 不保持比例(weightx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com