编程语言
首页 > 编程语言> > java-通过代码处理JDialog,不要让用户关闭它

java-通过代码处理JDialog,不要让用户关闭它

作者:互联网

我有一个简短的代码,我想设置一个对话框,但不希望用户能够关闭它,它只是行不通,请帮助我.

    import javax.swing.*;
    public class Processing extends JOptionPane{

    JDialog jd;
    public Processing(){
        super(null, JOptionPane.DEFAULT_OPTION, 
                  JOptionPane.INFORMATION_MESSAGE, null, new Object[]{});
        Icon processPic = new ImageIcon("C:\\Users\\Judit\\Desktop\\Mesekocka3D"
                + "\\externals\\pics\\szamitas.gif");

        setMessage(processPic);
        jd = createDialog("Számítás");

        jd.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        jd.setVisible(true);
        jd.dispose();
    }   
}

这是我的代码现在的样子,我使用Jdialog而不是Joptionpane,我是否应该写更多字符以使网站接受我的编辑?

import javax.swing.*;



public class Mifasz
{
 private static class Processing extends JDialog{

public Processing(){
    Icon processPic = new ImageIcon("C:\\Users\\Judit\\Desktop\\Mesekocka3D"
            + "\\externals\\pics\\szamitas.gif");
    JLabel label = new JLabel(processPic);
    add(label);
    setTitle("Számítás");
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);        
    setSize(400, 70);
    setLocation(330, 300);
    setModal(true);
    setVisible(true);
    dispose();
}       
}

  public static void main(String[] args)
  {
    Processing pc = new Processing();
  }

}

解决方法:

I’d like to show a gif for the user for a short time, it’s like a loadingscreen, I don’t want him to do anything with the app till, so I’m using joptionpane, but I would also like to disable the X button, so I’m using jdialog, becouse I didn’t find an option to disable the optionpane’s X button.

1)您可以使用未修饰的JDialog#setUndecorated(true);

2)将gif作为Icon放入JLabel

3)使用Swing Timer进行计时,Swing Timer的输出可以是Swing Action并在actionPerformed内放置JDialog#setVisible(fasle);

编辑

i’d like to block it, and it need to be used as many times the user clicks on a JCombobox item

1)仅创建一次JDialog,您将仅重用一次

2)以预期的延迟运行Swing Timer#repeat(false),在actionPerformed中放置JDialog#setVisible(fasle);

3)在选定项目(JComboBox)的事件上更改JLabel myLabel#setIcon(myAnotherGIF)中的图标

4)在invokeLater内部包装JDialog#setVisible(true);

5)不需要任何其他步骤

仅在特殊情况下

6)你必须打电话

myIcon.getImage().flush();
myLabel.setIcon(myIcon);

如果您在JLabel中对Icon的重新绘制有疑问

标签:jdialog,swing,dispose,java
来源: https://codeday.me/bug/20191201/2080853.html