📄 counter2.java
字号:
import java.awt.*;import java.awt.event.*;import java.applet.*;class SeparateSubTask extends Thread { private int count = 0; private Counter2 c2; private boolean runFlag = true; public SeparateSubTask(Counter2 c2) { this.c2 = c2; start(); } public void invertFlag() { if(runFlag) { runFlag = !runFlag; } } public void run() { while(true) { try { sleep(100); } catch(InterruptedException e) { } if(runFlag) { c2.t.setText(Integer.toString(count++)); } } }}//以下内容,引用自 Counterpublic class Counter2 extends Applet { //private int count = 0; private SeparateSubTask sst = null; private Button onOff = new Button("Stop count"), start = new Button("Start count"); TextField t = new TextField(10); //private boolean runFlag = true; public void init() { add(t); start.addActionListener(new StartL()); add(start); onOff.addActionListener(new OnOffL()); add(onOff); } /** * public void go() { while(true) { try { Thread.currentThread().sleep(100); } catch(InterruptedException ie) { if(runFlag) { t.setText(Integer.toString(count++)); } } } } */ class StartL implements ActionListener { public void actionPerformed(ActionEvent e) { //已经不采用 go() 方法了; if(sst == null) { sst = new SeparateSubTask(Counter2.this); } } } class OnOffL implements ActionListener { public void actionPerformed(ActionEvent e) { if(sst != null) { sst.invertFlag(); } sst = null; } } public static void main(String[] args) { Counter2 applet = new Counter2(); Frame aFrame = new Frame("Counter"); aFrame.addWindowListener (new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); aFrame.add(applet, BorderLayout.CENTER); aFrame.setSize(300, 200); applet.init(); applet.start(); aFrame.setVisible(true); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -