我目前有一个按钮,当单击它时,它执行一个方法,该方法创建一个带有加载多个图像的面板的jframe。如果多次单击此按钮,图像将不断添加到加载到jframe上的前置图像上。我应该使用什么代码,以便在加载jframe和元素之后单击按钮,在单击一次之后,不会添加任何其他内容。
非常感谢
发布于 2012-03-14 03:00:04
下面是什么:
public void actionPerformed(ActionEvent evt) {
button.setEnabled(false);
// the other code that creates imgFrame
imgFrame.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent evt) {
button.setEnabled(true);
}});
imgFrame.setVisible(true);
}发布于 2012-03-14 02:47:39
JVM实例存在之前,这些对象永远不会从JFrames中消失,您可以非常方便地查看CardLayout来解决多个视图的问题(在本例中,在一个JFrame)的Icon/ImageIcon
发布于 2012-03-14 15:10:45
当框架显示时禁用该按钮,当框架关闭时启用该按钮。
public void actionPerformed(ActionEvent evt) {
final JButton finalButton = button;
button.setEnabled(false);
JFrame frame = new JFrame()
{
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
finalButton.setEnabled(true);
}
super.processWindowEvent(e);
}
};
frame.setVisible(true);
}https://stackoverflow.com/questions/9690120
复制相似问题