📄 waitingdlg.java
字号:
package com.icbcsdc.ddlexp.ui.dialog;
import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import com.icbcsdc.ddlexp.pub.staticLog.Logger;
//import javax.swing.JPanel;
//import javax.swing.JProgressBar;
//import com.borland.jbcl.layout.XYConstraints;
//import com.borland.jbcl.layout.XYLayout;
/**
* @author lijf
* <p>进度条显示对话框</p>
* <p>edit by zhangp</p>
* <p>usage:</p>
* <p>在构造函数中传人父窗体和主线程</p>
* <p>可以显示在父窗体中间或屏幕中间或任意位置</p>
* <p>主线程结束则窗体自动关闭</p>
*/
public class WaitingDlg extends Window implements ActionListener {
public static long INTERVAL=1000;
public static Font DEFAULT_FONT=new Font("宋体",Font.PLAIN,14);
public int WIDTH=350;//窗体宽度
public int HEIGHT=150;//窗体高度
public String IMAGE_FILE="img/progress.gif";
public String progressText=null; //="节点正在连接中...";//进度条中显示的文字
private java.awt.Frame parent;
private DeamonThread thread;
/**
* 构造函数
* @param parent 父窗体
* @param thread 主线程
*/
public WaitingDlg(java.awt.Frame parent, DeamonThread thread,String text) {
super(parent);
parent.setEnabled(false);//禁止父窗体
progressText=text;
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
this.parent = parent;
this.thread = thread;
try {
jbInit();
} catch (Exception e) {
parent.setEnabled(true);
}
}
//Component initialization
private void jbInit() throws Exception {
Container contentPane=this;
contentPane.setLayout(null);
JLabel label=new JLabel();
label.setBounds(0,0,WIDTH,HEIGHT);
label.setBorder(BorderFactory.createLineBorder(Color.black));
this.add(label);
contentPane=label;
//gif图片
ImageIcon icon = new ImageIcon(IMAGE_FILE);
JLabel imgLabel=new JLabel(icon);
int imgW=icon.getIconWidth();
int imgH=icon.getIconHeight();
imgLabel.setBounds(0,0,imgW,HEIGHT);
//add
contentPane.add(imgLabel);
//进度条
JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
progressBar.setIndeterminate(true);
progressBar.setString(progressText);
progressBar.setFont(DEFAULT_FONT);
progressBar.setForeground(Color.BLUE);
progressBar.setBackground(Color.LIGHT_GRAY);
int barHeight =20;
int edge=10;
progressBar.setBounds(imgW+edge,(HEIGHT-barHeight)/2,WIDTH-imgW-2*edge,barHeight);
//add
contentPane.add(progressBar);
//this.setBackground(Color.MAGENTA);
this.setBackground(Color.LIGHT_GRAY);
this.setSize(new Dimension(WIDTH, HEIGHT));
//下面的代码用于检测父进程,以便自动关闭本对话框
final Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
if(thread!=null){
//Logger.log(Logger.DEBUG,"thread:"+thread+"/RUNNING:"+thread.isRunning());
//Logger.log(Logger.DEBUG,"thread:"+thread+"/ALIVE:"+thread.isAlive());
if( (!thread.isAlive()) ||(!thread.isRunning()) ){
parent.setEnabled(true);//启用父窗体
dispose();
timer.cancel();
}
}
}
};
timer.schedule(task, 0, INTERVAL);
}
//Overridden so we can exit when window is closed
// protected void processWindowEvent(WindowEvent e) {
// }
//Close the dialog on a button event
public void actionPerformed(ActionEvent e) {
}
/**
* 以给定坐标点显示窗体
* @param x
* @param y
*/
public void show(int x,int y){
this.setLocation(x,y);
show();
}
/**
* 显示在父窗体中间
*/
public void showInParentMiddle(){
Dimension parentSize=parent.getSize();
Dimension selfSize=this.getSize();
Point loc=parent.getLocation();
this.setLocation((parentSize.width - selfSize.width) / 2 + loc.x,
(parentSize.height - selfSize.height) / 2 + loc.y);
show();
}
/**
* 显示在屏幕中间
*/
public void showInScreenMiddle(){
Dimension parentSize=this.getToolkit().getScreenSize();
Dimension selfSize=this.getSize();
Point loc=new Point(0,0);
this.setLocation((parentSize.width - selfSize.width) / 2 + loc.x,
(parentSize.height - selfSize.height) / 2 + loc.y);
show();
}
public static void main(String[] args) {
JFrame frame=new JFrame("mainForm");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(400,400);
frame.show();
final DeamonThread mainthread=new DeamonThread();
mainthread.start();
WaitingDlg dlg = new WaitingDlg(frame,mainthread,"waiting...");
//dlg.show(300,300);
dlg.showInParentMiddle();
//dlg.showInScreenMiddle();
}
/**
* Returns the progressText.
* @return String
*/
public String getProgressText() {
return progressText;
}
/**
* Sets the progressText.
* @param progressText The progressText to set
*/
public void setProgressText(String progressText) {
this.progressText = progressText;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -