Java 为什么我必须添加两次JSepator?

Java 为什么我必须添加两次JSepator?,java,swing,user-interface,jseparator,Java,Swing,User Interface,Jseparator,在我的代码中,我添加了一些jsepators。一切都很好,除了在开始时,我必须添加两个jsepators,让它们显示出来,而不是一个!代码如下: setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); int counter = 1; add(new JSeparator(SwingConstants.HORIZONTAL)); //Here is the add(new JSeparator(SwingConst

在我的代码中,我添加了一些
jsepator
s。一切都很好,除了在开始时,我必须添加两个
jsepator
s,让它们显示出来,而不是一个!代码如下:

    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 ++;
        }
    }
setLayout(新的BoxLayout(这是BoxLayout.PAGE_轴));
int计数器=1;
添加(新JSepator(SwingConstants.HORIZONTAL))//这是你的电话号码
添加(新JSepator(SwingConstants.HORIZONTAL))//问题
JPanel p2=新的JPanel();
对于(int i=0;i
如您所见,我必须调用
add(newjsepator(SwingConstants.HORIZONTAL))两次,它才能工作。如果我只调用一次,则不会显示
jsepator
。为什么会这样

这是可编译的代码:

我没有问题

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

  • 第一个分隔符可能与容器顶部对齐,紧靠标题栏,使其“显示”为未添加
  • 您没有在EDT中创建UI
  • 在完成创建窗格之前,您正在显示框架
  • 还有什么你没告诉我们的
  • 公共类testseparator{
    公共静态void main(字符串[]args){
    新的testseparator();
    }
    公共testseparator(){
    invokeLater(新的Runnable(){
    @凌驾
    公开募捐{
    试一试{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }捕获(ClassNotFoundException ex){
    }catch(实例化异常){
    }捕获(非法访问例外){
    }捕获(无支持的LookandFeelexception ex){
    }
    JFrame=新JFrame(“测试”);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(新的BorderLayout());
    frame.add(新的testseparatorpane());
    frame.pack();
    frame.setLocationRelativeTo(空);
    frame.setVisible(true);
    }
    });
    }
    公共类TestSeparatorPane扩展了JPanel{
    公共测试分离烷(){
    setLayout(新的BoxLayout(这是BoxLayout.PAGE_轴));
    int计数器=1;
    add(newjsepator(SwingConstants.HORIZONTAL));//下面是
    添加(新JSepator(SwingConstants.HORIZONTAL));//问题
    JPanel p2=新的JPanel();
    int-petsx=4;
    int-petsy=4;
    对于(int i=0;i
    没有sscce就无法判断。顺便说一句:在任何情况下(甚至不是分隔符),使用合适的布局管理器。您没有在EDT中创建UI~我该怎么做?@PicklishDoorknob从示例中查看构造函数。语句
    EventQueue.invokeLater
    正在确保runnable中的所有调用都在EDT中执行。好的,我会试试。编辑:问题。我有一个声明
    f.add(这个)
    在我的类中(
    this
    是主JPanel),当包装在Runnable中时,它假设Runnable是
    this
    。类似于
    TestSeparatorPane的东西。这可能就足够了。如果您在UI组件的构造函数中执行
    invokeLater
    ,那么您已经迟到了。简单的规则是,如果要与UI交互(尤其是更改它),必须在EDT中进行。
    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++;
                    }
                }
            }
        }
    }