一个加秒的小程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TimeTest extends JApplet implements ActionListener {
private Time time;
private JLabel hourLabel, minuteLabel, secondLabel;
private JTextField hourField ,minuteField ,secondField,displayField;
private JButton tickButton;
public void init(){
time=new Time();
Container container= getContentPane();
container.setLayout(new FlowLayout());
hourLabel = new JLabel("Set Hour");
hourField = new JTextField(10);
container.add (hourLabel);
container.add (hourField);
minuteLabel= new JLabel("Set Minute");
minuteField=new JTextField(10);
container.add(minuteLabel);
container.add(minuteField);
secondLabel= new JLabel("Set Second");
secondField= new JTextField(10);
container.add(secondLabel);
container.add(secondField);
displayField=new JTextField(30);
displayField.setEditable(false);
container.add(displayField);
tickButton = new JButton("Add 1 to second ");
container.add(tickButton);
hourField.addActionListener(this);
minuteField.addActionListener(this);
secondField.addActionListener(this);
tickButton.addActionListener(this);
displayTime();
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==tickButton)
tick();
else if (event.getSource()==hourField){
time.setHour(Integer.parseInt(event.getActionCommand()));
hourField.setText("");
}
else if(event.getSource()==minuteField){
time.setMinute(Integer.parseInt(event.getActionCommand()));
minuteField.setText("");
}
else if(event.getSource()==secondField){
time.setSecond(Integer.parseInt(event.getActionCommand()));
secondField.setText("");
}
displayTime();
}
public void displayTime(){
displayField.setText("Hour: "+time.getHour()+ ";Minute: "+time.getMinute()+";Second: "+time.getSecond());
showStatus("Standard time is: "+time.toStandardString()+";Universal time is: "+time.toUniversalString());
}
public void tick(){
time.setSecond((time.getSecond()+1)%60);
if(time.getSecond()==0){
time.setMinute((time.getMinute()+1)%60);
if(time.getMinute()==0){
time.setHour((time.getHour()+1)%24);
}
}
}
}
import java.text.DecimalFormat;
/**
* Created by End on 15/12/12.
*/
public class Time {
private int hour ;
private int minute;
private int second ;
public Time (){
this(0,0,0);
}
public Time (int h,int m,int s){
setTime(h,m,s);
}
public void setTime(int h,int m,int s){
setHour(h);
setMinute(m);
setSecond(s);
}
public void setHour(int h){
hour=((h>=0&&h<24)?h:0);
}
public void setMinute(int m){
minute=((m>=0&&m<60)?m:0);
}
public void setSecond(int s){
second=((s>=0&&s<60)?s:0);
}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
public int getSecond(){
return second;
}
public String toUniversalString(){
DecimalFormat twoDights=new DecimalFormat("00");
return twoDights.format(hour)+":"+twoDights.format(minute)+":"+twoDights.format(second);
}
public String toStandardString(){
DecimalFormat twoDights=new DecimalFormat("00");
return ((hour==12||hour==0)?12:hour%12)+":"+twoDights.format(minute)+":"+twoDights.format(second)+(hour<12 ? "AM" : "PM" );
}
}
“`
“`