有人可以解释为什么以下不能像我期望的那样工作吗?
按下"应该"按钮会导致显示只包含(空)JScrollPane,即输入字段和按钮应该消失.然而,它们一直存在,直到组件调整大小...
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);
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.页.