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

查看:129
本文介绍了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);
}

感谢您的帮助。 p。

推荐答案

更新可见的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天全站免登陆