MILY: 宋体; mso-bidi-font-size: 10.0pt">import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestFrame extends JFrame
{
//GUI所需组件
public JPanel testPanel = null;
public JButton testButton = null;
public JFrame testFrame = null;
public TestFrame()
{
//设置GUI为windows风格
try
{
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println(“Exception: ” + ex);
}
testFrame = this;
// 初始化GUI
Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dimensions.width /2, dimensions.height /2);
setLocation(dimensions.width/2-dimensions.width/4,
dimensions.height/2-dimensions.height/4);
testPanel = new JPanel();
testButton = new JButton("开始线程");
testPanel.add(testButton);
getContentPane().add(testPanel);
//增加按钮testButton事件监听器
testButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
TestThread testThread = new TestThread();//新生成一个处理事务线程
testThread.start();//启动事务线程
(new ThreadDiag(testFrame, testThread ,
"正在执行,请等待......")).start();//启动等待提示框线程
}
});
//增加testFrame事件监听器
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
//主程序
TestFrame testFrame2 = new TestFrame();
testFrame2.setTitle("线程等待测试");
testFrame2.show();
}
}
② TestThread类
TestThread类是处理事务线程,即在标准输出设备上显示从1到100000。
public class TestThread extends Thread
{
public void run()
{
for (int i = 1; i < 100000 ; i++ )
{
System.out.println(i);
}
}
}
③ ThreadDiag类
ThreadDiag类用来显示“线程正在运行”提示框。
import java.awt.*;
import javax.swing.*;
public class ThreadDiag extends Thread
{
private Thread currentThread = null;//实际调用时就是TestThread事务处理线程
private String messages = "";//提示框的提示信息
private JFrame parentFrame = null;//提示框的父窗体
private JDialog clueDiag = null;// “线程正在运行”提示框
private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
private int width = dimensions.width / 4, height = 60;
private int left = 0, top = 0;
public ThreadDiag(JFrame parentFrame, Thread currentThread, String messages)
{
this.parentFrame = parentFrame;
this.currentThread = currentThread;
this.messages = messages;
initDiag();//初始化提示框
}
protected void initDiag()
{
clueDiag = new JDialog(parentFrame,"正在执行,请等待...",true);
clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR));
JPanel testPanel = new JPanel();
JLabel testLabel = new JLabel(messages);
clueDiag.getContentPane().add(testPanel);
testPanel.add(testLabel);
(new DisposeDiag()).start();//启动关闭提示框线程
}
public void run()
{
//显示提示框
int left = (dimensions.width - width)/2;
int top = (dimensions.height - height)/2;
clueDiag.setSize(new Dimension(width,height));
clueDiag.setLocation(left, top);
clueDiag.show();
}
}
④ DisposeDiag类
DisposeDiag类用来关闭提示框
class DisposeDiag extends Thread
{
public void run()
{
try
{
currentThread.join();//等待事务处理线程结束
}
catch(InterruptedException e)
{
System.out.println("Exception:" + e);
}
clueDiag.dispose();//关闭提示框
}
}
注:为了共用变量clueDiag,上述ThreadDiag类和DisposeDiag类放在同一个Java文件内,如果分开存放,只需传递一下参数即可。
上述程序在jdk1.3下运行通过。
(4)程序运行结果
运行结果如下图所示:
当事务执行完后,“正在执行”提示框自动关闭。