import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestJFrame {
public static void main(String[] args) {
JFrame frame = new MeinFrame();
frame.setVisible(true);
}
}
class MeinFrame extends JFrame {
private DoSomething something = new DoSomething();
public MeinFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button = new JButton("doIT");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
something.doIt();
}
}).start();
}
});
add(button);
setAlwaysOnTop(true);
pack();
}
}
class DoSomething {
private int counter = 0;
public void doIt() {
while (true) {
System.out.println("ich bin Thread[" +Thread.currentThread().getId() +"] " +counter++);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (counter >= 50) {
counter = 0;
return;
}
}
}
}