秒表的个个位数的计算:
package Seconds; public class Watch_time extends Thread{ int ms,ms_1,ms_2; int s,s_1; int m,m_1; Watch w; public void set(Watch w) { this.w=w; } public void setNum() { ms=ms_1= s=s_1=m=m_1=0; } public void run() { while(true) { try { Thread.sleep(9); ms_1++; if(ms_1==10) { ms_1=0; ms++; } if(ms==10) { ms=0; s_1++; } if(s_1==10) { s_1=0; s++; } if(s==6) { s=0; m_1++; } if(m_1==10) { m_1=0; m++; } w.textArea.setText(m+""+m_1+":"+s+""+s_1+"."+ms+""+ms_1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
Frame类:
可视化界面
package Seconds; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextArea; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JScrollPane; import java.awt.SystemColor; import java.awt.Font; import javax.swing.UIManager; import javax.swing.LayoutStyle.ComponentPlacement; import org.eclipse.wb.swing.FocusTraversalOnArray; import java.awt.Component; import java.awt.Color; public class Watch extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; JTextArea textArea = new JTextArea(); int a=1; int i=0; public Watch(Watch_time w) { setTitle("\u79D2\u8868\u8BA1\u65F6\u5668 --\u00A9Dong"); textArea.setText("00:00.00"); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setEnabled(false); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JTextArea textArea_1 = new JTextArea(); textArea_1.setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 13)); textArea_1.setEditable(false); textArea_1.setBackground(UIManager.getColor("Button.background")); JTextArea textArea_2 = new JTextArea(); textArea_2.setFont(UIManager.getFont("Viewport.font")); textArea_2.setEditable(false); textArea_2.setText(" \u51C6\u5907\u8BA1\u65F6"); textArea_2.setBackground(SystemColor.control); JButton btnNewButton=new CircleButton("开始",Color.GREEN); btnNewButton.setBackground(Color.CYAN); JButton btnNewButton_1=new CircleButton("\u8BB0\u5F55",Color.GREEN); JButton btnNewButton_2=new CircleButton("\u91CD\u7F6E",Color.GREEN); btnNewButton.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e) { if(btnNewButton.getText().equals("开始")) { if(a==1) { w.start(); textArea_2.setText(" 正在计时"); btnNewButton_1.setEnabled(true); btnNewButton_2.setEnabled(false); btnNewButton.setText("暂停"); a=2; } else { textArea_2.setText(" 正在计时"); btnNewButton_1.setEnabled(true); btnNewButton_2.setEnabled(false); w.resume(); btnNewButton.setText("暂停"); } } else { textArea_2.setText(" 暂 "+" 停"); btnNewButton_1.setEnabled(false); btnNewButton_2.setEnabled(true); w.suspend(); btnNewButton.setText("开始"); } } }); btnNewButton_1.setEnabled(false); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textArea_1.append("第"+(++i)+"次计数:"+textArea.getText()+'\n'); } }); btnNewButton_2.setEnabled(false); btnNewButton_2.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e) { textArea_1.setText(""); i=0; btnNewButton_2.setEnabled(false); textArea.setText("00:00.00"); textArea_2.setText(" 准备计时"); w.suspend(); w.setNum(); } }); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE) .addGroup(gl_contentPane.createSequentialGroup() .addGap(48) .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE) .addGap(38) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE) .addGap(44) .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup() .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE) .addContainerGap(134, Short.MAX_VALUE)) .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup() .addComponent(textArea_2, GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE) .addGap(178)))) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE) .addGap(33) .addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(textArea_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(15) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE) .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE) .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)) .addGap(44)) ); textArea.setEditable(false); textArea.setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 16)); textArea.setBackground(SystemColor.control); scrollPane.setViewportView(textArea_1); contentPane.setLayout(gl_contentPane); contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{scrollPane, btnNewButton, textArea_1, btnNewButton_1, textArea, btnNewButton_2, textArea_2})); } }