的Java Swing处理状态(Java Swing processing status)

2019-08-19 09:15发布

我想实现一个秋千架。 在此,我想用一个不同的线程在执行任务的需要,以显示在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();
    }
}

}

Answer 1:

首先我想,“你应该使用SwingWorker ,因为它的方法来处理的进展和EDT更新...”

但是,当我看清楚,你实际上并不真正关心的过程本身,你只是想一些地方表明进程正在运行...他们是两个独立的实体,即只与,因为一个(用户界面更新)将只要其他程序运行期间运行。

所以,相反,我用了一个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为什么你原来的代码没有工作,看到我在上面的评论部分评论的原因;)



文章来源: Java Swing processing status