简体   繁体   English

在执行按钮操作期间删除按钮

[英]Removing a button during actionperformed of that button

I was trying to figure out why my program freezes and I was able to replicate it with a small script so I can put it here. 我试图弄清楚为什么我的程序死机了,我能够用一个小的脚本来复制它,所以我可以把它放在这里。 Basically, in this script When you clicked on button Test1, it is supposed to remove it and added new button Test2. 基本上,在此脚本中,当您单击按钮Test1时,应该将其删除并添加新按钮Test2。 The program freezes. 程序冻结。 Why? 为什么? How can I over come this? 我怎么能克服这个?

final JFrame frame = new JFrame("FrameDemo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(800, 600));

                final JPanel panel = new JPanel();
                final JButton bTest1 = new JButton("test1");
                bTest1.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e) {
                      panel.remove(bTest1);
                      panel.add(new JButton("test2"));
                    }

                });
                panel.add(bTest1);
                frame.getContentPane().add(panel);

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

Offcourse in the real program, on the button click, remove all the contents of the panel and re add a new set of components. 在真实程序中的Offcourse中,单击按钮,删除面板的所有内容并重新添加一组新的组件。

Looking for your help! 寻找您的帮助!

Offcourse in the real program, on the button click, remove all the contents of the panel and re add a new set of components. 在真实程序中的Offcourse中,单击按钮,删除面板的所有内容并重新添加一组新的组件。

Then you should probably be using a CardLayout . 然后,您可能应该使用CardLayout A CardLayout is designed to allow you to swap panels. CardLayout旨在允许您交换面板。

Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples. 阅读Swing教程中有关如何使用CardLayout的部分, 获取更多信息和工作示例。

The program freezes. 程序冻结。 Why? 为什么? How can I over come this? 我怎么能克服这个?

It doesn't freeze, its just that the panel isn't smart enough to repaint itself. 它不会冻结,只是面板不够聪明,无法自我重绘。 It you resize the frame you will see the new button. 调整框架大小后,您将看到新按钮。

The problem is you remove the button and add a new button but the panel never repaints itself because the panel is not aware of the changes. 问题是您删除了按钮并添加了新按钮,但是面板从未重新绘制自身,因为面板不知道所做的更改。 You need to invoke the layout manager so the new button can be given a proper size. 您需要调用布局管理器,以便可以为新按钮指定适当的大小。

The basic code for add/removing components on a visible GUI is: 在可见的GUI上添加/删除组件的基本代码是:

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed

Action performed method of the JButton will be executed in AWT thread. JButton的动作执行方法将在AWT线程中执行。 When you remove the button from the container, that will launch events that should be executed as well in the same thread. 从容器中删除按钮时,这将启动应在同一线程中执行的事件。 So one is waiting for the other , and so the program freezes. 因此,一个正在等待另一个,因此程序冻结了。 For solving that situation use 为了解决这种情况,请使用

SwingUtilities.invokeLater SwingUtilities.invokeLater

method to execute the removing action of your button 执行按钮的删除操作的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM