进程的实践与练习2
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(); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步