用swing编写的简单安装界面

public class InstallUI extends JFrame implements ChangeListener, ActionListener {

private static final long serialVersionUID = 1L;
static JLabel jLabelTop;//顶层图片标签
static JLabel jLabelLeft;//左边图片标签
static JLabel jLabelCenter;//中间显示倒计时标签
static JTextArea jTextArea;//右边文本域标签
static JButton jButton;//底部按钮
static JProgressBar jProgressbar;//进度条
static Timer timer;//进度条计时器
static String progressSgin;//标识进度条显示快慢
static boolean stopTimer = false;//倒计时暂停标识
Object[] options = { "OK", "Cancel" };//OK、Cancel按钮
Object[] errorOpt = { "OK" };//出错按钮
MyThread thread = null;//倒计时线程

/**
* 界面初始化构造函数
*/
public InstallUI() {
Image icon = Toolkit.getDefaultToolkit().getImage("./bin/title.gif");
this.setIconImage(icon);// 替换Java图标
this.setSize(720, 460);
this.getContentPane().setLayout(null);
this.add(getJLabelTop(), null);
this.add(getJTextArea(), null);
this.add(getJButton(), null);
this.add(getJLabelLeft(), null);
this.add(getJProgressBar(), null);
this.add(getJLabelCenter(), null);
this.setTitle("Install UniBI");
this.setResizable(false);// 去掉Jframe最大化按钮
this.setLocationRelativeTo(null);// 设置窗口中央显示
// this.setUndecorated(true); //去除JFrame的边框就跟JWindow一样

/*
* 处理关闭窗口的操作,若你没写这一段,就算你已经关闭窗口了,但程序并不会终止。
*/
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);//设置点击关闭按钮触发事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
showOptionDialog();
}
});
}

/**
* 顶层图片标签
* @return
*/
private JLabel getJLabelTop() {
if (jLabelTop == null) {
jLabelTop = new javax.swing.JLabel(new ImageIcon("./bin/title.gif"));
jLabelTop.setBounds(245, 10, 453, 40);
}
return jLabelTop;
}

/**
* 左边图片标签
* @return
*/
private JLabel getJLabelLeft() {
if (jLabelLeft == null) {
jLabelLeft = new javax.swing.JLabel(
new ImageIcon("./bin/left1.gif"));
jLabelLeft.setBounds(8, 10, 230, 400);
}
return jLabelLeft;
}

/**
* 倒计时图片标签
* @return
*/
private JLabel getJLabelCenter() {
if (jLabelCenter == null) {
jLabelCenter = new javax.swing.JLabel();
jLabelCenter.setBounds(35, 210, 150, 30);

}
return jLabelCenter;
}

/**
* 文本域显示区
* @return
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new javax.swing.JTextArea();
jTextArea.setBounds(245, 60, 440, 260);
}
return jTextArea;
}

/**
* 进度条显示区
* @return
*/
private JProgressBar getJProgressBar() {
if (jProgressbar == null) {
jProgressbar = new JProgressBar();
jProgressbar.setBounds(245, 345, 430, 27);
jProgressbar.setOrientation(JProgressBar.HORIZONTAL);
jProgressbar.setMinimum(0);
jProgressbar.setMaximum(100);
jProgressbar.setValue(0);
jProgressbar.setStringPainted(true);
jProgressbar.addChangeListener(this);
jProgressbar.setPreferredSize(new Dimension(300, 20));
jProgressbar.setBorderPainted(true);
jProgressbar.setBackground(Color.white);
jProgressbar.setForeground(Color.decode("#33CC00"));
setTimer(10);
}
return jProgressbar;
}

/**
* 弹出消息选择框
*/
private void showOptionDialog() {
stopTimer = true;
timer.stop();
int response = JOptionPane.showOptionDialog(null,
"Are you sure want to exit installation UniBI?",
"Exit Installation UniBI", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (response == 0) {
System.exit(0);
} else {
timer.start();
stopTimer = false;
}
}

/**
* 点击底部按钮时触发事件
* @return
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new javax.swing.JButton();
jButton.setBounds(615, 390, 75, 27);
jButton.setText("Cancel");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showOptionDialog();

}
});
}
return jButton;
}

/**
* 设置文本域显示信息
* @param str
*/
public void setExposition(String str) {
String exposeInfor = null;
String[] showInfor = str.split("#");
exposeInfor = showInfor[0] + "\n";
for (int i = 1; i < showInfor.length; i++) {
exposeInfor = exposeInfor + "\n    " + showInfor[i] + "\n";
}
jTextArea.setText(exposeInfor);
jTextArea.setFont(new Font("Arial", Font.ROMAN_BASELINE, 12));
}

/**
* 弹出错误消息框
* @param str
*/
public void setMoreErrorInfo(String str){
stopTimer = true;
timer.stop();
String exposeInfor = "";
String[] showInfor = str.split("#");
exposeInfor = "1."+showInfor[1]+ "\n";
for (int i = 2; i < showInfor.length; i++) {
exposeInfor = exposeInfor +String.valueOf(i)+"."+ showInfor[i]+ "\n";
}
JOptionPane pane = new JOptionPane(showInfor[0],
JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION,null,errorOpt);
JDialog dialog = pane.createDialog(null, "Error");
Container grabbedContent = dialog.getContentPane();
JTextArea area = new JTextArea(exposeInfor, 4,
20);
pane.setFont(new Font("宋体", 0, 12));
area.setFont(new Font("宋体", 0, 12));
area.setLineWrap(true);
area.setWrapStyleWord(true);

JScrollPane scroller = new JScrollPane(area,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
MoreInfoPanel mip = new MoreInfoPanel(grabbedContent, scroller);
dialog.setContentPane(mip);
dialog.pack();
dialog.setVisible(true);
System.exit(0);
}

@Override
public void stateChanged(ChangeEvent e) {

}

private void setTimer(int timers) {
timer = new Timer(timers, (ActionListener) this);
timer.start();

}

/**
* 进度条触发事件
*/
@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == timer) {
int value = jProgressbar.getValue();
if (value < 100)
jProgressbar.setValue(++value);
else {
timer.stop();
jProgressbar.setValue(0);
if (progressSgin.equals("step1")) {
setTimer(1000);
} else if (progressSgin.equals("step2")) {
setTimer(100);
}
// jButton.setText("Finish");
// this.dispose();//当完成100%的时候自动退出程序
}
}

}

/**
* 显示倒计时
* @param num
*/
@SuppressWarnings("deprecation")
public void showUntime(int num) {
if (thread != null) {
thread.stop();
}
thread = new MyThread();
thread.start();
}

/**
* 倒计时线程
* @author dKF41073
*
*/
private class MyThread extends Thread {
public void run() {
while (true) {
try {
for (int i = 0; i <= 70; i++) {
while (stopTimer) {
sleep(1);
}
sleep(100);
jLabelCenter.setText("About " + String.valueOf(70 - i)
+ " minutes left...");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
}
}

public static void main(String[] args) {
InstallUI w = new InstallUI();
w.setVisible(true);
InstallUI.progressSgin="step1";
w.showUntime(70);
w.setExposition("Init installation and check Preparations#1.Read and Check install config------Processing");
InstallUI.jLabelLeft.setIcon(new ImageIcon("./bin/left2.gif"));
// InstallUI.jProgressbar.setValue(95);
// InstallUI.jLabelCenter.setText("haha");

}
}

你可能感兴趣的:(swing)