G
Gardakor
Gast
I'm sorry for the english comments, I don't speak German
This is the code :
Does anybody know a better solution than starting a new thread ?
My teacher (who hasn't the time to examine this case) told me it's something about resetting the focus :bahnhof:
This is the code :
Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class TestProblem implements ActionListener {
public static void main(String[] args) {
new TestProblem();
}
final int WAIT_TIME_IN_SECONDS = 5;
JButton button1, button2;
public TestProblem () {
JFrame frame = new JFrame();
JPanel window = new JPanel();
button1 = new JButton("Test");
button1.addActionListener(this);
window.add(button1);
button2 = new JButton("Click on this button, and within "+WAIT_TIME_IN_SECONDS+" seconds, 10 times on the other button");
button2.addActionListener(this);
window.add(button2);
frame.setContentPane(window);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
/*
the commands under the first if-statement show strange behaviour.
All GUI-related commands will be only executed after this method has returned.
Put all commands belonging to the first if-statement in an separate thread -> problem solved
*/
if (e.getSource() == button2) {
// this command will be executed after this ActionEvent has been handled !
button1.setEnabled(false);
try {
Thread.sleep(WAIT_TIME_IN_SECONDS * 1000);
} catch (Exception ex) {}
// this command will be executed after this ActionEvent has been handled !
button1.setEnabled(true);
}
else if (e.getSource() == button1) {
//JOptionPane.showMessageDialog(null, "Noooooo");
System.out.println("Button1 pressed");
}
}
}
Does anybody know a better solution than starting a new thread ?
My teacher (who hasn't the time to examine this case) told me it's something about resetting the focus :bahnhof: