界面:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class testindex extends JFrame {
public testindex() {
JPanel panel = new JPanel();
JPanel pane2 = new JPanel();
final JTextField Field1 = new JTextField(20);
final JTextField Field2= new JTextField(20);
JButton 启动 = new JButton("启动");
启动.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
addnum add1 = new addnum(100, Field1);
addchar add2 = new addchar('a', 100, Field2);
add1.start();
add2.start();
}
});
panel.add(Field1);
pane2.add(Field2);
this.add(panel, BorderLayout.NORTH);
this.add(pane2,BorderLayout.CENTER);
this.add(启动, BorderLayout.SOUTH);
}
public static void main(String[] args) {
testindex frame = new testindex();
frame.setVisible(true);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); }}
追加数字:
import javax.swing.JTextField;
public class addnum extends Thread {
private int lastNum;
private JTextField field;
public addnum(int n, JTextField field) {
lastNum = n;
this.field = field;
}
@Override
public void run() {
// TODO Auto-generated method stub
String s = "";
for(int i = 1; i <= lastNum; i++) {
s+=i;
this.field.setText(s);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
}
追加字符:
import javax.swing.JTextField;
public class addchar extends Thread {
private char charToPrint; private int times;
private JTextField field;
public addchar(char c, int t, JTextField field) {
charToPrint = c; times = t; this.field = field;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
// TODO: handle exception
}
String s = "";
for(int i = 0; i < times; i++) {
s+=charToPrint; this.field.setText(s);
try { Thread.sleep(100);
}
catch (InterruptedException e) {
// TODO: handle exception }
}
}
}