Gridlayout的不确定行为
我在Java中创建了一个Gridlayout(3,2)
,并插入6 Jlabel
组件。它们应按照书面的代码顺序插入,但它们是按降序排列的。为什么?
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Summary_Graph_JPanel1 extends JPanel {
public Summary_Graph_JPanel1() {
GridLayout g=new GridLayout(3,2);
setLayout(g);
add(new JLabel("1"),JLabel.CENTER);
add(new JLabel("2"),JLabel.CENTER);
add(new JLabel("3"),JLabel.CENTER);
add(new JLabel("4"),JLabel.CENTER);
add(new JLabel("5"),JLabel.CENTER);
add(new JLabel("6"),JLabel.CENTER);
}
}
这就是外观:

I have created a GridLayout(3,2)
in Java and inserted 6 JLabel
components. They should be inserted in order of the code written, but they are arranged in descending order. Why?
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Summary_Graph_JPanel1 extends JPanel {
public Summary_Graph_JPanel1() {
GridLayout g=new GridLayout(3,2);
setLayout(g);
add(new JLabel("1"),JLabel.CENTER);
add(new JLabel("2"),JLabel.CENTER);
add(new JLabel("3"),JLabel.CENTER);
add(new JLabel("4"),JLabel.CENTER);
add(new JLabel("5"),JLabel.CENTER);
add(new JLabel("6"),JLabel.CENTER);
}
}
This is how it looks:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jlabel.center
的使用位于错误的位置。它应该在jlabel
构造函数中,而不是交给add(Jlabel,Constraint)
方法。它不是布局的适当约束,而是标签的限制。The use of
JLabel.CENTER
is in the wrong place. It should be within theJLabel
constructor rather than handed to theadd(JLabel, constraint)
method. It is not an appropriate constraint of the layout, but of the label.