performActionメソッドでいくつかの操作を行うスイングアプリケーションを作成しましたが、progressBarを示すスレッドを作成すると、progressActionメソッドの実行中にprogressBarが表示されませんが、最後にprogressBarは100%直接
メインクラス:スイングでactionPerformed内に新しいスレッドを作成するのは安全ですか?
import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class main {
public static void main(String[] args) {
JFrame win = new JFrame("Test");
FlowLayout layout = new FlowLayout();
Button b1 = new Button("Click ");
win.add(b1);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
window win = new window();
win.start();
try
{
Thread.sleep(2000);
}
catch(InterruptedException e5){}
}
});
win.setLayout(layout);
win.setSize(500, 300);
win.setLocationRelativeTo(null);
win.setVisible(true);
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
ウィンドウクラス:
import javax.swing.*;
import java.awt.*;
public class window extends Thread{
public window(){
}
@Override
public void run() {
JFrame fen = new JFrame("New window");
FlowLayout layout = new FlowLayout();
fen.setLayout(layout);
Button b2 = new Button();
fen.setVisible(true);
fen.setSize(100, 100);
fen.setLocationRelativeTo(null);
fen.add(b2);
try
{
for(int i = 0; i <= 100; i++)
{
b2.setLabel("Button " + i);
Thread.sleep(10);
}
}
catch(InterruptedException e2){
}
}
}
これはすべて間違っています。スイング同時実行チュートリアル(https://docs.oracle.com/javase/tutorial/uiswing/concurrency/)を読んでください。そして、Javaの命名規則を尊重してください。 –