为什么我要两次添加JSeparator?

时间:2012-11-06 13:25:01

标签: java swing user-interface jseparator

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

1 个答案:

答案 0 :(得分:2)

我没有问题......

enter image description here

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

  1. 第一个分隔符可能会与容器的顶部对齐,正对着标题栏,使其“显示”,就好像它没有被添加一样..
  2. 您没有在EDT中创建用户界面
  3. 您在完成创建窗格之前显示框架
  4. 你没有告诉我们的其他事情
  5. 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++;
                    }
                }
            }
        }
    }