import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class StringBewegen extends JFrame {
private StringBewegenPanel panel = null;
public StringBewegen() {
super("String bewegen");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
panel = new StringBewegenPanel();
panel.setPreferredSize(new Dimension(300, 100));
final JButton button = new JButton("Start");
add(button, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(new Runnable(){
public void run() {
button.setEnabled(false);
int w = panel.getWidth();
for(int i = panel._x; i < w; i++){
panel._x = i;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
SwingUtilities.invokeLater(new Runnable(){
public void run() {
panel.validate();
panel.repaint();
};
});
}
panel._x = 20;
panel.repaint();
button.setEnabled(true);
}
}).start();
}
});
setResizable(false);
pack();
setVisible(true);
}
private class StringBewegenPanel extends JPanel {
private int _x = 20;
private int _y = 20;
@Override
protected void paintComponent(Graphics arg0) {
super.paintComponent(arg0);
arg0.drawString("Ich bewege mich", _x, _y);
}
}
/**
* @param args
*/
public static void main(String[] args) {
new StringBewegen().setVisible(true);
}
}