利用javax.swing.Timer类设计并实现一个模拟秒表功能的应用程序。程序中显示不断递增的时间,同时包含允许用户启动和终止计时功能的代码,以及一个可将时间复位为0的按钮。
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.util.TimerTask;
import java.text.DecimalFormat;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Font;
public class stopWatch extends JPanel{
private JLabel currentTimeLabel; //显示标签
private JButton startJButton; //开始按钮
private JButton stopJButton; //停止按钮
private JButton resetJButton; //复位按钮
private long countMis,countSec,countMin,countHour;//计时变量
private DecimalFormat textFormat=new DecimalFormat(\"00\");//格式化输出
Timer timer=new Timer(10,new TestActionListener());//计时单位10ms
public stopWatch() {
JPanel panel=new JPanel(new GridLayout(1,3,5,10)); //网格布局嵌入按钮
JPanel panel2=new JPanel();
currentTimeLabel=new JLabel(\" \");
TestActionListener actionListener=new TestActionListener();
currentTimeLabel.setForeground(Color.blue);
currentTimeLabel.setFont(new Font(\"SAN_SERIF\",Font.BOLD,50));
startJButton=new JButton(\"Start\");
stopJButton=new JButton(\"Stop\");
resetJButton=new JButton(\"Reset\");
//设置JButton相关属性
startJButton.setBorder(BorderFactory.createRaisedBevelBorder());
stopJButton.setBorder(BorderFactory.createRaisedBevelBorder());
resetJButton.setBorder(BorderFactory.createRaisedBevelBorder());
startJButton.setFont(new java.awt.Font(\"Times New Roman\", 1, 30));
stopJButton.setFont(new java.awt.Font(\"Times New Roman\", 1, 30));
resetJButton.setFont(new java.awt.Font(\"Times New Roman\", 1, 30));
stopJButton.setBackground(Color.cyan);
startJButton.setBackground(Color.red);
resetJButton.setBackground(Color.orange);
stopJButton.addActionListener(actionListener);
startJButton.addActionListener(actionListener);
resetJButton.addActionListener(actionListener);
this.setLayout(new BorderLayout());
panel2.setBackground(Color.gray);
panel2.setBorder(BorderFactory.createLoweredBevelBorder());
panel2.add(currentTimeLabel);
panel.add(stopJButton);
panel.add(startJButton);
panel.add(resetJButton);
this.add(panel2,BorderLayout.NORTH);
this.add(panel,BorderLayout.CENTER);
}
//处理相关事件
class TestActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==startJButton){
timer.start();
startJButton.setEnabled(false);
}
else if(e.getSource()==stopJButton){
timer.stop();
startJButton.setEnabled(true);
}
else if(e.getSource()==resetJButton){
countHour=0;
countMin=0;
countSec=0;
countMis=0;
}
else{//满位后复位
countMis++;
if(countMis>=99){
countSec++;
countMis=0;
if(countSec>=59){
countMin++;
countSec=0;
if(countMin>=59){
countHour++;
countMin=0;
}
}
}
}
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
currentTimeLabel.setText(textFormat.format(countHour)+\":\"+textFormat.format(countMin)+
\":\"+textFormat.format(countSec)+\":\"+textFormat.format(countMis));
repaint();
}
public static void main(String args[]){
JFrame frame=new JFrame(\"秒表演示\");
stopWatch stopwatch=new stopWatch();
frame.setSize(480,280);
frame.getContentPane().add(stopwatch);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!
猪小侠源码-最新源码下载平台 PHP教程 java实现秒表功能 http://www.20zxx.cn/585766/xuexijiaocheng/qes.html
猪小侠源码,优质资源分享网
相关文章
- java非法字符‘\\ufeff‘解决方法 2024-03-11
- Java中单体应用锁的局限性&分布式锁 2024-03-11
- 如何通过php函数解决页面渲染慢的问题? 2024-03-11
- 如何评估php性能优化函数的效果? 2024-03-11
- 如何利用PHP脚本在Linux中进行目录操作 2024-03-11
- 如何通过PHP脚本在Linux中进行系统监测 2024-03-11
- 如何使用php函数来优化表单处理和提交功能? 2024-03-11
- 如何通过PHP脚本在Linux服务器上实现数据加密 2024-03-11
- 如何通过php函数来优化验证码生成和校验? 2024-03-11
- 如何使用php函数来优化多语言支持功能? 2024-03-11
做猪小侠源码的代理,提供一站式服务
如果你不懂得搭建网站或者服务器,小程序,源码之类的怎么办? 第一通过本站学习各种互联网的技术 第二就是联系客服,我帮帮你搭建(当然要收取部分的费用) 第三成为我们的代理,我们提供整套的服务。