0

スイングフレームを実装しようとしています。これで、必要なタスクを実行しながら、別のスレッドを使用して 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();
    }
}

}
4

1 に答える 1