Java Swing处理状态

Java Swing处理状态,java,multithreading,swing,animation,Java,Multithreading,Swing,Animation,我正在尝试实现一个摆动框架。在本文中,我希望在执行所需任务时,使用不同的线程在textPanel中显示处理状态。我尝试了以下代码。当然,逻辑有问题。请给我提供正确的方法 import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.

我正在尝试实现一个摆动框架。在本文中,我希望在执行所需任务时,使用不同的线程在textPanel中显示处理状态。我尝试了以下代码。当然,逻辑有问题。请给我提供正确的方法

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();
    }
}

}
首先我想,“你应该使用一个
SwingWorker
,因为它有处理进度和EDT更新的方法……”

但当我仔细观察时,你实际上并不关心流程本身,你只想在其中显示流程正在运行……它们是两个独立的实体,它们只是相关的,因为一个(UI更新)将在另一个运行时运行

因此,我使用了
javax.swing.Timer
。这使我可以计划每
n
毫秒发生一次事件,并在EDT中触发该事件,保持整洁

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关于您的原始代码不起作用的原因,请参阅上述注释部分中的我的注释;)

也许让我们看看什么是当前行为?让我们从当前无行为开始。让我再看一次第二个建议。我马上想到的事情(EDT违规除外)是,您立即将
processing
再次设置为false,这意味着您的线程实际上永远不会进入它的循环,这正是目的所在。如果可能的话,请推荐一些其他的页面来学习“Swing中的并发”,这比oracle的页面更容易掌握!评论中的链接是最好的开始。从这里,您可以简单地开始寻找SwingWorker示例