import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.SimpleDateFormat;
public class NewFrame extends JFrame
{
JLabel lab1=new JLabel("设置日期:");
JLabel lab2=new JLabel("设置时间:");
JLabel lab3=new JLabel(" 标题:");
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JTextField tf3=new JTextField();
JTextArea ta=new JTextArea();
JButton btnQue=new JButton("确定");
JButton btnCancle=new JButton("取消");
JLabel lab4=new JLabel(" ");
JLabel labCue = new JLabel("");
public NewFrame()
{
super();
setSize(500,600);
Container conn=getContentPane();
conn.setBounds(0,0,450,450);
lab1.setFont(new Font("宋体",Font.BOLD,15));
lab2.setFont(new Font("宋体",Font.BOLD,15));
lab3.setFont(new Font("宋体",Font.BOLD,15));
lab1.setBounds(20,20,90,20);
lab2.setBounds(20,60,90,20);
lab3.setBounds(20,100,90,20);
tf1.setBounds(100,20,200,20);
tf2.setBounds(100,60,200,20);
tf3.setBounds(100,100,200,20);
conn.add(lab1);
conn.add(tf1);
conn.add(lab2);
conn.add(tf2);
conn.add(lab3);
conn.add(tf3);
JScrollPane scroll=new JScrollPane(ta);
scroll.setBounds(20,150,400,200);
conn.add(scroll);
btnQue.setBounds(20,400,90,20);
ButtonHandler handler = new ButtonHandler();
btnQue.addActionListener(handler);
conn.add(btnQue);
btnCancle.setBounds(130,400,90,20);
conn.add(btnCancle);
conn.add(labCue);
labCue.setBounds(20,430,240,30);
lab4.setBounds(20,480,150,20);
conn.add(lab4);
SimpleDateFormat dateformatD=new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat dateformatT=new SimpleDateFormat("HH:mm:ss");
Date date=new Date();
String now=(dateformatD.format(date)).toString();
String now1=(dateformatT.format(date)).toString();
tf1.setText(now);
tf2.setText(now1);
btnCancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnQue)
{
String sql=" ";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JOptionPane.showMessageDialog(new Frame(),"确定");
String source="jdbc:odbc:xie";
Connection con=DriverManager.getConnection(source);
con.setAutoCommit(true);
Statement stmt=con.createStatement();
tf1.getText();
tf2.getText();
tf3.getText();
sql="INSERT INTO Table1(date, time, lab, thing) VALUES ('"+tf1.getText()+"', '"+tf2.getText()+"', '"+tf3.getText()+"', '"+ta.getText()+"')";
stmt.executeUpdate(sql);
labCue.setForeground(Color.green);
labCue.setText("数据插入成功");
long oldTime = System.currentTimeMillis();//现记录当前程序启动时的时间,然后启动线程
CountTime ct = new CountTime( oldTime );
//System.out.println( oldTime );
ct.start();
}catch(Exception ew)
{
labCue.setForeground(Color.RED);
labCue.setText("数据插入失败");
}
}
}
}
public void rePaint()
{
tf3.setText(" ");
ta.setText(" ");
}
public static void main(String[] args)
{
NewFrame newframe=new NewFrame();
newframe.setVisible(true);
}
class CountTime extends Thread
{
long oldTime = 0;
CountTime( long oldTime )
{
this.oldTime = oldTime;
}
public void run()
{
while(true)
{
//get recent system time
long newTime = System.currentTimeMillis();
//System.out.println( newTime );
if( newTime== Double.parseDouble(tf2.getText()))
{
JOptionPane.showMessageDialog( new Frame(), "TIME OUT" );
System.exit( 0 );
}
}
}
}}
帮忙看下这段代码中的Thread类有没有写错?我要实现的功能是从tf2中得到要弹出提醒的时间,然后和系统当前时间对比,若相等则弹出提醒,帮忙!急用!!!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.text.*;
import java.text.SimpleDateFormat;
public class NewFrame extends JFrame
{
JLabel lab1=new JLabel("设置日期:");
JLabel lab2=new JLabel("设置时间:");
JLabel lab3=new JLabel(" 标题:");
JTextField tf1=new JTextField();
JTextField tf2=new JTextField();
JTextField tf3=new JTextField();
JTextArea ta=new JTextArea();
JButton btnQue=new JButton("确定");
JButton btnCancle=new JButton("取消");
JLabel lab4=new JLabel(" ");
JLabel labCue = new JLabel("");
public NewFrame()
{
super();
setSize(500,600);
Container conn=getContentPane();
conn.setBounds(0,0,450,450);
lab1.setFont(new Font("宋体",Font.BOLD,15));
lab2.setFont(new Font("宋体",Font.BOLD,15));
lab3.setFont(new Font("宋体",Font.BOLD,15));
lab1.setBounds(20,20,90,20);
lab2.setBounds(20,60,90,20);
lab3.setBounds(20,100,90,20);
tf1.setBounds(100,20,200,20);
tf2.setBounds(100,60,200,20);
tf3.setBounds(100,100,200,20);
conn.add(lab1);
conn.add(tf1);
conn.add(lab2);
conn.add(tf2);
conn.add(lab3);
conn.add(tf3);
JScrollPane scroll=new JScrollPane(ta);
scroll.setBounds(20,150,400,200);
conn.add(scroll);
btnQue.setBounds(20,400,90,20);
ButtonHandler handler = new ButtonHandler();
btnQue.addActionListener(handler);
conn.add(btnQue);
btnCancle.setBounds(130,400,90,20);
conn.add(btnCancle);
conn.add(labCue);
labCue.setBounds(20,430,240,30);
lab4.setBounds(20,480,150,20);
conn.add(lab4);
SimpleDateFormat dateformatD=new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat dateformatT=new SimpleDateFormat("HH:mm:ss");
Date date=new Date();
String now=(dateformatD.format(date)).toString();
String now1=(dateformatT.format(date)).toString();
tf1.setText(now);
tf2.setText(now1);
btnCancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnQue)
{
String sql=" ";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JOptionPane.showMessageDialog(new Frame(),"确定");
String source="jdbc:odbc:xie";
Connection con=DriverManager.getConnection(source);
con.setAutoCommit(true);
Statement stmt=con.createStatement();
tf1.getText();
tf2.getText();
tf3.getText();
sql="INSERT INTO Table1(date, time, lab, thing) VALUES ('"+tf1.getText()+"', '"+tf2.getText()+"', '"+tf3.getText()+"', '"+ta.getText()+"')";
stmt.executeUpdate(sql);
labCue.setForeground(Color.green);
labCue.setText("数据插入成功");
long oldTime = System.currentTimeMillis();//现记录当前程序启动时的时间,然后启动线程
CountTime ct = new CountTime( oldTime );
//System.out.println( oldTime );
ct.start();
}catch(Exception ew)
{
labCue.setForeground(Color.RED);
labCue.setText("数据插入失败");
}
}
}
}
public void rePaint()
{
tf3.setText(" ");
ta.setText(" ");
}
public static void main(String[] args)
{
NewFrame newframe=new NewFrame();
newframe.setVisible(true);
}
class CountTime extends Thread
{
long oldTime = 0;
CountTime( long oldTime )
{
this.oldTime = oldTime;
}
public void run()
{
while(true)
{
//get recent system time
long newTime = System.currentTimeMillis();
//System.out.println( newTime );
if( newTime== Double.parseDouble(tf2.getText()))
{
JOptionPane.showMessageDialog( new Frame(), "TIME OUT" );
System.exit( 0 );
}
}
}
}}
帮忙看下这段代码中的Thread类有没有写错?我要实现的功能是从tf2中得到要弹出提醒的时间,然后和系统当前时间对比,若相等则弹出提醒,帮忙!急用!!!
CountTime的逻辑可以这样设计:每隔1秒钟Tread.sleep(1000)判断一次是否需要提醒,如果系统当前时间大于设置时间则需要提醒,另外你在设置时间时必须要求,设置的时间比当前时间大才行
另外,你在时间的解析方面也有错误,给你个例子
Date currDate = new Date(); //获取系统当前时间
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
System.out.println(dateFormat.format(currDate)); //打印出当前时间 2008-02-24 20:36:52
Date setDate = null;
try {
setDate = dateFormat.parse("2008-02-24 20:36:52");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//setDate较大时返回>0,较小时返回0,相等时返回0
setDate.compareTo(currDate);