Java Swing组件

huangapple go评论67阅读模式
英文:

Java Swing Component

问题

我对Java Swing和布局还比较新。我想要创建一个类似这样的组件:

Comp 3的高度跨越1和2。所以,我想我将不得不使用覆盖布局来将comp 3定位在1和2的后面。我希望它们以相同的比例调整大小(可以任意决定)。我该如何实现这个?

英文:

I am quite new to Java Swing and layouts. I want to create a component like this:
Java Swing组件

Comp 3 spans the height of 1 and 2. So, I imagine I will have to use overlay layout to position comp 3 behind 1 and 2. I want them to resize in these same proportions (they can be arbitrarily decided). How can I achieve this?

答案1

得分: 1

您可以使用一个或多个GridBagLayout布局管理器的组合。GridBagLayout允许您以网格方式放置元素,通过使用GridBagConstraints类来指定比例和位置。

public void setupLayout() {
    JPanel container = new JPanel(); // 元素的主要容器
    // 根据您的屏幕截图,创建编号面板
    JPanel panel1 = new JPanel(), panel2 = new JPanel(),
            panel3 = new JPanel(), panel4 = new JPanel();
    panel1.setBackground(Color.RED); // 为了更好的可视化,设置不同的颜色
    panel2.setBackground(Color.BLUE);
    panel3.setBackground(Color.YELLOW);
    panel4.setBackground(Color.GREEN);

    container.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridwidth = gbc.gridheight = 1;
    gbc.weightx = 1;
    gbc.gridx = 0; gbc.gridy = 0; gbc.weighty = 0.2;
    container.add(panel1, gbc);

    gbc.gridy = 1; gbc.weighty = 0.3;
    container.add(panel2, gbc);

    gbc.gridy = 2; gbc.weighty = 0.5;
    container.add(panel3, gbc);

    gbc.gridx = 1; gbc.gridy = 0; gbc.gridheight = 3;
    container.add(panel4, gbc);

    // 放置容器在框架或其他用途上...
}

属性gridxgridy指定了您在网格上的位置。weightxweighty指定了它们占用的空间比例(从0到1)。

gridwidthgridheight不同,它们指定了一个单元格相对于其他单元格占用多少空间。一个单元格将占用与gridheight单元格在垂直方向上占用的空间或gridwidth单元格在水平方向上占用的空间相等的空间。因此,以实际示例来解释,由于panel1设置为weighty = 0.2panel2设置为weighty = 0.3,而panel3设置为weighty = 0.5,我们表示无论哪个单元格在第一行,都将始终占用垂直空间的0.2,第二行占用垂直空间的0.3,第三行占用一半(0.5)的垂直空间,因此,panel4将占用与之前的单元格高度总和相等的空间。如果panel4设置为gridheight = 2,它将占用两行的空间,因为在这个示例中,它放置在第一行,所以它将占用等于第一行和第二行之和的空间,即一半的垂直空间(0.2 + 0.3),但由于我们将其设置为3,这也是这个示例中的最大行数,它将占用所有垂直空间。混淆gridwidth/gridheightweightx/weighty是常见的,因此您必须注意它们之间的区别。请注意,使用gridwidth/gridheight/weightx/weighty只会“保留”您指定的空间,元素将放置在该空间内,锚定在anchor属性中指定的一侧。

为了使元素扩展到其保留的空间,我们设置了gbc.fill = GridBagConstraints.BOTH,其中BOTH代表水平和垂直两个方向上都填充空间(另外三个选项分别是GridBagConstraints.HORIZONTALGridBagConstraints.VERTICALGridBagConstraints.NONE,它是默认值,表示不填充)。

英文:

You can use a combination of one or multiple GridBagLayout layout managers.<br>GridBagLayout allows you to place your elements in a grid-like manner by specifiying the proportions and positioning using the class GridBagConstraints.

public void setupLayout() {
    JPanel container = new JPanel(); // the main holder of the elements
    // The numbered panels according to your screenshot
    JPanel panel1 = new JPanel(), panel2 = new JPanel(),
            panel3 = new JPanel(), panel4 = new JPanel();
	panel1.setBackground(Color.RED); // giving them colors for better visualization
	panel2.setBackground(Color.BLUE);
	panel3.setBackground(Color.YELLOW);
	panel4.setBackground(Color.GREEN);

    container.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.CENTER;
	gbc.fill = GridBagConstraints.BOTH;
    gbc.gridwidth = gbc.gridheight = 1;
	gbc.weightx = 1;
	gbc.gridx = 0; gbc.gridy = 0; gbc.weighty = 0.2;
	panel.add(panel1,gbc);

	gbc.gridy = 1; gbc.weighty = 0.3;
	panel.add(panel2,gbc);

	gbc.gridy = 2; gbc.weighty = 0.5;
	panel.add(panel3,gbc);

	gbc.gridx = 1; gbc.gridy = 0; gbc.gridheight = 3;
	panel.add(panel4,gbc);

    // Code to place your container in a frame or whatever usage you have for it...
}

The properties gridx and gridy specify the position you are on the grid.<br>weightx and weighty specify how much space (on a 0 to 1 proportion) they take up.
<br>Unlike gridwidth and gridheight they specify how much does a cell take up relative to other cells so a cell will take up a space equal to the space that gridheight cells take up vetically or gridwidth cells take up horizontally.<br>So, to be explained in action, since panel1 is set to weighty = 0.2, panel2 is set to weighty = 0.3 and panel3 is set to weighty = 0.5 we're saying that whatever cell is on the first row, will always take 0.2 of the vertical space, second row 0.3 of vertical space and third row half (0.5) the vertical space, therefore panel4 will take up the same space as how many cells heights sum up to the prior, if panel4 was set to gridheight = 2, it will take as much space as two rows take up, since in this example it's placed on the first row, it will take up therefore a space equivalent to that of the first and second rows, which is half the vertical space (0.2 + 0.3), but since we set it to 3 which is also the maximum number of rows in this example, it will take up all of the vertical space.<br>Mixing up gridwidth/gridheight and weightx/weighty is commmon therefore you have to be aware of the difference.<br>Note that using the gridwidth/gridheight/weightx/weighty will only "reserve" the space you're specifying, and the elements will be placed inside that spaced anchored to the side specified in the anchor property.
<br>In order to make the elements scale up to their reserved space we set gbc.fill = GridBagConstraints.BOTH where BOTH stands for filling up the space both horizontally and vertically (the other three choices being, clearly, GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL and GridBagConstraints.NONE which is the default value that represents no filling at all.
Java Swing组件

huangapple
  • 本文由 发表于 2023年7月31日 22:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定