2013-04-04 106 views
0

我正在嘗試實現一個擺動框架。在此,我想在執行所需任務時使用不同線程在textPanel中顯示處理狀態。我試了下面的代碼。當然,邏輯有問題。請爲我提供正確的做法Java Swing處理狀態

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class SampleSwing { 

private JFrame frame; 
public static JTextField textField; 
public static boolean processing=false; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       SampleSwing window = new SampleSwing(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public SampleSwing() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    textField = new JTextField(); 
    textField.setBounds(0, 31, 434, 20); 
    frame.getContentPane().add(textField); 
    textField.setColumns(10); 

    JButton btnNewButton = new JButton("New button"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      processing=true; 
      Processingstatus ps=new Processingstatus(); 
      ps.start(); 
      /*perform the actual task*/ 
      processing=false; 
     } 
    }); 
    btnNewButton.setBounds(174, 74, 89, 23); 
    frame.getContentPane().add(btnNewButton); 
} 
} 

class Processingstatus extends Thread{ 
public void run() { 
    try { 
     while(SampleSwing.processing) { 

      SampleSwing.textField.setText("Processing"); 
      Thread.sleep(1000); 
      SampleSwing.textField.setText("Processing.."); 
      Thread.sleep(1000); 
      SampleSwing.textField.setText("Processing..."); 
      Thread.sleep(1000); 
     } 
    } 
    catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

} 
+0

也許告訴我們什麼是目前的行爲? – giorashc 2013-04-04 06:23:58

+0

讓我們從[Swing中的併發]開始(http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – MadProgrammer 2013-04-04 06:24:25

+0

目前無所作爲。讓我再看看第二個建議。 – se7en 2013-04-04 06:27:19

回答

2

首先我想,「你應該使用SwingWorker,因爲它的方法來處理的進展和EDT更新...」

但是,當我仔細看了看,你實際上並不真正在意流程本身,你只是想要在哪裏顯示流程正在運行......它們是兩個獨立的實體,只是相關的,因爲一個(UI更新)將運行,只要其他正在運行。因此,我使用了javax.swing.Timer。這讓我來安排每n毫秒一個事件的發生,並有在EDT,非常乾淨,引發...

enter image description here

import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.List; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.SwingWorker; 
import javax.swing.Timer; 

public class SampleSwing { 

    private JFrame frame; 
    public static JTextField textField; 
    public static boolean processing = false; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        SampleSwing window = new SampleSwing(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public SampleSwing() { 
     initialize(); 
    } 
    private Timer processTimer; 

    private void initialize() { 
     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridBagLayout()); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 

     textField = new JTextField(25); 
     frame.add(textField, gbc); 

     processTimer = new Timer(500, new ActionListener() { 
      private StringBuilder dots = new StringBuilder(3); 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       dots.append("."); 
       if (dots.length() > 3) { 
        dots.delete(0, dots.length()); 
       } 
       textField.setText("Processing" + dots.toString()); 
      } 
     }); 

     JButton btnNewButton = new JButton("New button"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       if (!processing) { 
        processing = true; 
        processTimer.start(); 
       } else { 
        processTimer.stop(); 
        processing = false; 
        textField.setText(null); 
       } 
      } 
     }); 
     frame.add(btnNewButton, gbc); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
    } 
} 

PS對於爲什麼你原來的代碼沒」 t工作,看看我的評論在上面的評論部分;)

+0

,這正是目的。如果可能的話,請建議一些其他頁面來學習「併發在Swing中」,除了oracle之外,它更易於掌握! – se7en 2013-04-10 17:05:33

+0

評論中的鏈接是最佳開局。從這裏你可以簡單地開始尋找SwingWorker的例子 – MadProgrammer 2013-04-10 20:43:07