2013-04-04 128 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