日历记事本完整程序代码

日历记事本完整程序代码
日历记事本完整程序代码

日历记事本完整小程序

学校:河南科技学院

编写人员:王春山陈敬高

时间:2015-12-30

package https://www.360docs.net/doc/b4248665.html,;

import java.util.Calendar;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.Hashtable;

public class CalendarPad extends JFrame implements MouseListener

{

int year,month,day;

Hashtable hashtable;

File file;

JTextField showDay[];//JTextField()创建文本框

JLabel title[]; //JLabel用于短文本字符串或图像或二者的显示区

Calendar ca; //日历:ca

int week; //星期几:week

NotePad notepad=null;

Month rm;//负责改变月:rm

Year ry;//负责改变年:ry

String w[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

//声明两个面板对象

JPanel leftPanel,rightPanel;

private int userid;

public CalendarPad(int year,int month,int day, int userid) {

//创建一个标题为"日历记事本"的窗口

super("日历记事本");

https://www.360docs.net/doc/b4248665.html,erid = userid;

//实例化面板对象

leftPanel=new JPanel();//JPanel()为中间容器即面板

JPanel leftCenter=new JPanel();

JPanel leftNorth=new JPanel();

//设置面板布局

leftCenter.setLayout(new GridLayout(7,7));

//实例化面板对象

rightPanel=new JPanel();

JPanel rightCenter=new JPanel();

JPanel rightSuoth=new JPanel();

this.year=year;

this.month=month;

this.day=day;

ry=new Year(this);

ry.setYear(year);

rm=new Month(this);

rm.setMonth(month);

title=new JLabel[7]; //设置显示星期区

showDay=new JTextField[42];//JTextField()创建文本框

//星期区填充

for(int j=0;j<7;j++)

{

title[j]=new JLabel();

title[j].setText(w[j]);

title[j].setBorder(BorderFactory.createRaisedBevelBorder());

leftCenter.add(title[j]);

}

title[0].setBackground(Color.green);

title[6].setBackground(Color.green);

title[0].setForeground(Color.red);

title[6].setForeground(Color.blue);

//日期区填充

for(int i=0;i<42;i++)

{

showDay[i]=new JTextField();

showDay[i].addMouseListener(this);

showDay[i].setEditable(false);

leftCenter.add(showDay[i]);

}

ca = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。

Box box=Box.createHorizontalBox();//创建一个从左到右显示其组件的Box。box.add(ry);

box.add(rm);

leftNorth.add(box);

leftPanel.setLayout(new BorderLayout());

leftPanel.add(leftNorth,BorderLayout.NORTH);

leftPanel.add(leftCenter,BorderLayout.CENTER);

leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),

BorderLayout.SOUTH) ;

leftPanel.validate();

Container con=getContentPane();

JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,

leftPanel,rightPanel);

con.add(split,BorderLayout.CENTER);

con.validate();

hashtable=new Hashtable();

file=new File("日历记事本.txt");

if(!file.exists())

{

try{

FileOutputStream out=new FileOutputStream(file);

ObjectOutputStream objectOut=new ObjectOutputStream(out);

objectOut.writeObject(hashtable);

objectOut.close();

out.close();

}

catch(IOException e)

{

}

}

notepad=new NotePad(this);

Icon icon=new ImageIcon("a.gif");

JLabel label=new JLabel(icon,JLabel.CENTER);

rightPanel.add(notepad,BorderLayout.CENTER);

rightPanel.add(label,BorderLayout.SOUTH);

scp(year,month);//设置日历牌:scp

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

setVisible(true);

setBounds(200,100,524,585);

validate();

}

public int getUserid() {

return userid;

}

public void setUserid(int userid) {

https://www.360docs.net/doc/b4248665.html,erid = userid;

}

public void scp(int year,int month)

{

ca.set(year,month-1,1);

week=ca.get(Calendar.DAY_OF_WEEK)-1;

if(month==1||month==3||month==5||month==7

||month==8||month==10||month==12)

{

orderNumber(week,31);

}

else if(month==4||month==6||month==9||month==11)

{

orderNumber(week,30);

}

else if(month==2)

{

if((year%4==0&&year%100!=0)||(year%400==0))

{

orderNumber(week,29);

}

else

{

orderNumber(week,28);

}

}

}

public void orderNumber(int whatDay,int whatMonth)//排列号码:orderNumber

{

for(int i=whatDay,n=1;i

showDay[i].setText(""+n);

if(n==day)

{

showDay[i].setForeground(Color.pink);

// showDay[i].setBackground(Color.pink);

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,38));

}

else

{

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));

showDay[i].setForeground(Color.black);

//showDay[i].setBackground(C);

}

if(i%7==6)

{

showDay[i].setForeground(Color.blue);

}

if(i%7==0)

{

showDay[i].setForeground(Color.red);

}

n++;

}

for(int i=0;i

{

showDay[i].setText("");

}

for(int i=whatDay+whatMonth;i<42;i++)

{

showDay[i].setText("");

}

}

public int getYear()

{

return year;

}

public void setYear(int y)

{

year=y;

notepad.setYear(year);

}

public int getMonth()

{

return month;

}

public void setMonth(int m)

{

month=m;

notepad.setMonth(month);

public int getDay()

{

return day;

}

public void setDay(int d)

{

day=d;

notepad.setDay(day);

}

public Hashtable getHashtable()

{

return hashtable;

}

public File getFile()

{

return file;

}

public void mousePressed(MouseEvent e)

{

JTextField source=(JTextField)e.getSource();

try{

day=Integer.parseInt(source.getText());

notepad.setDay(day);

notepad.setInformation(year,month,day);

notepad.setArea(null);

notepad.getLcontent(year,month,day);

}

catch(Exception ee)

{

}

}

public void mouseClicked(MouseEvent e)

{

}

public void mouseReleased(MouseEvent e)

{

}

public void mouseEntered(MouseEvent e)

{

}

public void mouseExited(MouseEvent e)

{

}

package https://www.360docs.net/doc/b4248665.html,;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Month extends Box implements ActionListener

{

int month;

JTextField showMonth=null;

JButton nextMonth,lastMonth;//上月:lastMonth,下月:nextMonth CalendarPad calender;//

public Month(CalendarPad calender)

{

super(BoxLayout.X_AXIS);

this.calender=calender;

showMonth=new JTextField(2);

month=calender.getMonth();

showMonth.setEditable(false);

showMonth.setForeground(Color.blue);

showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));

nextMonth=new JButton("下月");

lastMonth=new JButton("上月");

add(lastMonth);

add(showMonth);

add(nextMonth);

lastMonth.addActionListener(this);

nextMonth.addActionListener(this);

showMonth.setText(""+month);

}

public void setMonth(int month)

{

if(month<=12&&month>=1)

{

this.month=month;

}

else

{

this.month=1;

}

showMonth.setText(""+month);

}

public int getMonth()

{

return month;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==lastMonth)

{

if(month>=2)

{

month=month-1;

calender.setMonth(month);

calender.scp(calender.getYear(),month);

}

else if(month==1)

{

month=12;

calender.ry.setYear(calender.ry.year-1);

calender.setMonth(month);

calender.scp(calender.getYear(),month);

}

showMonth.setText(""+month);

}

else if(e.getSource()==nextMonth)

{

if(month<12)

{

month=month+1;

calender.setMonth(month);

calender.scp(calender.getYear(),month);

}

else if(month==12)

{

month=1;

calender.ry.setYear(calender.ry.year+1);

calender.setMonth(month);

calender.scp(calender.getYear(),month);

}

showMonth.setText(""+month);

}

}

}

package https://www.360docs.net/doc/b4248665.html,;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.UnsupportedEncodingException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Hashtable;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import net.cw.view.DB;

public class NotePad extends JPanel implements ActionListener{ JTextArea text;

JButton saveLog,deleteLog;//保存日志,删除日志:saveLog,deleteLog Hashtable table;

JLabel articleInformation; //信息条:articleInformation.

int year,month,day;

File file;

CalendarPad calendar;

public NotePad(CalendarPad calendar)

{

this.calendar=calendar;

year=calendar.getYear();

month=calendar.getMonth();

day=calendar.getDay();;

table=calendar.getHashtable();

file=calendar.getFile();

articleInformation=new JLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);

articleInformation.setFont(new Font("TimesRoman",Font.BOLD,16));

articleInformation.setForeground(Color.blue);

text=new JTextArea(10,10);

saveLog=new JButton("保存日志") ;

deleteLog=new JButton("删除日志") ;

saveLog.addActionListener(this);

deleteLog.addActionListener(this);

setLayout(new BorderLayout());

JPanel pSouth=new JPanel();

add(articleInformation,BorderLayout.NORTH);

pSouth.add(saveLog);

pSouth.add(deleteLog);

add(pSouth,BorderLayout.SOUTH);

add(new JScrollPane(text),BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==saveLog)

{

saveLog(year,month,day);

}

else if(e.getSource()==deleteLog)

{

deleteLog(year,month,day);

}

}

public void setYear(int year)

{

this.year=year;

}

public int getYear()

{

return year;

}

public void setMonth(int month)

{

this.month=month;

}

public int getMonth()

{

return month;

}

public void setDay(int day)

{

this.day=day;

}

public int getDay()

{

return day;

}

public void setInformation(int year,int month,int day)//设置信息条:setInformation

{

articleInformation.setText(""+year+"年"+month+"月"+day+"日");

}

public void setArea(String s)//设置文本区:setArea

{

text.setText(s);

}

public void getLcontent(int year,int month,int day)//获取日志内容:getLcontent

{

String key=""+year+"年"+month+"月"+day+"日";

String text = searchLogLogic(year, month, day, key);

if (null != text) {

String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?";

int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

setArea(text);

} else {

setArea("无记录");

}

}

public String searchLogLogic (int year,int month,int day, String key) {

Connection con = DB.getConnection();

try {

//首先查一下这个日期有没有日志

PreparedStatement ps = con.prepareStatement("select * from log where create_time = ? and userid = ?");

ps.setString(1, key);

ps.setInt(2, calendar.getUserid());

ResultSet rs = ps.executeQuery();

rs.first();

if (rs.getRow() != 0) {

String text = rs.getString("content");

return text;

} else {

return null;

}

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

return null;

}

}

public void saveLog(int year,int month,int day)//保存日志:saveLog

{

String logContent=text.getText();

String key=articleInformation.getText();

System.out.println(logContent);

System.out.println(key);

Connection con = DB.getConnection();

try {

//首先查一下这个日期有没有日志

String sql = "insert into log (userid,content,create_time) values (?,?,?)";

if (null == searchLogLogic(year, month, day, key)) {

PreparedStatement ps = con.prepareStatement(sql);

ps.setInt(1, calendar.getUserid());

ps.setString(2, logContent);

ps.setString(3, key);

String m=""+year+"年"+month+"月"+day+"保存日志吗?";

int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if (0 < ps.executeUpdate()) {

setArea(logContent);

}

} else {

PreparedStatement ps = con.prepareStatement("update log set content = ?

where create_time = ?");

ps.setString(1, logContent);

ps.setString(2, key);

if (0 < ps.executeUpdate()) {

setArea(logContent);

}

}

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

public void deleteLog(int year,int month,int day)//删除日志:deleteLog

{

String key=""+year+"年"+month+"月"+day+"日";

Connection con = DB.getConnection();

try {

//首先查一下这个日期有没有日志

if (null != searchLogLogic(year, month, day, key)) {

PreparedStatement ps = con.prepareStatement("delete from log where create_time = ? and userid = ?");

ps.setString(1, key);

ps.setInt(2, calendar.getUserid());

String m="删除"+year+"年"+month+"月"+day+"日的日志吗?";

int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if (0 < ps.executeUpdate()) {

setArea("无记录");

}

}

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

package https://www.360docs.net/doc/b4248665.html,;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Year extends Box implements ActionListener

{

int year;

JTextField showYear=null;

JButton nextMonth,lastMonth;//去年:lastMonth,明年nextMonth CalendarPad calender;

public Year(CalendarPad calender)

{

super(BoxLayout.X_AXIS);

showYear=new JTextField(4);

showYear.setForeground(Color.blue);

showYear.setFont(new Font("TimesRomn",Font.BOLD,14));

this.calender=calender;

year=calender.getYear();

nextMonth=new JButton("下年");

lastMonth=new JButton("上年");

add(lastMonth);

add(showYear);

add(nextMonth);

showYear.addActionListener(this);

lastMonth.addActionListener(this);

nextMonth.addActionListener(this);

}

public void setYear(int year)

{

this.year=year;

showYear.setText(""+year);

}

public int getYear()

{

return year;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==lastMonth)

{

year=year-1;

showYear.setText(""+year);

calender.setYear(year);

calender.scp(year,calender.getMonth());

}

else if(e.getSource()==nextMonth)

{

year=year+1;

showYear.setText(""+year);

calender.setYear(year);

calender.scp(year,calender.getMonth());

}

else if(e.getSource()==showYear)

{

try

{

year=Integer.parseInt(showYear.getText());

showYear.setText(""+year);

calender.setYear(year);

calender.scp(year,calender.getMonth());

}

catch(NumberFormatException ee)

{

showYear.setText(""+year);

calender.setYear(year);

calender.scp(year,calender.getMonth());

}

}

}

}

package net.cw.view;

import java.sql.Connection;

import java.sql.DriverManager;

public class DB {

//数据库地址

private String Driver_name =

"jdbc:mysql://localhost:3306/test";

//数据库用户名

private String USER = "root";

//数据库密码

private String PASS = "123456";

//数据库连接

public static Connection con;

//构造方法

public DB(){

try {

//加载驱动

Class.forName("com.mysql.jdbc.Driver");

//获取连接

con = DriverManager.getConnection(Driver_name, USER, PASS);

//con.prepareStatement("set NAMES utf8").executeUpdate();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//获取连接

public static Connection getConnection(){

if(con == null){

new DB();

}

return con;

}

}

package net.cw.view;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.SQLException;

import java.util.Calendar;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import com.mysql.jdbc.log.LogUtils;

import https://www.360docs.net/doc/b4248665.html,.CalendarPad;

/**

* 登陆界面

* 1、创建登陆界面,初始化上面的对象

* 2、给按钮设置监听事件

*

*/

public class Login {

//初始化字体

Font d = new Font("楷体", Font.BOLD, 22);

Font f = new Font("楷体", Font.BOLD, 15);

// 初始化对象

JFrame logingui = new JFrame("用户登录界面");

JLabel userlogin = new JLabel("用户登录");

JLabel username = new JLabel("用户名:");

JLabel password = new JLabel("密码:");

JTextField name = new JTextField();

JTextField pwd = new JPasswordField();

JButton login = new JButton("登陆");

JButton cancle = new JButton("取消");

JButton regist = new JButton("注册");

//给User类初始化对象user

User user = new User();

public void LoginGui() {

// 设置对象

logingui.setBounds(450, 200, 400, 300);

//设置退出

logingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//取消框架格式

logingui.setLayout(null);

//设置位置、大小和字体

userlogin.setBounds(140, 30, 150, 30);

userlogin.setFont(d);

username.setBounds(50, 80, 100, 30);

username.setFont(f);

password.setBounds(50, 120, 100, 30);

password.setFont(f);

name.setBounds(140, 80, 180, 30);

name.setFont(f);

pwd.setBounds(140, 120, 180, 30);

login.setBounds(50, 200, 80, 30);

login.setFont(f);

cancle.setBounds(140,200,80,30);

cancle.setFont(f);

regist.setBounds(240,200,80,30);

regist.setFont(f);

// 添加对象

logingui.add(userlogin);

logingui.add(username);

logingui.add(password);

logingui.add(name);

logingui.add(pwd);

logingui.add(login);

logingui.add(cancle);

logingui.add(regist);

// 窗体可视化

logingui.setVisible(true);

//给登录按钮设置监听事件

login.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

//提取文本框里的用户名和密码

String name_text = name.getText();

String pwd_text = pwd.getText();

if (name_text.equals("")) {

JOptionPane.showMessageDialog(null, "用户名不能为空,请输入!", "提示",

JOptionPane.WARNING_MESSAGE);

return;

}

if (pwd_text.equals("")) {

JOptionPane.showMessageDialog(null, "密码不能为空,请输入!", "提示",

JOptionPane.WARNING_MESSAGE);

return;

}

//将得到的值存入user对象里面

user.setusername(name_text);

user.setuserpwd(pwd_text);

//给登陆接口实现类初始化对象

LoginUseImp l = new LoginUseImp();

boolean flag = false;

try {

flag = l.Query(user, "select * from user where name=? and password=? ");

} catch (SQLException e1) {

// TODO 自动生成的catch 块

e1.printStackTrace();

}

if (flag) {

int userid = user.getId();

Calendar calendar=Calendar.getInstance();

int y=calendar.get(Calendar.YEAR);

int m=calendar.get(Calendar.MONTH)+1;

int d=calendar.get(Calendar.DAY_OF_MONTH);

new CalendarPad(y,m,d, userid);

logingui.dispose();

//JOptionPane.showMessageDialog(null, "登陆成功");

}

else {

System.out.println("gsdjkfhsvj");

JOptionPane.showMessageDialog(null, "该用户不存在,请注册!");

name.setText("");

pwd.setText("");

}

}

});

//给注册按钮设置监听事件

regist.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

logingui.setVisible(false);

new registgui();

}

});

//给取消按钮设置监听事件

cancle.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

name.setText("");

pwd.setText("");

}

});

}

//整个程序执行的入口

public static void main(String[] args) {

Login l = new Login();

l.LoginGui();

}

}

package net.cw.view;

import java.sql.SQLException;

public interface LoginUse {

//查询

相关主题
相关文档
最新文档