Dar*_*pto 0 java concurrency swing jbutton event-dispatch-thread
我有一个应用程序,在更改后,出现绿色复选标记,表示更改成功.应用程序有几个可能的更改,我希望能够在2.5秒后使复选标记消失.我尝试过几样的事情:
panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);
Run Code Online (Sandbox Code Playgroud)
似乎没有什么工作.我添加了一个timer电话,接着是a 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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是关于计时器的一点.其他代码是相关的,因为它包括我对图形的声明以及如何调用和使用它.
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();
}
Run Code Online (Sandbox Code Playgroud)
panel.add(checkMark);
checkMark.setVisible(true);
panel.remove(checkMark);
checkMark.setVisible(false);
Run Code Online (Sandbox Code Playgroud)
从可见GUI添加/删除组件时,基本代码为:
panel.add(...);
panel.revalidate();
panel.repaint();
Run Code Online (Sandbox Code Playgroud)
默认情况下,所有组件都具有零大小,因此在执行revalidate()之前无需绘制任何内容,该revalidate()调用布局管理器以为组件提供大小.
因此,您将使用上面的代码来显示组件,然后您将启动您的计时器,当计时器触发时,您将删除它.
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |