我想建立一個JFrame
能夠運行一個後臺任務每次JButton
被點擊。當前我正在使用一個swing工作人員,它不會允許任務被執行多次。重複SwingWorker
我怎樣才能啓用SwingWorker
與JButton
點擊重複任務。
public class ScanFileFrame extends JFrame{
JButton btnTicking;
JLabel label1;
ScanFileFrame(){
JFrame jframe = new JFrame();
jframe.setLayout(new FlowLayout());
btnTicking = new JButton("Start Scanning Files");
label1 = new JLabel("No File Scanned");
btnTicking.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
worker.execute();
}
});
jframe.add(btnTicking);
jframe.add(label1);
jframe.setVisible(true);
jframe.setSize(300,300);
}
SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
@Override
protected Boolean doInBackground() throws Exception {
// Simulate scan file
System.out.println("scanning files ....");
Thread.sleep(2000);
return true;
}
//update jframe jlabel when background task finish
protected void done() {
label1.setText("Files Scanned");
System.out.println("complete");
}
};
public static void main(String[] args){
ScanFileFrame f = new ScanFileFrame();
}
}
*「使用揮杆工並且不允許任務被執行多次」*因此創建另一個。有什麼問題?爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)。 – 2014-10-28 02:16:57
哦,我如何實現它,因爲它將取決於用戶點擊。有沒有辦法可以自動創建一個新用戶時,我click.sorry是新的,有很多東西需要學習 – 2014-10-28 02:20:34
我將修改我的代碼.. – 2014-10-28 02:22:01