import javax.swing.*
import java.awt.HeadlessException
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.Font
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
public class Timer extends JFrame {
private static final long serialVersionUID = 1L
private static final String INITIAL_LABEL_TEXT = "00:00:00 000"
// 计数线程
private CountingThread thread = new CountingThread()
// 记录程序开始时间
private long programStart = System.currentTimeMillis()
// 程序旁并戚一开始就是暂停的蔽尘
private long pauseStart = programStart
// 程序暂停的总时间
private long pauseCount = 0
private JLabel label = new JLabel(INITIAL_LABEL_TEXT)
private JButton startPauseButton = new JButton("开始")
private JButton resetButton = new JButton("清零")
private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart)
thread.stopped = false
startPauseButton.setText("暂停")
} else {
pauseStart = System.currentTimeMillis()
thread.stopped = true
startPauseButton.setText("继续")
}
}
}
private ActionListener resetButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart
pauseCount = 0
thread.stopped = true
label.setText(INITIAL_LABEL_TEXT)
startPauseButton.setText("开始")
}
}
public Timer(String title) throws HeadlessException {
super(title)
setDefaultCloseOperation(EXIT_ON_CLOSE)
setLocation(300, 300)
setResizable(false)
setupBorder()
setupLabel()
setupButtonsPanel()
startPauseButton.addActionListener(startPauseButtonListener)
resetButton.addActionListener(resetButtonListener)
thread.start()// 计数线程运陵一直就运行着
}
// 为窗体面板添加边框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout())
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5))
this.setContentPane(contentPane)
}
// 配置按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout())
panel.add(startPauseButton)
panel.add(resetButton)
add(panel, BorderLayout.SOUTH)
}
// 配置标签
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER)
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40))
this.add(label, BorderLayout.CENTER)
}
// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} catch (Exception e) {
e.printStackTrace()
}
Timer frame = new Timer("计时器")
frame.pack()
frame.setVisible(true)
}
private class CountingThread extends Thread {
public boolean stopped = true
private CountingThread() {
setDaemon(true)
}
@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount
label.setText(format(elapsed))
}
try {
sleep(1) // 1毫秒更新一次显示
} catch (InterruptedException e) {
e.printStackTrace()
System.exit(1)
}
}
}
// 将毫秒数格式化
private String format(long elapsed) {
int hour, minute, second, milli
milli = (int) (elapsed % 1000)
elapsed = elapsed / 1000
second = (int) (elapsed % 60)
elapsed = elapsed / 60
minute = (int) (elapsed % 60)
elapsed = elapsed / 60
hour = (int) (elapsed % 60)
return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli)
}
}
}
你可以试试,希望能帮到你!
javaSystem.currentTimeMillis() 就是获取当前的毫秒数开始时尺腊记录 istart= System.currentTimeMillis()
结束时 记录陵型滑 iend= System.currentTimeMillis();
分钟就是 Math.round((iend-istart)/(60*1000))
那 秒数 就是 Math.round((iend-istart)/1000)%60
再开启一个定时器,定时获取 itmp= System.currentTimeMillis();计算分钟和秒数 显示出来
显示动态在跳得秒和租伍分
好吧,已看到你的评论,我在这里再回答一次:
1)你所说的置顶如果是属于悬浮窗效果,那么JFrame实例化后,再添加一行如下的代码:
form1.setAlwaysOnTop(true)// 总是允许窗口置顶2)时分秒更简单了,除一除转转换就行了,没有技术含量。
3)快捷键通过JButton类的setMnemonic方法实现
So,综上,整个程序的实现算法如下:
package hky.exampleimport java.awt.BorderLayout
import java.awt.Container
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent
import java.awt.event.WindowEvent
import java.awt.event.WindowListener
import java.io.*
import java.util.*
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JTextField
import javax.swing.plaf.OptionPaneUI
public class Demo{
static boolean isRuning=false
static boolean isFirst=true
static Integer hmsCounter=0
static int hour,minute,second
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
JFrame form1 = new JFrame("Form1")
form1.setAlwaysOnTop(true)// 1)总是允许窗口置顶
JTextField jTextField = new JTextField(10)
jTextField.setSize(10, 10)
jTextField.setText("0")
jTextField.setEditable(false)
JButton jButton = new JButton("开始")
jButton.setSize(10, 10)
Thread thread = new Thread(new Runnable() {
@Override
举者 困答笑 public void run() {
while (true) {
汪含 while(isRuning){
++hmsCounter
//3)时分秒显示
hour=hmsCounter/3600
minute=hmsCounter%3600/60
second=hmsCounter%60
jTextField.setText(hour+"时"+minute+"分"+second+"秒")
try {Thread.sleep(1000)} catch (Exception e2) {}
}
try {Thread.sleep(200)} catch (Exception e2) {}// 修复上一次回答的版本可能会存在的Bug
}
}
})
jButton.setMnemonic(KeyEvent.VK_ENTER)// 2)给JButton发送 Alt+Enter快捷键
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text=jButton.getText().equals("开始")?"暂停":"开始"
jButton.setText(text)
isRuning=!isRuning
if(isFirst){
thread.start()
isFirst=false
}
}
})
JPanel panel = new JPanel()
panel.setSize(200, 200)
panel.add(jTextField, BorderLayout.NORTH)
panel.add(jButton, BorderLayout.CENTER)
form1.add(panel)
form1.setBounds(200, 100, 250, 150)
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
form1.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// 窗口关闭前取出文本框的数字保存到外部文件,代码在此处写
JOptionPane.showMessageDialog(null, "Are you sure closing?")
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
})
form1.setVisible(true)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)