【发布时间】:2012-11-06 13:25:00
【问题描述】:
在我的代码中,我添加了一些JSeparators。一切正常,除了一开始,我必须添加两个JSeparators 才能显示,而不是一个!代码如下:
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
int counter = 1;
add(new JSeparator(SwingConstants.HORIZONTAL)); //Here is the
add(new JSeparator(SwingConstants.HORIZONTAL)); //problem
JPanel p2 = new JPanel();
for (int i = 0; i < petsx; i ++) {
for (int i2 = 0; i2 < petsy; i2 ++) {
p2.add(new JLabel(new ImageIcon(pics[i][i2])));
p2.add(new JLabel(names[i][i2]));
if (counter % petsx == 0) {
add(p2);
add(new JSeparator(SwingConstants.HORIZONTAL));
p2 = new JPanel();
counter = 0;
} else {
JSeparator js = new JSeparator(SwingConstants.VERTICAL);
js.setPreferredSize(new Dimension(1, newPetHeight));
p2.add(js);
}
counter ++;
}
}
如您所见,我必须调用add(new JSeparator(SwingConstants.HORIZONTAL)); 两次才能使其正常工作。如果我只调用一次,JSeparator 不会出现。这是为什么呢?
这里是编译代码:http://pastebin.com/xbe47a29
【问题讨论】:
-
没有 sscce 就无法判断。顺便说一句:never-ever call setXXSize 在 anything 上(甚至不是分隔符),而是使用合适的 LayoutManager。
标签: java swing user-interface jseparator