需求:Java SE中运行程序,弹出一个框,用来告诉用户,系统已经运行了,关不关闭该框,都不影响后台程序的运行,该框可以随时关闭,但是不影响程序的运行,当程序运行完,框会自动关闭!
效果如图:
package com.accord.util;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* @author
* 2018年9月25日
* 窗口的打开和关闭
*/
public class TransferingUtil {
public static void main(String[] args) throws Exception {
TransferingUtil.open();
Thread.sleep(1000);
TransferingUtil.close();
}
static JFrame frame;
public static void open() {
frame = new JFrame();
frame.setTitle("温馨提示:系统导数运行中。。。");
frame.setBounds(400,300,600,400);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//frame.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
String imgPath = System.getProperty("user.dir") + "/config/1.png";
ImageIcon img = new ImageIcon(imgPath);
JLabel bg = new JLabel(img);
frame.getLayeredPane().add(bg,new Integer(Integer.MIN_VALUE));
bg.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
JPanel jp= (JPanel)frame.getContentPane();//强制类型转换
jp.setOpaque(false);
jp.setLayout(new FlowLayout());
jp.add(bg);
}
public static void close() {
frame.setVisible(false);
frame.dispose();
}
}