Swing を使用したサンプル コードがあります。
package playerlist;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.*;
public class Sample extends JFrame{
private JButton button1;
private JButton button2;
public Sample(){
super();
setTitle("Sample JFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button1ActionPerformed(e);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button2ActionPerformed(e);
}
});
setLayout(new FlowLayout());
add(button1);
add(button2);
pack();
}
private void button1ActionPerformed(ActionEvent ae){
button1.setEnabled(false);
button2.setEnabled(false);
try{
Thread.sleep(5000);
}catch(Exception e){
}
System.out.println("*** Button 1 Clicked ***");
button1.setEnabled(true);
button2.setEnabled(true);
}
private void button2ActionPerformed(ActionEvent ae){
button1.setEnabled(false);
button2.setEnabled(false);
try{
Thread.sleep(5000);
}catch(Exception e){
}
// I have disabled this button from button 1's action, but still when I click this button within
// 5 seconds, actions of this button is performed
System.out.println("*** Button 2 Clicked ***");
button1.setEnabled(true);
button2.setEnabled(true);
}
public static void main(String [] args){
new Sample().setVisible(true);
}
}
私はしたい-ボタン1をクリックすると(ボタン1のアクションが開始されたとき)、ボタン1とボタン2を無効にする必要があります(無効なボタンをクリックすると、アクションは実行されません)。setEnabled(false) を使用して両方のボタンを無効にしました。そして、button1 のアクションが完了すると、両方のボタンが有効になります。しかし、私のコードでは、ボタンを無効にした後でも、無効なボタンでアクションが実行されています。button1 のアクションでは、両方のボタンを無効にし、sleep メソッドを使用して実行を一時停止しました (重い作業をシミュレートするため)。私を助けてください。サンプルコードを提供しており、実行すると、button1 をクリックした後、すぐに button2 の両方のボタンのアクションが実行されます。ボタンを押すたびに、ボタンのクリック アクションで重い作業が行われます。その間、すべてのボタンを無効にするので、他のアクションは実行できません。最初のアクションが完了したら、すべてのボタンを有効にします。私を助けてください。前もって感謝します。