当前位置: 代码迷 >> Eclipse >> 编写一个Java小程序,该如何处理
  详细解决方案

编写一个Java小程序,该如何处理

热度:19   发布时间:2016-04-23 14:11:07.0
编写一个Java小程序
利用Runnable 接口实现多线程,编写一个Java小程序。在屏幕上显示时间,每隔一秒钟刷新一次。为使小程序不影响其他程序的运行,使用了多线程

------解决方案--------------------
Java code
import java.awt.*;import java.awt.event.*;import java.text.SimpleDateFormat;import java.util.*;import javax.swing.*;import javax.swing.border.*;public class TestTimePieceJFrame extends JFrame {    ControlPanel controlPane = null; //-----------------控制面板    JTextField jTextField;    public TestTimePieceJFrame() {    super();    init();    }    private void init() {    controlPane = new ControlPanel(this);    add(controlPane);    setPreferredSize(new Dimension(300, 170));    pack();    setVisible(true);    setResizable(false);    this.setLocationRelativeTo(null);    this.setDefaultCloseOperation(EXIT_ON_CLOSE);    addKeyListener(new TimePieceKeyListener());    }    public static void main(String[] args) {    new TestTimePieceJFrame();    ;    }    class TimePieceKeyListener extends KeyAdapter {    public void keyReleased(KeyEvent e) {        controlPane.keyReleased(e);    }    }}class ControlPanel extends JPanel {    private JFrame snakeFrame = null;    JLabel jLabel,jLabel1, jLabel2, jLabel3, jLabel4;    Date dateWhenStartJpanel;//保存程序启动时的时间        Date dateWhenClickF5;    Thread thread ;    public ControlPanel(JFrame snakeFrame) {    super();    this.snakeFrame = snakeFrame;    init();    }    private void init() {    dateWhenStartJpanel = new Date();    setSize(608, 31);    setBackground(Color.WHITE);    setLayout(new FlowLayout());    setBorder(new LineBorder(Color.white, 5));    setLayout(new FlowLayout());    jLabel = new JLabel();    jLabel1 = new JLabel();    jLabel2 = new JLabel();    jLabel3 = new JLabel();    jLabel4 = new JLabel();    add(jLabel);    add(jLabel1);    add(jLabel2);    add(jLabel3);    add(jLabel4);    jLabel.setText("F5:开始计时 F6:停止计时");    }      public void keyReleased(KeyEvent e) {    int keyCode = e.getKeyCode();    if (keyCode == KeyEvent.VK_F5) {        dateWhenClickF5 = new Date();//sava time when click F5        thread = new Thread(new TimePieceRunnable())        ;thread.start();    } else if (keyCode == KeyEvent.VK_F6) {        if(thread != null)thread.stop();        else prompt("请先按F5");    }    }    class TimePieceRunnable implements Runnable {    public void run() {        while (true) {        Date tempDate = new Date();        String tempString = formateToTime(tempDate, "现在时间是 E kk:mm:ss:");        jLabel1.setText(tempString);        tempString = formateToTime(dateWhenStartJpanel,            "启动程序时间是 E kk:mm:ss:");        jLabel2.setText(tempString);                tempString = formateToTime(dateWhenClickF5,            "最近一次按动F5时间是 E kk:mm:ss:");        jLabel3.setText(tempString);        tempDate = getMillisOfTimeDifference(dateWhenClickF5, tempDate);                tempString = formateToTime(tempDate,            "最近一次按动F5到现在的时间差是 kk:mm:ss:");        jLabel4.setText(tempString);        try {                       repaint();            Thread.sleep(100);////每隔0.1秒刷新一次        } catch (InterruptedException e) {                     e.printStackTrace();        }        }    }    }    public String formateToTime(Date date, String timeRegex) {    SimpleDateFormat f = new SimpleDateFormat(timeRegex);    String newTypeDate = f.format(date);    return newTypeDate;    }    public String convertMillis(String millis){    return ((Long)(Long.parseLong(millis)*60/1000)).toString();    }     public String formateToTime(Date date) {// 将Date格式化,以String返回    Calendar calendar = Calendar.getInstance();    calendar.setTime(date);    SimpleDateFormat f = new SimpleDateFormat("kk:mm:ss:SSS");    String temp = f.format(date);    String newTypeDate =  temp.substring(0,9)+ (int)(Integer.parseInt(temp.substring(9)) * 60/1000);    return newTypeDate;    }          public Date getMillisOfTimeDifference(Date date1, Date date2) {    Calendar calendar = Calendar.getInstance();    calendar.setTime(date1);    long timelnMillis1 = calendar.getTimeInMillis();    calendar.setTime(date2);    long timelnMillis2 = calendar.getTimeInMillis();    calendar.setTimeInMillis(timelnMillis2 - timelnMillis1);    return calendar.getTime();    }         private static int prompt(String promptMessage) {    return JOptionPane.showConfirmDialog(null, promptMessage, "友情提示",        JOptionPane.WARNING_MESSAGE);    }  }
  相关解决方案