秒表类的实现:
最近为做期末最后的Java大作业做准备,其中有一个实现秒表,看着挺简单的,结果为了设计布局花费了我
一个上午的时间才整出来,其中秒表类中的核心代码参考了网上的代码。
代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.border.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class TimeCounter extends JFrame{
private Timer timer=new Timer();
private JLabel timeRun_label = new JLabel("00 : 00 : 00"); //用于显示秒表的时间
private JLabel reset_label = new JLabel("Reset"); //重置
private JLabel pause_start = new JLabel("Start"); //暂停/开始
private JButton left_button,right_button; //用于重置、暂停/开始的按钮组件
private static Icon[] icons; //为按钮装饰的图片
private boolean is_pause=true;
private timetask mytimetask=new timetask();
/*
minutes1、minutes2分别为分的十位数和个位数
second1、second2分别为秒的十位数和个位数字
millisecond1、millisecond2分别是毫秒的十位数字和个位数字
*/
private int minutes1,minutes2,second1,second2,millisecond1,millisecond2;
private String date;
public TimeCounter(){
//Container contentPane = getContentPane();
//contentPane.setLayout(null);
icons = new Icon[]{
new ImageIcon(getClass().getResource("reset.jpg")), //图片放在.class文件所在目录下
new ImageIcon(getClass().getResource("pause.jpg")),
new ImageIcon(getClass().getResource("start.jpg")),
};
setLayout(null);
left_button = new JButton(icons[0]); //为按钮组件装饰图片
right_button = new JButton(icons[2]);
timeRun_label.setSize(130,30); //设置各空间的大小
left_button.setSize(25,25);
right_button.setSize(25,25);
reset_label.setSize(35,15);
pause_start.setSize(45,15);
setSize(150,100);
timeRun_label.setLocation(10, 5); //设置各空间的位置或边框大小和颜色
timeRun_label.setBorder(new LineBorder(Color.BLUE,4));
left_button.setLocation(35, 40);
right_button.setLocation(95, 40);
reset_label.setLocation(30, 65);
pause_start.setLocation(90, 65);
left_button.addActionListener(new ActionListener(){ //为重置按钮控件添加监听器
public void actionPerformed(ActionEvent evt){
is_pause = true;
pause_start.setText("Start");
minutes1 = 0; second1 = 0; millisecond1 = 0;
minutes2 = 0; second2 = 0; millisecond2 = 0;
date=" " +minutes1 + minutes2 + " : " + second1 + second2 + " : "+ millisecond1 + millisecond2;
timeRun_label.setText(date);
}
});
right_button.addActionListener(new ActionListener(){ //位暂停/开始按钮控件添加监听器
public void actionPerformed(ActionEvent evt){
if(is_pause){
is_pause = false;
pause_start.setText("Pause");
right_button.setIcon(icons[1]);
}
else{
is_pause = true;
pause_start.setText("Start");
right_button.setIcon(icons[2]);
}
}
});
timeRun_label.setHorizontalAlignment(SwingConstants.CENTER);
add(pause_start); //在JPanel中添加控件
add(timeRun_label);
add(left_button);
add(right_button);
add(reset_label);
add(pause_start);
setDefaultCloseOperation(EXIT_ON_CLOSE);
timer.scheduleAtFixedRate(mytimetask, 0, 10); //设置秒表的速度
setVisible(true);
}
class timetask extends TimerTask{ //timetask类
private int count_time=0;
//private String date;
@Override
public void run() {
// TODO Auto-generated method stub
if(!is_pause){
count_time++;
minutes1 = (count_time/60000)%10;
minutes2 = (count_time/6000)%10;
second1 = (count_time/1000)%6;
second2 = (count_time/100)%10;
millisecond1 = (count_time/10)%10;
millisecond2 = count_time%10;
date=" " + minutes1 + minutes2 + " : " + second1 + second2 + " : " + millisecond1 + millisecond2;
timeRun_label.setText(date);
}
}
}
public static void main(String args[]){
new TimeCounter();
}
}
秒表类中用到的主要的类:三个JLabel类,分别用于显示时间、“Reset”、“Pause/Start”,
两个JButton类,分别用于重置时间和暂停/继续按钮的操作
Time类和TimeTask类,个人认为秒表类的核心代码,设置秒表跑动
Icon类和ImageIcon类,为JButton类添加图片效果