在我的代码中,我添加了一些JSeparator
s。一切都很好,除了在开始时,我必须添加两个JSeparator
来显示,而不是一个!这是代码:
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
答案 0 :(得分:2)
我没有问题......
我怀疑您可能正在执行以下一项或多项工作
public class TestSeperator {
public static void main(String[] args) {
new TestSeperator();
}
public TestSeperator() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestSeperatorPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestSeperatorPane extends JPanel {
public TestSeperatorPane() {
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();
int petsx = 4;
int petsy = 4;
for (int i = 0; i < petsx; i++) {
for (int i2 = 0; i2 < petsy; i2++) {
p2.add(new JLabel(":)"));
p2.add(new JLabel("A"));
if (counter % petsx == 0) {
add(p2);
add(new JSeparator(SwingConstants.HORIZONTAL));
p2 = new JPanel();
counter = 0;
} else {
JSeparator js = new JSeparator(SwingConstants.VERTICAL);
// This is a bad idea...
//js.setPreferredSize(new Dimension(1, newPetHeight));
js.setBorder(new EmptyBorder(6, 0, 6, 0));
p2.add(js);
}
counter++;
}
}
}
}
}