【问题标题】:Why do I have to add a JSeparator twice?为什么我必须添加两次 JSeparator?
【发布时间】:2012-11-06 13:25:00
【问题描述】:

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

【问题讨论】:

  • 没有 sscce 就无法判断。顺便说一句:never-ever call setXXSizeanything 上(甚至不是分隔符),而是使用合适的 LayoutManager。

标签: java swing user-interface jseparator


【解决方案1】:

我没有问题...

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

  1. 第一个分隔符可能会与容器的最顶部对齐,紧靠标题栏,使其“显示”就好像它没有被添加一样..
  2. 您不是在 EDT 中创建用户界面
  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++;
                }
            }
        }
    }
}

【讨论】:

  • 2.你不是在 EDT 中创建你的 UI 〜我该怎么做呢?
  • @PicklishDoorknob 看一下示例中的构造函数。语句 EventQueue.invokeLater 确保可运行对象中的所有调用都在 EDT 中执行
  • 好的,我会试试的。编辑:问题。我在课堂上有声明 f.add(this);this 是主要的 JPanel),并且当包装在 Runnable 中时,它假定 Runnable 是 this
  • TestSeperatorPane.this 之类的内容可能就足够了。如果您在 UI 组件的构造函数中执行 invokeLater,那么您已经迟到了。简单的规则,您想要与 UI 交互(尤其是更改它),您必须在 EDT 中进行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多