Rod*_*dez 4 java swing awt layout-manager grid-layout
我一直试图解决这个问题但没有成功.如何JPanel p尊重定义GridLayout?我得到的是第一行包含3个面板,但不是4,因为我告诉Java要执行.
什么是必要的伏都教(或我的无知的知识),使它工作?
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();
}
}
Run Code Online (Sandbox Code Playgroud)
因为这是我的第一个StackOverflow问题,所以我试图严格遵守规则:
具体一点:我想GridLayout尊重行x列的定义
我的最终目的是:在第一行显示4个面板,在第二行显示后续的面板.
让它与其他人相关:这就是为什么这段代码是教学的,重复所有的面板声明,所以新手(和我)可以理解所有的代码并专注于这个GridLayout问题.
And*_*son 11
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?
Run Code Online (Sandbox Code Playgroud)
像这样?

改为使用:
GridLayout flow = new GridLayout(0,4);
Run Code Online (Sandbox Code Playgroud)
引用构造函数的文档:
创建具有指定行数和列数的网格布局.布局中的所有组件都具有相同的大小.
行和列中的一个(但不是两个)可以为零,这意味着可以将任意数量的对象放置在行或列中.
| 归档时间: |
|
| 查看次数: |
3012 次 |
| 最近记录: |