一个加秒的小程序

一个加秒的小程序

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" );
    }
}

“`

“`这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值