removeAll 在下次验证时不删除? [英] removeAll not removing at next validate?

查看:28
本文介绍了removeAll 在下次验证时不删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么以下内容不能按我的预期工作吗?

Can someone explain why the following doesn't work as I expect?

按下按钮应该"导致显示仅包含(空)JScrollPane,即输入字段和按钮应该消失.但是它们会一直保留到组件调整大小...

Pressing the button 'should' result in the display only containing the (empty) JScrollPane, ie the input field and button should disappear. However they stay until the component is resized...

public static void main(String[] args)
{
    JFrame frame = new JFrame("test");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    final JPanel panel = new JPanel();

    Container cp = frame.getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(new JScrollPane(panel));

    Component textField = new JTextField("i am input");
    JButton button = new JButton(new AbstractAction("i am pressy")
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            // this is already on the EDT
            panel.removeAll();
            panel.revalidate();
        }
    });

    panel.setLayout(new FlowLayout());
    panel.add(textField);
    panel.add(button);

    frame.pack();
    frame.setVisible(true);
}

感谢您的帮助.

推荐答案

当更新一个可见的 GUI 时,代码应该是:

When updating a visible GUI the code should be:

panel.revalidate();
panel.repaint(); // sometimes needed, this appears to be one of them

这篇关于removeAll 在下次验证时不删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆