gpt4 book ai didi

java - Swing 不遵守我的 GridLayout Rows x Columns 定义。怎么修?

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:04 24 4
gpt4 key购买 nike

我一直在尝试解决这个问题,但没有成功。如何使 JPanel p 遵守定义的 GridLayout?我得到的只是第一行包含 3 个面板,但不是我告诉 Java 执行的 4 个面板。

要让它发挥作用,有什么必要的巫术(或我不知道的知识)?

package temp2;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Temp2 {

public static void main(String[] args) {

JFrame f = new JFrame();
JPanel p = new JPanel();
// GridLayout flow = new GridLayout(1, 5); --> With this I get a row with 5 jpanels
GridLayout flow = new GridLayout(2, 4); // --> Why it doenst work? I want 4
// jpanels in the first row and
// subsequent one on the next row
// --> Why it doesnt respect my code?
p.setLayout(flow);
// p.setSize(800,800); // --> This doesnt make any difference on final result either
// p.setPreferredSize(new Dimension(800,800)); // --> This doesnt make any
// difference on final result either
p.setMinimumSize(new Dimension(800,800));

JPanel x1 = new JPanel();
x1.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
x1.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
x1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

JPanel x2 = new JPanel();
x2.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
x2.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
x2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

JPanel x3 = new JPanel();
x3.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
x3.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
x3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

JPanel x4 = new JPanel();
x4.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
x4.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
x4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

JPanel x5 = new JPanel();
x4.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect these preferences
x5.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect this maximum
x5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

p.add(x1);
p.add(x2);
p.add(x3);
p.add(x4);
p.add(x5);

f.getContentPane().add(p, "Center");

f.setSize(800, 800);
f.setVisible(true);
p.updateUI();

}
}

因为这是我的第一个 StackOverflow 问题,所以我试图严格遵守规则:

具体:我希望 GridLayout 遵守行 x 列定义

我的最终目的:在第一行显示 4 个面板,在第二行显示后续面板。

让它与其他人相关:这就是为什么这段代码是说教的,重复所有面板声明,以便新手(和我)能够理解所有代码并专注于 GridLayout问题。

最佳答案

    GridLayout flow = new GridLayout(2, 4); // --> Why it doenst work? I want 4 
// jpanels in the first row and
// subsequent one on the next row
// --> Why it doesnt respect my code?

像这样?

enter image description here

改用:

    GridLayout flow = new GridLayout(0,4);

引用 constructor 的文档:

Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

关于java - Swing 不遵守我的 GridLayout Rows x Columns 定义。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710164/

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