Java語言從其誕生到現在不過短短五年時間
一
在Java應用程序編程中
二
解決上述問題的方法就是采用Java的多線程特性
三
(
這裡舉一個簡單的例子來介紹如何用JAVA實現線程等待提示框
此例實現一個很簡單的GUI
按下testButton
(
為了達到上述功能
當按下按鈕後
為了使提示框在TestThread結束後
(
① TestFrame類
TestFrame是Java運行主程序
import javax
import java
import java
public class TestFrame extends JFrame
{
//GUI所需組件
public JPanel testPanel = null;
public JButton testButton = null;
public JFrame testFrame = null;
public TestFrame()
{
//設置GUI為windows風格
try
{
UIManager
}
catch (Exception ex)
{
System
}
testFrame = this;
// 初始化GUI
Dimension dimensions = Toolkit
setSize(dimensions
setLocation(dimensions
dimensions
testPanel = new JPanel();
testButton = new JButton(
testPanel
getContentPane()
//增加按鈕testButton事件監聽器
testButton
public void actionPerformed(ActionEvent e) {
TestThread testThread = new TestThread();//新生成一個處理事務線程
testThread
(new ThreadDiag(testFrame
}
});
//增加testFrame事件監聽器
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System
}
});
}
public static void main(String[] args)
{
//主程序
TestFrame testFrame
testFrame
testFrame
}
}
② TestThread類
TestThread類是處理事務線程
public class TestThread extends Thread
{
public void run()
{
for (int i =
{
System
}
}
}
③ ThreadDiag類
ThreadDiag類用來顯示
import java
import javax
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
private int width = dimensions
private int left =
public ThreadDiag(JFrame parentFrame
{
this
this
this
initDiag();//初始化提示框
}
protected void initDiag()
{
clueDiag = new JDialog(parentFrame
clueDiag
JPanel testPanel = new JPanel();
JLabel testLabel = new JLabel(messages);
clueDiag
testPanel
(new DisposeDiag())
}
public void run()
{
//顯示提示框
int left = (dimensions
int top = (dimensions
clueDiag
clueDiag
clueDiag
}
}
④ DisposeDiag類
DisposeDiag類用來關閉提示框
class DisposeDiag extends Thread
{
public void run()
{
try
{
currentThread
}
catch(InterruptedException e)
{
System
}
clueDiag
}
}
注
上述程序在jdk
(
運行結果如下圖所示
當事務執行完後
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27781.html