Java Jframe重新启动中的计时器
嗨,我最近几天一直在努力,似乎无法解决这个问题,我想在Jframe中制作一个计时器,它将被重置并重新启动。我似乎得到错误的计时器已被取消,但无法解决这个问题,任何帮助将不胜感激。 这是我的密码Java Jframe重新启动中的计时器,java,swing,timer,jframe,restart,Java,Swing,Timer,Jframe,Restart,嗨,我最近几天一直在努力,似乎无法解决这个问题,我想在Jframe中制作一个计时器,它将被重置并重新启动。我似乎得到错误的计时器已被取消,但无法解决这个问题,任何帮助将不胜感激。 这是我的密码 package cubebuddy; import java.util.Timer; import java.util.TimerTask; public class One_timer extends javax.swing.JFrame { Timer timer= new Timer(
package cubebuddy;
import java.util.Timer;
import java.util.TimerTask;
public class One_timer extends javax.swing.JFrame {
Timer timer= new Timer();
String ms, s, m;
public One_timer() {
initComponents();
}
TimerTask myTimerTask = new TimerTask() {
@Override
public void run() {
ms = Timer_display_ms.getText();
s = Timer_display_s.getText();
m = Timer_display_m.getText();
Long MS_timer = Long.valueOf(ms);
Long S_timer = Long.valueOf(s);
Long M_timer = Long.valueOf(m);
if (ms.equals(Long.toString(99))) {
s = Long.toString(++S_timer);
Timer_display_s.setText(s);
}
if ("99".equals(Timer_display_ms.getText())) {
Timer_display_ms.setText("0");
} else {
ms = Long.toString(++MS_timer);
Timer_display_ms.setText(ms);
}
if (s.equals(Long.toString(60))) {
m = Long.toString(++M_timer);
Timer_display_m.setText(m);
Timer_display_s.setText("0");
}
}
};
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
OT_start_button = new javax.swing.JButton();
Timer_display_ms = new javax.swing.JTextField();
OT_stop_button = new javax.swing.JButton();
Timer_display_s = new javax.swing.JTextField();
Timer_display_m = new javax.swing.JTextField();
OT_Reset_button = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setBackground(new java.awt.Color(0, 153, 204));
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
OT_start_button.setText("Start");
OT_start_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OT_start_buttonActionPerformed(evt);
}
});
Timer_display_ms.setEditable(false);
Timer_display_ms.setBackground(new java.awt.Color(102, 102, 102));
Timer_display_ms.setFont(new java.awt.Font("sansserif", 0, 48)); // NOI18N
Timer_display_ms.setHorizontalAlignment(javax.swing.JTextField.CENTER);
Timer_display_ms.setText("0");
Timer_display_ms.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Timer_display_msActionPerformed(evt);
}
});
OT_stop_button.setText("Stop");
OT_stop_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OT_stop_buttonActionPerformed(evt);
}
});
Timer_display_s.setEditable(false);
Timer_display_s.setBackground(new java.awt.Color(102, 102, 102));
Timer_display_s.setFont(new java.awt.Font("sansserif", 0, 48)); // NOI18N
Timer_display_s.setHorizontalAlignment(javax.swing.JTextField.CENTER);
Timer_display_s.setText("0");
Timer_display_m.setEditable(false);
Timer_display_m.setBackground(new java.awt.Color(102, 102, 102));
Timer_display_m.setFont(new java.awt.Font("sansserif", 0, 48)); // NOI18N
Timer_display_m.setHorizontalAlignment(javax.swing.JTextField.CENTER);
Timer_display_m.setText("0");
OT_Reset_button.setText("Reset");
OT_Reset_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OT_Reset_buttonActionPerformed(evt);
}
});
jLabel1.setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Min");
jLabel2.setBackground(new java.awt.Color(0, 0, 0));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("sec");
jLabel3.setBackground(new java.awt.Color(0, 0, 0));
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("Mil sec");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Timer_display_m, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Timer_display_s, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(Timer_display_ms, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(OT_stop_button, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(OT_Reset_button, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
.addComponent(OT_start_button, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Timer_display_s)
.addComponent(Timer_display_ms, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Timer_display_m, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(OT_start_button, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(OT_stop_button, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(OT_Reset_button, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26))
);
pack();
}// </editor-fold>
private void OT_start_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
timer.schedule(myTimerTask, 0,1);
}
private void OT_stop_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
timer.cancel();
}
private void OT_Reset_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
m = "0";
s = "0";
ms = "0";
Timer_display_ms.setText(ms);
Timer_display_s.setText(s);
Timer_display_m.setText(m);
}
private void Timer_display_msActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(One_timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(One_timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(One_timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(One_timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new One_timer().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton OT_Reset_button;
private javax.swing.JButton OT_start_button;
private javax.swing.JButton OT_stop_button;
private javax.swing.JTextField Timer_display_m;
private javax.swing.JTextField Timer_display_ms;
private javax.swing.JTextField Timer_display_s;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
package-cubebuddy;
导入java.util.Timer;
导入java.util.TimerTask;
公共类One_计时器扩展javax.swing.JFrame{
定时器=新定时器();
字符串ms,s,m;
公共1_计时器(){
初始化组件();
}
TimerTask myTimerTask=新TimerTask(){
@凌驾
public void run(){
ms=Timer\u display\u ms.getText();
s=计时器\显示\ s.getText();
m=计时器显示\u m.getText();
长毫秒定时器=长数值(毫秒);
长S_定时器=长.值of(S);
长M_定时器=长数值(M);
if(ms.equals(Long.toString(99))){
s=长.toString(++s_定时器);
定时器显示设置文本;
}
如果(“99”.equals(Timer\u display\u ms.getText())){
定时器显示设置文本(“0”);
}否则{
ms=长.toString(++ms\u定时器);
定时器显示设置文本(毫秒);
}
如果(s.equals(Long.toString(60))){
m=长.toString(++m_定时器);
定时器显示设置文本(m);
定时器显示设置文本(“0”);
}
}
};
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
OT_start_button=newjavax.swing.JButton();
Timer_display_ms=newjavax.swing.JTextField();
OT_stop_button=newjavax.swing.JButton();
Timer_display_s=newjavax.swing.JTextField();
Timer_display_m=newjavax.swing.JTextField();
OT_Reset_button=newjavax.swing.JButton();
jLabel1=newjavax.swing.JLabel();
jLabel2=newjavax.swing.JLabel();
jLabel3=newjavax.swing.JLabel();
挫折背景(新java.awt.Color(0153204));
setCursor(新java.awt.Cursor(java.awt.Cursor.DEFAULT_Cursor));
addWindowListener(新java.awt.event.WindowAdapter(){
public void windowActivated(java.awt.event.WindowEvent evt){
窗体窗口激活(evt);
}
});
OT_start_按钮.setText(“开始”);
OT_start_button.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
OT_开始按钮已执行(evt);
}
});
定时器显示设置可编辑(假);
定时器显示设置背景(新java.awt.Color(102102102));
Timer_display_ms.setFont(新的java.awt.Font(“sansserif”,0,48));//NOI18N
计时器显示setHorizontalAlignment(javax.swing.JTextField.CENTER);
定时器显示设置文本(“0”);
计时器\u显示\u ms.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
定时器显示执行的操作(evt);
}
});
OT_stop_按钮.setText(“停止”);
OT\u stop\u button.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行OT_停止按钮操作(evt);
}
});
定时器显示设置可编辑(假);
定时器显示设置背景(新java.awt.Color(102102102));
Timer_display_s.setFont(新的java.awt.Font(“sansserif”,0,48));//NOI18N
计时器显示设置水平对齐(javax.swing.JTextField.CENTER);
定时器显示设置文本(“0”);
定时器显示设置可编辑(假);
定时器显示设置背景(新java.awt.Color(102102102));
Timer_display_m.setFont(新的java.awt.Font(“sansserif”,0,48));//NOI18N
计时器显示设置水平对齐(javax.swing.JTextField.CENTER);
定时器显示设置文本(“0”);
OT_Reset_按钮.setText(“重置”);
OT_Reset_button.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行OT_重置_按钮操作(evt);
}
});
setBackground(新java.awt.Color(0,0,0));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText(“最小值”);
setBackground(新java.awt.Color(0,0,0));
setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText(“sec”);
setBackground(新java.awt.Color(0,0,0));
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText(“Mil-sec”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(Timer\u display\u m,javax.swing.GroupLayout.PREFERRED\u SIZE,260,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(46,46,46)
.addGroup(layout.createParallelGroup(
private void OT_start_buttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
timer = new Timer();
MyTimerTask task = new MyTimerTask();
timer.schedule(task, 0, 1);
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
ms = Timer_display_ms.getText();
s = Timer_display_s.getText();
m = Timer_display_m.getText();
Long MS_timer = Long.valueOf(ms);
Long S_timer = Long.valueOf(s);
Long M_timer = Long.valueOf(m);
if (ms.equals(Long.toString(99))) {
s = Long.toString(++S_timer);
Timer_display_s.setText(s);
}
if ("99".equals(Timer_display_ms.getText())) {
Timer_display_ms.setText("0");
} else {
ms = Long.toString(++MS_timer);
Timer_display_ms.setText(ms);
}
if (s.equals(Long.toString(60))) {
m = Long.toString(++M_timer);
Timer_display_m.setText(m);
Timer_display_s.setText("0");
}
}
};