【问题标题】:Make an item disappear使项目消失
【发布时间】:2013-12-09 20:23:51
【问题描述】:

我有一个应用程序,在进行更改后会出现一个绿色复选标记,表示更改成功。该应用程序有几个可能的更改,我希望能够在 2.5 秒后使复选标记消失。我尝试了几件事,例如:

panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);

似乎没有任何效果。我添加了一个timer 呼叫,然后是一个checkMark.setVisible(false),似乎没有任何帮助。

有人可以指出我做错了什么吗?以下是我的代码:

//Create Change Role Button
    final JButton changeRoleBtn = new JButton("Change Role");
    changeRoleBtn.setBounds(50, 500, 150, 30);
    changeRoleBtn.setToolTipText("Changes the role of the User");
    changeRoleBtn.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //Create Success Image
            final ImageIcon i1 = new ImageIcon("/Users/vhaislsalisc/Documents/workspace/Role_Switcher/greenCheck.png");
            final JLabel checkMark = new JLabel(i1);
            checkMark.isOptimizedDrawingEnabled();
            i1.paintIcon(changeRoleBtn, getGraphics(), 400,25); 
            checkMark.setVisible(true);
            try
            {
                timer = new Timer(2000, new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        checkMark.setVisible(false);
                        timer.stop();

                    }
                });
                timer.start();

            }
            catch(Exception e5)
            {
                e5.printStackTrace();
                timer.stop();
            }
        }

    });

这里是关于计时器的一点。其他代码是相关的,因为它包括我对图形的声明以及它是如何被调用和使用的。

try
            {
                timer = new Timer(2000, new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        checkMark.setVisible(false);
                        timer.stop();

                    }
                });
                timer.start();

            }
            catch(Exception e5)
            {
                e5.printStackTrace();
                timer.stop();
            }

【问题讨论】:

  • 请问,您的 200 多行 JDBC 代码与这个问题有什么关系?另一方面,你甚至没有展示你对Timer 的尝试,这是唯一重要的事情。
  • 尝试使用计时器是 200 行代码的一部分。
  • 我的意思是:我什至无法找到它。
  • @MarkoTopolnik 它在最底层
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing concurrency jbutton event-dispatch-thread


【解决方案1】:
panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);

当您从可见的 GUI 添加/删除组件时,基本代码是:

panel.add(...);
panel.revalidate();
panel.repaint();

默认情况下,所有组件的大小都为零,因此在您执行 revalidate() 调用布局管理器为组件指定大小之前,没有任何东西可以绘制。

所以你可以使用上面的代码来显示组件,然后你会启动你的定时器,当定时器触发时你会删除它。

【讨论】:

  • 您在删除复选框后仍需要重新验证()面板。发布您的 SSCCE 来证明您的问题。
【解决方案2】:

在我的checkMark.setVisible(false) 之后添加了panel.repaint();,它就像一个魅力。

try
            {
                timer = new Timer(1000, new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        checkMark.setVisible(false);
                        panel.repaint();
                        timer.stop();

                    }
                });
                timer.start();

            }
            catch(Exception e5)
            {
                e5.printStackTrace();
                timer.stop();
            }

【讨论】:

    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2013-10-25
    • 2013-10-14
    • 2013-09-18
    相关资源
    最近更新 更多