帮忙解释一个Java小程序(秒表)
我是一名Java新手,在网上找了一个秒表小程序,哪位大哥帮忙解释下这些代码的意思。添加些注释 谢谢了packagecom.sofmit.tool;importj...
我是一名Java新手,在网上找了一个秒表小程序,哪位大哥帮忙解释下这些代码的意思。添加些注释 谢谢了package com.sofmit.tool;import javax.swing.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.*;import java.util.Date;import java.text.SimpleDateFormat;public class StopWatch extends JFrame { JButton btnStart, btnStop; JLabel label; Timer timer; public StopWatch() { label = new JLabel("00:00:00.000"); btnStart = new JButton("start"); btnStop = new JButton("stop"); final int delay = 100; final Date startTime = new Date(); final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S"); final Action taskPerformer = new AbstractAction() { public void actionPerformed(ActionEvent evt) { //显示时间 Date d = new Date(System.currentTimeMillis() - startTime.getTime() - 28800000); label.setText(sdf.format(d)); label.repaint(); } }; btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { startTime.setTime(new Date().getTime()); timer = new Timer(delay, taskPerformer); timer.start(); } }); btnStop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if (timer != null && timer.isRunning()) timer.stop(); } }); Container c = getContentPane(); c.add(label, BorderLayout.NORTH); c.add(btnStart, BorderLayout.CENTER); c.add(btnStop, BorderLayout.SOUTH); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private static void createAndShowGUI() { StopWatch window = new StopWatch(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.pack(); window.setVisible(true); }}
展开
展开全部
package com.sofmit.tool;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;import java.util.Date;
import java.text.SimpleDateFormat;
public class StopWatch extends JFrame {
JButton btnStart, btnStop;
JLabel label; Timer timer;
public StopWatch() {
label = new JLabel("00:00:00.000");
btnStart = new JButton("start");
btnStop = new JButton("stop");
final int delay = 100;
final Date startTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt)
{ //显示时间
Date d = new Date(System.currentTimeMillis() - startTime.getTime() - 28800000); label.setText(sdf.format(d)); label.repaint();
}
};
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer = new Timer(delay, taskPerformer); timer.start(); } });
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (timer != null && timer.isRunning())
timer.stop();
} });
Container c = getContentPane();
c.add(label, BorderLayout.NORTH);
c.add(btnStart, BorderLayout.CENTER);
c.add(btnStop, BorderLayout.SOUTH); }
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); } }); }
private static void createAndShowGUI() { StopWatch window = new StopWatch(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.pack(); window.setVisible(true); }}
你下载个java文档,中文版的,或者先看看孙鑫的java无难事,这是给你最好的思路!
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;import java.util.Date;
import java.text.SimpleDateFormat;
public class StopWatch extends JFrame {
JButton btnStart, btnStop;
JLabel label; Timer timer;
public StopWatch() {
label = new JLabel("00:00:00.000");
btnStart = new JButton("start");
btnStop = new JButton("stop");
final int delay = 100;
final Date startTime = new Date();
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt)
{ //显示时间
Date d = new Date(System.currentTimeMillis() - startTime.getTime() - 28800000); label.setText(sdf.format(d)); label.repaint();
}
};
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer = new Timer(delay, taskPerformer); timer.start(); } });
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (timer != null && timer.isRunning())
timer.stop();
} });
Container c = getContentPane();
c.add(label, BorderLayout.NORTH);
c.add(btnStart, BorderLayout.CENTER);
c.add(btnStop, BorderLayout.SOUTH); }
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); } }); }
private static void createAndShowGUI() { StopWatch window = new StopWatch(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.pack(); window.setVisible(true); }}
你下载个java文档,中文版的,或者先看看孙鑫的java无难事,这是给你最好的思路!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import javax.swing.*;
import java.awt.event.
ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/** 导包 **/
public class test1 extends JFrame {
JButton btnStart, btnStop;
JLabel label;
Timer timer;
public test1() {
label = new JLabel("00:00:00.000"); //初始化一个标签显示00:00:00.000也就是秒表在没有开始的时候显示
btnStart = new JButton("start"); //开始按钮
btnStop = new JButton("stop"); //停止按钮
final int delay = 1000;
final Date startTime = new Date(); //得到当前日期
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S"); //日期格式
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { //显示时间
Date d = new Date(System.currentTimeMillis() - startTime.getTime() - 28800000); //要显示的日期运算
label.setText(sdf.format(d)); label.repaint(); } }; //给标签赋值
btnStart.addActionListener(new ActionListener() { //按钮定义方法
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer = new Timer(delay, taskPerformer);//在delay/1000秒后执行taskPerformer任务
timer.start(); //开始timer
}
});
btnStop.addActionListener(new ActionListener() { //按钮定义方法
public void actionPerformed(ActionEvent evt) {
if (timer != null && timer.isRunning())
timer.stop(); //停止timer
}
});
Container c = getContentPane(); //得到窗口中的顶级容器
c.add(label, BorderLayout.NORTH); //
c.add(btnStart, BorderLayout.CENTER); //
c.add(btnStop, BorderLayout.SOUTH);
}
public static void main(String[] args) {
//启动程序
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {createAndShowGUI();}
});
}
private static void createAndShowGUI() {
test1 window = new test1();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序。仅在应用程序中使用。
window.pack(); //验证这个视窗的所有子元件
window.setVisible(true); //窗口显示
}
}
import javax.swing.*;
import java.awt.event.
ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/** 导包 **/
public class test1 extends JFrame {
JButton btnStart, btnStop;
JLabel label;
Timer timer;
public test1() {
label = new JLabel("00:00:00.000"); //初始化一个标签显示00:00:00.000也就是秒表在没有开始的时候显示
btnStart = new JButton("start"); //开始按钮
btnStop = new JButton("stop"); //停止按钮
final int delay = 1000;
final Date startTime = new Date(); //得到当前日期
final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S"); //日期格式
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt) { //显示时间
Date d = new Date(System.currentTimeMillis() - startTime.getTime() - 28800000); //要显示的日期运算
label.setText(sdf.format(d)); label.repaint(); } }; //给标签赋值
btnStart.addActionListener(new ActionListener() { //按钮定义方法
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer = new Timer(delay, taskPerformer);//在delay/1000秒后执行taskPerformer任务
timer.start(); //开始timer
}
});
btnStop.addActionListener(new ActionListener() { //按钮定义方法
public void actionPerformed(ActionEvent evt) {
if (timer != null && timer.isRunning())
timer.stop(); //停止timer
}
});
Container c = getContentPane(); //得到窗口中的顶级容器
c.add(label, BorderLayout.NORTH); //
c.add(btnStart, BorderLayout.CENTER); //
c.add(btnStop, BorderLayout.SOUTH);
}
public static void main(String[] args) {
//启动程序
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {createAndShowGUI();}
});
}
private static void createAndShowGUI() {
test1 window = new test1();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使用 System exit 方法退出应用程序。仅在应用程序中使用。
window.pack(); //验证这个视窗的所有子元件
window.setVisible(true); //窗口显示
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询