在我的代码中,我添加了一些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
