本人java新手,刚学java没两个月,现在在写一个java的计时器,一般的计时器都是用按钮控制的,我要写的是用键盘代替按钮来控制计时器(不是在按钮上加keylistener).
    现在的问题是java计时器可以正常运行,但是不知道怎么替换按钮……
    在这种情况下用什么类来写,怎么写,最好能有一些简单的例子,谢谢众高手!!!

解决方案 »

  1.   

    直接在Frame中addListener就行了,addListener的用法可以查看api文档
      

  2.   

    在面版中增加键盘监听器this.addKeyListener();
       然后在监听器事件keyPressed方法里,用一个switch(getKeyCoke),得到相关键盘数子的Coke的值,让计算器的文本中显示相关的数子,就行了!
      

  3.   

    下面有相关的例子
    http://blog.csdn.net/baillluu/article/details/6751909#comments
      

  4.   


    import java.awt.*;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.*;import javax.swing.*;
    import javax.swing.border.*;public class TestTimePiece extends JFrame {
        ControlPanel controlPane = null; //-----------------控制面板
        JTextField jTextField;    public TestTimePiece() {
    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 TestTimePiece();
    ;
        }    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) {
    //Date date = new Date();
    //SimpleDateFormat f = new SimpleDateFormat("'BeiJing Time':yyyy年MM月dd日 E kk时mm分ss秒");
    SimpleDateFormat f = new SimpleDateFormat(timeRegex);
    String newTypeDate = f.format(date);
    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);
        }
      
    }
      

  5.   

    这种就用resume();suspend();吧,虽然过时了,但不涉及同步上锁的话还是可以用的。结合4#的再改改就行了,我很早写的...import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class CSDN3 extends JFrame  {
    TextField txt = new TextField();
    JButton b1 = new JButton("运行");
    JButton b2 = new JButton("挂起");
    JPanel jp = new JPanel();
    private boolean flag;
    count c=new count();
    Thread t=new Thread(c);
    public CSDN3() {

    this.setLocation(400, 300);
    this.setSize(300, 300);
    txt.setEnabled(false);
    txt.setText("0");
    this.add(txt, BorderLayout.NORTH);
    jp.setLayout(new FlowLayout());
    jp.add(b1);
    jp.add(b2);
    this.add(jp);
    this.add(jp, BorderLayout.SOUTH);
    this.setResizable(false);
    this.setVisible(true);
    t.start();
    b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    t.resume();
    }
    });
    b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    t.suspend();
        txt.setText("暂停中...");
    }
    });
    } public static void main(String[] args) {
    new CSDN3();
    }
    class count implements Runnable{
    public void run()
    {   flag=true;
    int i = 0;
    while(flag)
    {
    i++;
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    txt.setText( Integer.toString(i));
    }
    }
    }
    }