この質問に答えている間、私はこの興味深い状況に出くわしました。
設計が不十分なこのコードを試してください-
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class abc extends JFrame implements ActionListener
{
boolean button_clicked = false;
JButton b1;
abc(){
this.setSize (400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.createUI();
}
void createUI(){
this.setLayout(null);
b1 = new JButton("Click here");
b1.setSize(110,30);
b1.setLocation(10,210);
this.add(b1);
b1.addActionListener(this);
}
public boolean isButton_clicked()
{
return button_clicked;
}
public void setButton_clicked(boolean button_clicked) {
this.button_clicked = button_clicked;
}
public void actionPerformed(ActionEvent arg0) {
button_clicked = true;
}
}
これが主な方法です。
class tempMain extends JFrame
{
public static void main(String[] args) throws Exception
{
abc temp = new abc();
temp.setVisible(true);
while(true)
{
// Thread.sleep(200);
if(temp.isButton_clicked())
{
JOptionPane.showMessageDialog(null, "Hello");
temp.setButton_clicked(false);
}
}
}
}
これをWindows7マシンで実行したとき、ボタンをクリックしてから少なくとも約1分間は何も起こりませんでした(その後は待ちませんでした)。
さて、小さな変更を1つだけ行います-
Thread.sleep(200); // uncomment this from the main.
そして驚くべきことに、それは機能し、JOptionPaneメッセージが表示されます。メッセージが最初に表示されないのはなぜですか?