1

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

4

1 回答 1

2

我没有问题...

在此处输入图像描述

我怀疑您可能正在执行以下一项或多项操作

  1. 第一个分隔符可能与容器的最顶部对齐,紧靠标题栏,使其“出现”,就好像它没有被添加一样。
  2. 你不是在 EDT 中创建你的 UI
  3. 您在完成创建窗格之前显示框架
  4. 你没有告诉我们的其他事情

.

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++;
                }
            }
        }
    }
}
于 2012-11-06T19:05:45.570 回答