public class Test extends JFrame{
Thread threadA;
public Test() {
// TODO Auto-generated constructor stub
Container c = getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300, 300, 300, 300);
setLayout(new FlowLayout());
JButton jb1 = new JButton("开始");
JButton jb2 = new JButton("结束");
threadA = new Thread(new Runnable() {
public void run() {
while (true) {
System.out.println("123456123");
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
});
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
threadA.start();
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
threadA.stop();
}
});
c.add(jb1);
c.add(jb2);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}