【问题标题】:Weird/different behaviour when updating JDialog更新 JDialog 时出现奇怪/不同的行为
【发布时间】:2014-11-28 06:02:42
【问题描述】:

在一个项目中,我有一个 JFrame A 启动一个 JDialog B,它本身启动一个 JDialog C(全部使用按钮)。但是,当遵循这些程序之一时:

  • 我点击 A 启动 B ;或
  • 我点击 A 启动 B,然后点击 B 启动 C,然后点击 C 的取消按钮,

B中显示的不一样(第二个过程给出了一个奇怪丑陋的东西)。

我不明白为什么会这样,因为两种方式都会调用我的 updateAll 方法。

我尝试用一​​个小程序重新创建它,以便更容易看到发生了什么。在我的实际项目中解决这个问题可能会也可能不会,但它肯定会有所帮助。

由于我不知道它可能来自哪里,这里是我的(测试)程序的完整代码。振作起来。

“A”框架

public class MyFrame extends JFrame {
    private static final long serialVersionUID = 7073064926636937881L;
    public MyFrame() {
        this.setSize(200, 300);
        JButton button = new JButton("Click me");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                new MyDialog1().setVisible(true);
            }
        });
        this.getContentPane().add(button);
    }

    public static void main(String args[]) {
        new MyFrame().setVisible(true);
    }
}

“B”对话框

 public class MyDialog1 extends JDialog {
        private static final long serialVersionUID = 9181006217120036637L;
        private JScrollPane scrollPane;
        public String text = "aaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaa";

    public MyDialog1() {
        this.setVisible(false);
        this.setSize(800, 600);

        this.initComponent();
        this.updateAll();
    }

    private void initComponent() {
        this.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        this.scrollPane = new JScrollPane();

        c.gridx = 0;
        c.gridy = 0;
        this.getContentPane().add(this.scrollPane, c);

        c.gridx = 0;
        c.gridy = 1;
        JButton b = new JButton("Supposedly edit stuff");
        final MyDialog1 caller = this;
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                new MyDialog2(caller).setVisible(true);
            }
        });
        this.getContentPane().add(b, c);

        c.gridx = 1;
        c.gridy = 1;
        b = new JButton("Leave");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                setVisible(false);
            }
        });
        this.getContentPane().add(b, c);
    }

    public void updateAll() {
        JPanel mainPanel = new JPanel();
        for (int i = 0 ; i < 5 ; i++) {
            JPanel subPanel = new JPanel();
            JTextArea t = new JTextArea(this.text);
            t.setSize(60, 30);
            t.setVisible(true);// Useful ? What about setSize ?
            subPanel.add(t);
            mainPanel.add(subPanel);
        }
        this.scrollPane.setSize(150, 150); // FIXME When in initComponent, doesn't do anything, and when in updateAll, behavior is inconsistent 
        this.scrollPane.setViewportView(mainPanel); // Replacing previous JPanel

    }
}

“C”对话框

public class MyDialog2 extends JDialog {
    private static final long serialVersionUID = 5676648412234106581L;
    private MyDialog1 caller;

    public MyDialog2(MyDialog1 c) {
        this.setSize(100, 150);
        this.caller = c;
        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                setVisible(false);
                caller.text += "\nbbbbbbbbbbbbb\nbbbbbbbbbbbbbbbbbb\nbbbbbbbbbbb\nbbbbbbbbbbbbbb\ncccccccccccccccccccccccccccccccccccccccccccc\ncccccccccc";
                caller.updateAll();
            }
        });
        this.getContentPane().add(cancelButton);
    }
}

感谢您的帮助。

【问题讨论】:

  • 这是一个布局问题(尤其是setSize 的使用),但我不确定您的意图是什么,建议另一种方式。也许张贴一张图(可能相对粗糙,例如...... MSPaint)并描述你希望MyDialog1如何正确表现。

标签: java swing awt


【解决方案1】:

我建议使用

c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH; // make the component fill its display area entirely
c.ipady = 150; //height
c.anchor = GridBagConstraints.FIRST_LINE_START; // component start from the left top corner
this.getContentPane().add(this.scrollPane, c);

用于定义 JScrollPane 约束。

另外,修改元素后添加validate()和repaint()

this.scrollPane.setViewportView(mainPanel); // Replacing previous JPanel
this.validate();
this.repaint();

【讨论】:

  • 仅使用 validate 和 repaint 使两个过程具有相同的行为。我会检查其他字段。
【解决方案2】:

忽略良好实践的简单答案:

替换这个:

this.scrollPane.setSize(150, 150);

用这个:

this.scrollPane.setMinimumSize(new Dimension(150, 150));

在具有布局的容器中的组件上调用setSize 通常什么都不做;充其量,它将设置大小直到下一次验证容器时,因为大小将被布局管理器覆盖。但是,设置 minimumSize(或 preferredSize 或 maximumSize)会设置一个受(大多数)布局管理器尊重的持久属性。

为什么setMinimumSize 会有所作为?因为您没有在任何 GridBagConstraints 上设置任何 weightxweighty 属性,所以 GridBagLayout 没有足够的空间以首选大小显示您的 JScrollPane。当 GridBagLayout 确定没有足够的空间以所需的大小显示所有内容时,布局“平底船”并强制所有内容恢复到其最小尺寸。

第一次显示 B 对话框时,您会看到 JScrollPane 的最小尺寸(即,刚好大到足以显示滚动条和视口边框)。取消 C 对话框后,setSize(150, 150) 生效,但随后对 GridBagLayout 的任何后代的任何更改都可能导致 (150, 150) 再次被 JScrollPane 的最小尺寸覆盖。

更复杂的答案,这是一种很好的做法:

删除每个类中对 setSize 的所有调用。通常您根本不应该设置显式大小,但如果必须这样做,请使用setPreferredSize

像 JScrollPanes 等用于增长和收缩的组件应在其对应的 GridBagConstraints 中分配正的 weightxweighty 值,同时将 fill 设置为 GridBagConstraints.BOTH,正如 pcej 建议的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 2017-02-01
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多