java模拟时间修改器的实现
java模拟时间修改器的实现
结合之前所学的sql数据库,本人模仿了window的时间修改机制,制作了一个模拟修改器,本人修改了很久,找不到bug了,如果哪位大牛发现bug,不吝赐教!程序运行图如下:
?实现不算复杂,代码如下:
package Exercise4; /** * 使用说明:必须安装sql数据库,导入driver类,新建一个ctong库,库中新建exercise4的表,表的属性是:hour(int),minute(int),second(int) * id(int,auto_increment,primary key);完成上述步骤后,自己开启sql数据库,use ctong,之后便可使用! */ import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.sql.ResultSet; import java.sql.Statement; import java.util.Date; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JTextField; import javax.swing.border.BevelBorder; public class Exercise4 extends Mysql{ //时分秒的框 private JTextField field1,field2,field3; //hour表示当前时间,hour0用来存储时间差,hour1表示你修改后的最终时间,也是用来显示的时间 private static int hour,minute,second,hour0,minute0,second0,hour1,minute1,second1,copy_hour,copy_minute,copy_second; private JLabel Picture; private static boolean applyflag = false; public Exercise4(){ final JFrame frame = new JFrame("Exercise4(Time setting)"); //北边面板 JPanel northPanel = new JPanel(); Picture = new JLabel(); northPanel.add(Picture); //中间面板 JPanel centerPanel =createCenterPanel(); //南边面板 JPanel southPanel = new JPanel(); JButton ok = new JButton("OK"); JButton cancel = new JButton("Cancel"); JButton apply = new JButton("Apply"); ok.setMnemonic(KeyEvent.VK_ENTER); cancel.setMnemonic(KeyEvent.VK_BACK_SPACE); apply.setMnemonic(KeyEvent.VK_F1); ok.setPreferredSize(new Dimension(80,30)); cancel.setPreferredSize(new Dimension(80,30)); apply.setPreferredSize(new Dimension(80,30)); ok.setBackground(new Color(0,110,255)); cancel.setBackground(new Color(0,110,255)); apply.setBackground(new Color(0,110,255)); ok.setForeground(Color.WHITE); cancel.setForeground(Color.WHITE); apply.setForeground(Color.WHITE); southPanel.add(ok); southPanel.add(cancel); southPanel.add(apply); //界面属性 frame.setLayout(new GridLayout(3,1)); frame.setLocation(500,200); frame.setResizable(false); frame.setSize(300,300); frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); frame.setIconImage(new ImageIcon("321.jpg").getImage()); frame.add(northPanel); frame.add(centerPanel); frame.add(southPanel); frame.setDefaultCloseOperation(3); frame.setVisible(true); //监听器 ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addTable(getHour(), getMinute(), getSecond(),1); timeSQL(); close0(); System.exit(0); } }); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(applyflag){ addTable(getHour(), getMinute(), getSecond(),1); } timeSQL(); close0(); System.exit(0); } }); apply.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { applyflag=true; JOptionPane.showMessageDialog(null, "时间已修改"); } }); } //中间面板的创建函数 private JPanel createCenterPanel() { JPanel initPanel = new JPanel(); JButton houradd = new JButton("▲"); JButton hoursub = new JButton("▼"); //快捷键,下同 houradd.setMnemonic(KeyEvent.VK_Q); hoursub.setMnemonic(KeyEvent.VK_A); //鼠标指针,下同 houradd.setCursor(new Cursor(12)); hoursub.setCursor(new Cursor(12)); //上下按钮面板,下同 JPanel hourPanel =new JPanel(new GridLayout(2,1)); houradd.setPreferredSize(new Dimension(40,10)); //按钮凸起,下同 houradd.setBorder(new BevelBorder(0)); hoursub.setBorder(new BevelBorder(0)); hourPanel.add(houradd); hourPanel.add(hoursub); JButton minuteadd = new JButton("▲"); JButton minutesub = new JButton("▼"); minuteadd.setMnemonic(KeyEvent.VK_W); minutesub.setMnemonic(KeyEvent.VK_S); minuteadd.setCursor(new Cursor(12)); minutesub.setCursor(new Cursor(12)); JPanel minutePanel =new JPanel(new GridLayout(2,1)); minuteadd.setPreferredSize(new Dimension(40,10)); minuteadd.setBorder(new BevelBorder(0)); minutesub.setBorder(new BevelBorder(0)); minutePanel.add(minuteadd); minutePanel.add(minutesub); JButton secondadd = new JButton("▲"); JButton secondsub = new JButton("▼"); secondadd.setMnemonic(KeyEvent.VK_E); secondsub.setMnemonic(KeyEvent.VK_D); secondadd.setCursor(new Cursor(12)); secondsub.setCursor(new Cursor(12)); JPanel secondPanel =new JPanel(new GridLayout(2,1)); secondadd.setPreferredSize(new Dimension(40,10)); secondadd.setBorder(new BevelBorder(0)); secondsub.setBorder(new BevelBorder(0)); secondPanel.add(secondadd); secondPanel.add(secondsub); field1 = new JTextField(2); field2 = new JTextField(2); field3 = new JTextField(2); //box,用来设置组件的间距 Box baseBox = Box.createHorizontalBox(); baseBox.add(field1); baseBox.add(hourPanel); baseBox.add(Box.createHorizontalStrut(10));//放置透明box来表示间距 baseBox.add(field2); baseBox.add(minutePanel); baseBox.add(Box.createHorizontalStrut(10)); baseBox.add(field3); baseBox.add(secondPanel); field1.setFont(new Font(null, Font.BOLD, 18)); field2.setFont(new Font(null, Font.BOLD, 18)); field3.setFont(new Font(null, Font.BOLD, 18)); //线程,用来及时显示时间变化 Thread thread = new Thread(new Runnable() { public void run() { hour = new Date().getHours(); minute = new Date().getMinutes(); second = new Date().getSeconds(); SelectTables(); SelectTables1(); SelectTables0(); DelData(1); DelData(2); changePic(); while(true){ if(copy_hour != getHour()){ copy_hour = getHour(); if(copy_hour>60){ field1.setText("0"); }else field1.setText(""+getHour()); } if(copy_minute != getMinute()){ copy_minute = getMinute(); if(copy_minute>60){ field2.setText("0"); int m = getHour(); setHour(++m); if(m>=24) setHour(0); } else field2.setText(""+getMinute()); } if(copy_second != getSecond()){ copy_second = getSecond(); field3.setText(""+ getSecond()); } secUp(); changePic(); //休眠1秒 try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); thread.start(); initPanel.add(baseBox); //监听器 houradd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { houUp(); } }); hoursub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { houDown(); } }); minuteadd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { minUp(); } }); minutesub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { minDown(); } }); secondadd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { secUp(); } }); secondsub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { secDown(); } }); return initPanel; } //重写get,set方法 public void setHour(int hour) { field1.setText(""+hour); } public void setMinute(int minute) { field2.setText(""+minute); } public void setSecond(int second) { field3.setText(""+second); } public int getHour() { if(field1.getText().isEmpty()) setHour(0); return Integer.parseInt(field1.getText()); } public int getMinute() { if(field2.getText().isEmpty()) setMinute(0); return Integer.parseInt(field2.getText()); } public int getSecond(){ if(field3.getText().isEmpty()) setSecond(0); return Integer.parseInt(field3.getText()); } //以下6个方法表示相关时间的加减 public void houUp(){ int t = getHour(); setHour(++t); if(t>=24) setHour(0); } public void houDown(){ if(getHour()!=0){ int t = getHour(); setHour(--t); }else{ setHour(23); } } public void minUp(){ int t = getMinute(); setMinute(++t); if(t>=60) { setMinute(0); int m = getHour(); setHour(++m); if(m>=24) setHour(0); } } public void minDown(){ if(getMinute()!=0){ int t = getMinute(); setMinute(--t); }else{ setMinute(59); if(getHour()!=0){ int t = getHour(); setHour(--t); }else{ setHour(23); } } } public void secUp(){ int t = getSecond(); setSecond(++t); if(t>=60){ setSecond(0); int k = getMinute(); setMinute(++k); if(k>=60){ setMinute(0); int m = getHour(); setHour(++m); if(m>=24) setHour(0); } } } public void secDown(){ if(getSecond()!=0){ int t = getSecond(); setSecond(--t); }else{ setSecond(59); if(getMinute()!=0){ int t = getMinute(); setMinute(--t); }else{ setMinute(59); if(getHour()!=0){ int t = getHour(); setHour(--t); }else{ setHour(23); } } } } //改变图片 public void changePic(){ if(getHour()>=7&&getHour()<=11){ Picture.setIcon(new ImageIcon("zaoshang.jpg")); } else if(getHour()>11&&getHour()<=15){ Picture.setIcon(new ImageIcon("zhengwu.jpg")); } else if(getHour()>15&&getHour()<=18){ Picture.setIcon(new ImageIcon("huanghun.jpg")); } else { Picture.setIcon(new ImageIcon("wanshang.jpg")); } } //获取当前时间,并存入数据库 public void timeSQL(){ int hour = new Date().getHours(); int minute = new Date().getMinutes(); int second = new Date().getSeconds(); addTable(hour, minute, second, 2); } //获取时间差 public void SelectTables(){ try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from exercise4"; ResultSet rs = statement.executeQuery(sql); int hour1 = 0; int minute1 = 0; int second1 = 0; int id; while (rs.next()) { hour1 = rs.getInt("hour"); minute1 = rs.getInt("minute"); second1 = rs.getInt("second"); id=rs.getInt("id"); if(id==2){ hour0 = Math.abs(hour - hour1); minute0 = Math.abs(minute - minute1); second0 = Math.abs(second - second1); System.out.println("hour =" + hour0 + " minute=" + minute0+" second="+second0+" id="+id); } } rs.close(); } catch (Exception e) { e.printStackTrace(); } } //获取显示时间 public void SelectTables1(){ try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from exercise4"; ResultSet rs = statement.executeQuery(sql); hour1 = 0; minute1 = 0; second1 = 0; int id; while (rs.next()) { hour1 = rs.getInt("hour"); minute1 = rs.getInt("minute"); second1 = rs.getInt("second"); id=rs.getInt("id"); if(id==1){ hour1 += hour0; minute1 += minute0; second1 += second0; System.out.println("hour =" + hour1 + " minute=" + minute1+" second="+second1+" id="+id); copy_hour = hour1; copy_minute = minute1; copy_second = second1; field1.setText(""+hour1); field2.setText(""+minute1); field3.setText(""+second1); } } rs.close(); }catch (Exception e) { e.printStackTrace(); } } //对用户是否修改过时间进行判断 public void SelectTables0(){ try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from exercise4"; ResultSet rs = statement.executeQuery(sql); int n = 0; while(rs.next()) { n++; } if(n<=1){ copy_hour = hour; copy_minute = minute; copy_second = second; field1.setText(""+hour); field2.setText(""+minute); field3.setText(""+second); } rs.close(); }catch (Exception e) { e.printStackTrace(); } } /** * 主函数 * @param ctong */ public static void main(String[] args) { new Exercise4(); } }
?
package Exercise4; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 1.增加数据-insert into game(name,type) values(?,?) * public void dd2Table(String name,String type); * public void add2Table(String name,String type,int id); * 2.删除数据-delete from game where id = ? * public void DelData(int id) * 3.查找数据-select * from game * public void SelectTables() * 4.修改数据-update game set name=?,type=? where id=? * public void upData(String name,String type,int id) * * * * @author ctong * */ public class Mysql { /* 数据库加载固定属性 */ //mySql 中驱动类的名字就是其驱动包中的com.mysql.jdbc.Driver.class 类。 String driver="com.mysql.jdbc.Driver"; Connection con; //jdbc:mysql://ip 地址: 端口号/数据库名字 String url="jdbc:mysql://localhost:3306/ctong"; String user="root"; //连接上数据库mysql public Mysql() { try { Class.forName(driver); //第三个属性是密码 con=DriverManager.getConnection(url,user,""); if(!con.isClosed()) System.out.println("连接成功"); } catch (Exception e) { e.printStackTrace(); } } //在表末增加一栏数据 public void addTable(int hour,int minute , int second) { try { //预处理命令添加 PreparedStatement sql; sql = con.prepareStatement("insert into exercise4(hour,minute,second) values(?,?,?)"); sql.setInt(1,hour); sql.setInt(2,minute); sql.setInt(3,second); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //增加一栏数据-----指定id public void addTable(int hour,int minute , int second,int id) { try { //预处理命令添加 PreparedStatement sql; sql = con.prepareStatement("insert into exercise4(hour,minute,second,id) values(?,?,?,?)"); sql.setInt(1,hour); sql.setInt(2,minute); sql.setInt(3,second); sql.setInt(4, id); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //查找表中所有数据 public void SelectTables(){ try { // 执行sql语句 Statement statement = con.createStatement(); String sql = "select * from exercise4"; ResultSet rs = statement.executeQuery(sql); int hour = 0; int minute = 0; int second = 0; int id; while (rs.next()) { hour = rs.getInt("hour"); minute = rs.getInt("minute"); second = rs.getInt("second"); id=rs.getInt("id"); System.out.println("hour =" + hour + " minute=" + minute+" second="+second+" id="+id); } rs.close(); } catch (Exception e) { e.printStackTrace(); } } //删除一行数据 public void DelData(int id) { try { PreparedStatement sql; sql = con.prepareStatement("delete from exercise4 where id = ?"); sql.setInt(1,id); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } //修改一栏数据 public void upData(int hour,int minute ,int second,int id) { try { PreparedStatement sql; sql = con.prepareStatement("update exercise4 set hour=?,minute=?,second=?, where id=?"); sql.setInt(1,hour); sql.setInt(2,minute); sql.setInt(3,second); sql.setInt(4, id); sql.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public void close0(){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
?相关图片就不提供了,谢谢观看!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。