JAVA需要一条秒表计时器代码
1.需要计时器窗口有置顶功能2.能显示时分秒3.没有“清零”按钮,仅有开始和暂停按钮,并且能使用快捷键使用。...
1.需要计时器窗口有置顶功能
2.能显示时分秒
3.没有“清零”按钮,仅有开始和暂停按钮,并且能使用快捷键使用。 展开
2.能显示时分秒
3.没有“清零”按钮,仅有开始和暂停按钮,并且能使用快捷键使用。 展开
2个回答
展开全部
好吧,已看到你的评论,我在这里再回答一次:
1)你所说的置顶如果是属于悬浮窗效果,那么JFrame实例化后,再添加一行如下的代码:
form1.setAlwaysOnTop(true);// 总是允许窗口置顶
2)时分秒更简单了,除一除转转换就行了,没有技术含量。
3)快捷键通过JButton类的setMnemonic方法实现
So,综上,整个程序的实现算法如下:
package hky.example;
import 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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询