1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import java.awt.Dimension;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestTextArea extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
new TestTextArea();
}
public TestTextArea() {
setSize(400, 400);
setContentPane(new JPanel());
TextArea textArea=new TextArea();
textArea.setPreferredSize(new Dimension(300,300));
JButton jButton=new JButton("click me to show action");
jButton.addActionListener(new ChangeTextActionListener(textArea));
add(textArea);
add(jButton);
setVisible(true);
}
class ChangeTextActionListener implements ActionListener {
private TextComponent textComponent;
public ChangeTextActionListener(TextComponent textComponent) {
this.textComponent = textComponent;
}
public void actionPerformed(ActionEvent e) {
for (int i=0;i<20;i++) {
textComponent.setText("N"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
} |
Partager