TThread0.java
Select all
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TThread0 extends JFrame { JButton btnStart, btnClear; JLabel lblNum; int count = 0; public static void main(String[] args) { TThread0 app = new TThread0(); app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); app.init(); app.pack(); app.setVisible(true); } public void init() { Container c = this.getContentPane(); c.setLayout(new GridLayout(3,1)); lblNum = new JLabel("Running Total"); btnStart = new JButton("Start"); btnClear = new JButton("Clear"); btnClear.setEnabled(false); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { btnStart.setEnabled(false); btnClear.setEnabled(true); doCount(); } }); btnClear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { count = 0; lblNum.setText(Integer.toString(count++)); btnStart.setEnabled(true); btnClear.setEnabled(false); } }); c.add(lblNum); c.add(btnStart); c.add(btnClear); } private void doCount() { while (count < 10000) { // next line not reflected until end lblNum.setText(Integer.toString(count++)); // the following line is used to show activity System.out.println(Integer.toString(count)); } } }