好吧,已看到你的评论,我在这里再回答一次:
10余年的朝阳县网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整朝阳县建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“朝阳县网站设计”,“朝阳县网站推广”以来,每个客户项目都认真落实执行。
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);
}
}
应该用线程里面的Timer来控制package com.sy.game.test;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTask {
public static void main(String[] args) {
TimeTask tTask=new TimeTask();
tTask.timeVoid();
}
public void timeVoid(){
final Timer timer = new Timer();
TimerTask tt=new TimerTask() {
@Override
public void run() {
System.out.println("到点啦!");
timer.cancel();
}
};
timer.schedule(tt, 3000);
}
}
整合的:
/*
* java倒计时器
* shiyang
* */
package com.sy.game.test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
@SuppressWarnings("unused")
public class TimeController extends JFrame implements ActionListener {
private static final long serialVersionUID = 4603262282860990473L;
private static final int DEFAULT_WIDTH = 200;
private static final int DEFAULT_HEIGHT = 100;
private static final int width = Toolkit.getDefaultToolkit()
.getScreenSize().width;
private static final int height = Toolkit.getDefaultToolkit()
.getScreenSize().height;
private Container container;
private JButton btn;
private JTextField jtfTime;
private Timer tmr;
public TimeController() {
initComponents();
Timer tmr = new Timer(1000, this);
this.tmr = tmr;
setVisible(true);
}
private void initComponents() {
this.setTitle("SY秒表");
this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation((width - DEFAULT_WIDTH) / 2,
(height - DEFAULT_HEIGHT) / 2);
jtfTime = new JTextField("10");
btn = new JButton("开始倒计时");
container = getContentPane();
JPanel panel = new JPanel();
panel.add(btn);
panel.add(jtfTime);
this.add(panel);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn) {
jtfTime.setText("10");
tmr.start();
} else {
int t;
t = Integer.parseInt(jtfTime.getText());
t--;
jtfTime.setText("" + t);
if (t = 0) {
tmr.stop();
}
}
}
public static void main(String[] args) {
TimeController timeController = new TimeController();
}
}
package demo;
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);
}
}
}
你可以试试,希望能帮到你!