一、项目简介
设计一个计时器,在输入区输入一个时间后自动变为时分秒计时,开始计时后显示已计时间与剩余时间
二、项目采用技术
Java Swing、Time
三、架构图
四、需求功能分析
本系统采用MVC架构设计,针对开会时使用。使用者在发言时可看到自己已发言时间与剩余发言时间,便于合理安排自己的发言时间,做好规划。暂停按钮可以使发言者中途暂停自己的时间;若提前结束发言,可点击停止来结束倒计时。
五、项目亮点
实现倒计时的基本功能;
界面良好,操作简单,易于上手;
可靠性,响应速度快。
六、主要功能截图
开始界面
运行时
暂停时
输错时
七、团队成员负责模块
团队成员 | 负责模块 |
杨少波 | 功能设计 |
李浩芃 | 界面设计 |
八、项目git地址
源代码:
public class Time {
public static void main(String[] args) {
TimeModel timeModel = new TimeModel();
TimeView timeView = new TimeView();
TimeController timeController = new TimeController(timeView, timeModel);
timeController.run();
}
}
TimeModel
public class TimeModel {
private long ms = 0;
private String sh;
private String sm;
private String ss;
public void init(String sm) {
long min = Long.parseLong(sm);
long h = min / 60;//时分秒
h = h < 0 ? 0 : h;
long m = min - h * 60 - 1;
m = m < 0 ? 0 : m;
long s = min == 0 ? 0 : 60;
this.sh = h + "";
this.sm = m + "";
this.ss = s + "";
}
public void update(String sh, String sm, String ss) {
long h = "".equals(sh) ? 0 : Long.parseLong(sh);
long m = "".equals(sm) ? 0 : Long.parseLong(sm);
long s = "".equals(ss) ? 0 : Long.parseLong(ss);
s--;
if ((h != 0 || m != 0) && s == 0) {
m--;
s = 59;
}
if (h != 0 && m == 0) {
h--;
m = 59;
}
h = h < 0 ? 0 : h;//? 和:相当于if和else 例(a%2==0)?a是偶数:a是奇数
m = m < 0 ? 0 : m;
s = s < 0 ? 0 : s;
this.sh = h + "";
this.sm = m + "";
this.ss = s + "";
}
public String getH() {
return sh;
}
public String getM() {
return sm;
}
public String getS() {
return ss;
}
public void incrementMs() {
this.ms++;
}
public String getPH() {
return ms / 60 / 60 + "";
}
public String getPM() {
return ms / 60 + "";
}
public String getPS() {
return ms % 60 + "";
}
public void resetMs() {
this.ms = 0;
}
}
TimeView
import javax.swing.*;
import java.awt.*;
public class TimeView extends JFrame {
private static final String[] NS =
{"会议发言计时器", "时", "分", "秒",
"计时总分钟数:",
"你已用:", "时", "分", "秒", "发言剩余时间:",
"开始倒计时", "暂停计时", "停止计时"};
private int index;
private final JTextField[] ts;
private final JButton[] bs;
public JTextField[] getTs() {
return ts;
}
public JButton[] getBs() {
return bs;
}
public TimeView() {
this.setTitle(NS[0]);
ts = new JTextField[7];
for (int i = 0; i < 4; i++) {//界面设计
ts[i] = new JTextField();
ts[i].setPreferredSize(new Dimension(55, 50));//setpreferredsize为设置最好的大小
ts[i].setFont(new Font("宋体", Font.BOLD, 30));//高宽 font为字体显示效果
ts[i].setHorizontalAlignment(SwingConstants.CENTER);//居中对齐
ts[i].setOpaque(true);//设置不透明
ts[i].setBackground(Color.CYAN);//背景颜色
ts[i].setForeground(Color.RED);//前景颜色
if (i != 3) {
ts[i].setEditable(false);//设置选项不可用
}
}
for (int i = 4; i < 7; i++) {
ts[i] = new JTextField();
ts[i].setPreferredSize(new Dimension(55, 60));
ts[i].setFont(new Font("宋体", Font.BOLD, 30));
ts[i].setHorizontalAlignment(SwingConstants.CENTER);//居中对齐
ts[i].setOpaque(true);
ts[i].setBackground(Color.BLUE);
ts[i].setForeground(Color.RED);
}
JLabel[] ls = new JLabel[10];
for (int i = 0; i < 10; i++) {
ls[i] = new JLabel(NS[index++]);//字体样式
ls[i].setForeground(Color.black);
ls[i].setFont(new Font("宋体", Font.BOLD, 30));
}
bs = new JButton[3];
for (int i = 0; i < 3; i++) {
bs[i] = new JButton(NS[index++]);
bs[i].setBackground(Color.BLUE);//背景
bs[i].setForeground(Color.YELLOW);//前景
}
JComponent[][] cs = {{ls[0]},
{ls[4], ts[3]},
{ls[9], ts[0], ls[1], ts[1], ls[2], ts[2], ls[3]},
{ls[5], ts[4], ls[6], ts[5], ls[7], ts[6], ls[8]},
{bs[0], bs[1], bs[2]}};
JPanel[] ps = new JPanel[5];//Jpanel面板
JPanel wrap = new JPanel();
wrap.setLayout(new BoxLayout(wrap, BoxLayout.Y_AXIS));//边界布局 Y_AXIS为Y坐标轴
for (int i = 0; i < 5; i++) {
ps[i] = new JPanel(new FlowLayout(FlowLayout.CENTER));//流式布局(从上到下、从左到右)居中
for (int j = 0; j < cs[i].length; j++) {
ps[i].add(cs[i][j]);
ps[i].setBackground(Color.YELLOW);//背景颜色
}
wrap.add(ps[i]);
}
this.add(wrap);
}
public void init() {
pack();
setResizable(true);//可自由调整大小
setLocationRelativeTo(null);//生成窗口置于屏幕中央
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit退出。使用Systemexit方法退出应用程序。仅在应用程序中使用。
setVisible(true);//显示GUI组件
}
}
TimeController
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
public class TimeController {
//控制器需要交互模型和视图。
private final TimeModel model;
private final TimeView view;
private Timer timer;
private boolean isPaused = false;
private final JTextField[] ts;
private final JButton[] bs;
public TimeController(TimeView view, TimeModel timeModel) {
this.view = view;
this.model = timeModel;
this.ts = view.getTs();
this.bs = view.getBs();
bs[0].addActionListener(new ButtonListenerOne());
bs[1].addActionListener(new ButtonListenerTwo());
bs[2].addActionListener(new ButtonListenerThree());
}
public void run() {
view.init();
}
class ButtonListenerOne implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)//actionprformed为如果你要求这个button做一些事情,你就可以在actionPerformed方法中写你要做的事情
{
try {
if (!isPaused) {
model.init(ts[3].getText());
ts[0].setText(model.getH());//发言剩余时间 输入框背景前景颜色设置
ts[0].setBackground(Color.BLUE);
ts[0].setForeground(Color.YELLOW);
ts[1].setText(model.getM());
ts[1].setBackground(Color.BLUE);
ts[1].setForeground(Color.YELLOW);
ts[2].setText(model.getS());
ts[2].setBackground(Color.BLUE);
ts[2].setForeground(Color.YELLOW);
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
long h = Long.parseLong(ts[0].getText());//字符串转数字
long m = Long.parseLong(ts[1].getText());//字符串转数字
long s = Long.parseLong(ts[2].getText());//字符串转数字
model.update(ts[0].getText(), ts[1].getText(), ts[2].getText());
ts[0].setText(model.getH());
ts[1].setText(model.getM());
ts[2].setText(model.getS());
model.incrementMs();
ts[4].setText(model.getPH());
ts[5].setText(model.getPM());
ts[6].setText(model.getPS());
if (h == 0 && m == 0 && s == 0) {
bs[2].doClick();
}
}
}, 0, 1000);
bs[0].setEnabled(false);//设置为false,按钮就不可点击
} catch (NumberFormatException nfe)//捕捉异常
{
JOptionPane.showConfirmDialog(view,//输入错误字符时,弹出“输入错误,请重新输入分钟的整数”
"输入错误,请重新输入分钟的整数。",
"友情提示",
JOptionPane.DEFAULT_OPTION,//对话框组件
//Java消息提示框JOptionPane的使用方法
JOptionPane.WARNING_MESSAGE);
}
}
}
class ButtonListenerTwo implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)//actionprformed为如果你要求这个button做一些事情,你就可以在actionPerformed方法中写你要做的事情
{
if (null != timer) {
timer.cancel();
timer = null;
}
bs[0].setEnabled(true);
isPaused = true;//暂停
}
}
class ButtonListenerThree implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)//actionprformed为如果你要求这个button做一些事情,你就可以在actionPerformed方法中写你要做的事情
{
if (null != timer) {
timer.cancel();
timer = null;
}
bs[0].setEnabled(true);
isPaused = false;//停止
model.resetMs();
}
}
}