java代码写日记本功能 Java 实现日记软件
用java编写一个“我的日记”的界面并使其实现写日记的功能, 最好含有登陆界面的
代码如下,自己测试哦
创新互联公司是一家专业提供封丘企业网站建设,专注与网站设计制作、成都做网站、H5响应式网站、小程序制作等业务。10年已为封丘众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import javax.swing.JColorChooser;
class Diary extends JFrame implements ActionListener,TreeSelectionListener{
JMenuBar menubar;
JMenu menu1,menu2,menu3,menu4,menu5,menu6;
JMenuItem item1,item2,item3,item4,item5,item6,item7,item8,item52,item61,item62,item63,item64;
JTextArea text=new JTextArea(20,40);
JButton b_save=new JButton("保存日志");
JButton b_del=new JButton("删除日志");
JButton b3=new JButton("锁定日志");
JButton b4=new JButton("解除锁定");
JSplitPane split1,split2;
JScrollPane scroll1,scroll2;
JPanel p;
JTree tree=null;
int i=0;
DefaultMutableTreeNode root;
DefaultMutableTreeNode month[]=new DefaultMutableTreeNode[13];
Diary(){
final JFrame frame = this;
menubar=new JMenuBar();
menu4=new JMenu("登陆");
item6=new JMenuItem("密码登陆");
menu4.add(item6);
menubar.add(menu4);
menu1=new JMenu("文件");
item1=new JMenuItem("新建");
item2=new JMenuItem("退出");
menu1.add(item1);
menu1.add(item2);
menubar.add(menu1);
menu2=new JMenu("编辑");
item3=new JMenuItem("复制");
item4=new JMenuItem("剪切");
item5=new JMenuItem("粘贴");
item52=new JMenuItem("全选");
menu2.add(item3);
menu2.add(item4);
menu2.add(item5);
menu2.add(item52);
menubar.add(menu2);
menu3=new JMenu("设置");
//item6=new JMenuItem("密码设置");
item63=new JMenuItem("设置字体颜色");
item64=new JMenuItem("设置背景颜色");
item61=new JMenuItem("锁定编辑区");
item62=new JMenuItem("解除锁定");
//menu3.add(item6);
menu3.add(item63);
menu3.add(item64);
menu3.add(item61);
menu3.add(item62);
menubar.add(menu3);
menu4=new JMenu("查看");
item7=new JMenuItem("状态栏");
menu4.add(item7);
menubar.add(menu4);
menu5=new JMenu("帮助");
item8=new JMenuItem("我的日记本信息");
menu5.add(item8);
menubar.add(menu5);
setJMenuBar(menubar); //把菜单条添加到窗口顶端
Container con=getContentPane(); //调用getContentPane()方法获的内容面板
root=new DefaultMutableTreeNode("日历记事本"); //结合树的输入与输出建立一个日历记事本
for(i=1;i=12;i++)
{
month[i]=new DefaultMutableTreeNode(""+i+"月");
root.add(month[i]);
}
for(i=1;i=12;i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
for(int j=1;j=31;j++)
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
else if(i==4||i==6||i==9||i==11)
{
for(int j=1;j=30;j++)
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
else
{
for(int j=1;j=28;j++)
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
}
tree=new JTree(root);
p=new JPanel(); //使用JPanel创建一个面板
p.add(b_save);p.add(b_del);p.add(b3);p.add(b4); //把这4个按钮组件假如面板中
scroll1=new JScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //把文本框放入滚动窗格中
b_save.addActionListener((ActionListener) this); //按钮的监听器
b_del.addActionListener((ActionListener) this);
scroll2=new JScrollPane(tree);
split1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,p,scroll1); //水平拆分这4个按钮和文本区
split2=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll2,split1); //竖直拆分树行日历和文本按钮区
item1.addActionListener((ActionListener) this); //菜单栏的监听器
item2.addActionListener((ActionListener) this);
item3.addActionListener((ActionListener) this);
item4.addActionListener((ActionListener) this);
item5.addActionListener((ActionListener) this);
item6.addActionListener((ActionListener) this);
item7.addActionListener((ActionListener) this);
item8.addActionListener((ActionListener) this);
item52.addActionListener((ActionListener) this);
item61.addActionListener((ActionListener) this);
item62.addActionListener((ActionListener) this);
item63.addActionListener((ActionListener) this);
tree.addTreeSelectionListener((TreeSelectionListener) this); //树形日历的监听器
con.setLayout(new FlowLayout()); //设置布局
setSize(600,500); //设置窗体的大小
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-300)/2,(screen.height-220)/2);
setResizable(false); //设置窗口不可以调节大小
setVisible(true); //设置窗口为可视
con.add(split2); //把树形日历和按钮,文本区都加入到内容面板中
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //单击关闭图标后关闭窗口
}
public void valueChanged(TreeSelectionEvent e) //处理树形事件的接口
{
text.setText(null);
if(e.getSource()==tree)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(node.isLeaf())
{
String str=node.toString();
for( i=0;i=12;i++)
{
if(node.getParent()==month[i])
{
try{String temp=null;
File f=new File(node.getParent().toString()+str+".text");
FileReader file=new FileReader(f);
BufferedReader in=new BufferedReader(file);
while((temp=in.readLine())!=null)
text.append(temp+'\n');
file.close();
in.close();
}
catch(Exception el){}
}
}
}
}
}
//}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b_save) //保存按钮的实现方法
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if(node.isLeaf())
{
try{
File f=new File(node.getParent().toString()+str+".text");
FileWriter tofile=new FileWriter(f);
BufferedWriter out=new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.flush();
tofile.close(); out.close();
}
catch(Exception el){}
}
}
else if(e.getSource()==b_del)
{
int n=JOptionPane.showConfirmDialog(this, "该文件还没有保存,确定要删除吗?","确认对话框", JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if(node.isLeaf())
{
File f=new File(node.getParent().toString()+str+".text");
f.delete();
}
}
else if(n==JOptionPane.NO_OPTION)
{
System.exit(0);
}
}
else if(e.getSource()==b3)
{
text.setEditable(false);
}
else if(e.getSource()==b4)
{
text.setEditable(true);
}
String selected=e.getActionCommand(); //获取命令
if(selected.equals("退出")){ //执行"退出"命令
dispose();
}
else if(selected.equals("新建")){
text.setText("");
}
else if(selected.equals("复制"))
{
text.copy();
}
else if(selected.equals("剪切"))
{
text.cut();
}
else if(selected. equals("粘贴"))
{
text.paste();
}
else if(selected.equals("全选"))
{
text.selectAll();
}
else if(selected.equals("密码登陆"))
{
LoginWindow login=new LoginWindow();
}
else if(selected.equals("设置字体颜色")){
Color newColor=JColorChooser.showDialog(this, "选择字体颜色", text.getForeground());
if(newColor !=null)
{
text.setForeground(newColor);
}
}
else if(selected.equals("设置背景颜色"))
{
Color newColor=JColorChooser.showDialog(this, "选择背景颜色", text.getBackground());
if(newColor !=null)
{
text.setBackground(newColor);
}
}
else if(selected.equals("锁定编辑区"))
{
text.setEditable(false);
}
else if(selected.equals("解除锁定"))
{
text.setEditable(true);
}
}
}
class LoginWindow extends JFrame implements ActionListener {
JPanel p1=new JPanel(); //定义并建立面板
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JTextField text1=new JTextField(15); //用户名文本框
JPasswordField text2=new JPasswordField(15); //密码域
JButton ok=new JButton("确定");
JButton cancel=new JButton("取消");
LoginWindow()
{
setBackground(Color.DARK_GRAY); //设置背景颜色
Container con=getContentPane(); //取出内容面板
con.setLayout(new GridLayout(5,1)); //设置布局为5行1列
p2.add(new JLabel("用户名:"));p2.add(text1); //将组件添加到中间容器
p3.add(new JLabel("密码"));p3.add(text2);
p4.add(ok);p4.add(cancel);
ok.addActionListener(this); //注册事件监听器
cancel.addActionListener(this);
text1.addActionListener(this);
text2.addActionListener(this);
con.add(p1); con.add(p2); con.add(p3); con.add(p4); con.add(p5); //将面板添加到内容面板
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //单击关闭图标后关闭窗口
setSize(300,220); //设置窗口的大小
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-300)/2,(screen.height-220)/2);
setTitle("登录窗口");
setResizable(false); // 不让用户改变窗口大小
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==ok||e.getSource()==text2){
if(text1.getText().trim().equals("meijianwen") text2.getText().trim().equals("070341320")){
dispose(); //关闭登陆窗口
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
text1.requestFocus(); //设置焦点
text1.setSelectionStart(0); //设置选中文本开始位置
text1.setSelectionEnd(text1.getText().length());
}
}
else if(e.getSource()==cancel){ //单击取消按钮
dispose();
//System.exit(0);
}
else if(e.getSource()==text1) //在用户名文本框按回车焦点移到密码域
text2.requestFocus();
}
}
public class MyDiary
{
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
Font font=new Font("JFrame",Font.PLAIN,14); //定义字体
Enumeration keys=UIManager.getLookAndFeelDefaults().keys(); //枚举风格关键字
while(keys.hasMoreElements())
{
Object key=keys.nextElement();
// if(((String)key).equals("Menu.foreground")||((String)key).equals("MenuItem.foreground"))
// UIManager.put(key,Color.DARK_GRAY); //设置菜单文字颜色
if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
}
Diary win=new Diary();
win.validate();
}
}
用java写的日历记事本代码?
详细代码
//CalendarWindow类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class CalendarWindow extends JFrame implements ActionListener,
MouseListener,FocusListener{
int year,month,day;
CalendarMessage calendarMessage;
CalendarPad calendarPad;
NotePad notePad;
JTextField showYear,showMonth;
JTextField [] showDay;
CalendarImage calendarImage;
Clock clock;
JButton nextYear,previousYear,nextMonth,previousMonth;
JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;
File dir;
Color backColor=Color.gray;
public CalendarWindow(){
dir=new File("./dailyRecord");
dir.mkdir();
showDay=new JTextField[42];
for(int i=0;ishowDay.length;i++){
showDay[i]=new JTextField();
showDay[i].setBackground(backColor);
showDay[i].setLayout(new GridLayout(3,3));
showDay[i].addMouseListener(this);
showDay[i].addFocusListener(this);
}
calendarMessage=new CalendarMessage();
calendarPad=new CalendarPad();
notePad=new NotePad();
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH)+1;
day=calendar.get(Calendar.DAY_OF_MONTH);
calendarMessage.setYear(year);
calendarMessage.setMonth(month);
calendarMessage.setDay(day);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.setShowDayTextField(showDay);
notePad.setShowMessage(year,month,day);
calendarPad.showMonthCalendar();
doMark(); //给有日志的号码做标记,见后面的doMark()方法
calendarImage=new CalendarImage();
calendarImage.setImageFile(new File("sea.jpg"));
clock=new Clock();
JSplitPane splitV1=
new JSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);
JSplitPane splitV2=
new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);
JSplitPane splitH=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2);
add(splitH,BorderLayout.CENTER);
showYear=new JTextField(""+year,6);
showYear.setFont(new Font("TimesRoman",Font.BOLD,12));
showYear.setHorizontalAlignment(JTextField.CENTER);
showMonth=new JTextField(" "+month,4);
showMonth.setFont(new Font("TimesRoman",Font.BOLD,12));
showMonth.setHorizontalAlignment(JTextField.CENTER);
nextYear=new JButton("下年");
previousYear=new JButton("上年");
nextMonth=new JButton("下月");
previousMonth=new JButton("上月");
nextYear.addActionListener(this);
previousYear.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
showYear.addActionListener(this);
JPanel north=new JPanel();
north.add(previousYear);
north.add(showYear);
north.add(nextYear);
north.add(previousMonth);
north.add(showMonth);
north.add(nextMonth);
add(north,BorderLayout.NORTH);
saveDailyRecord=new JButton("保存日志") ;
deleteDailyRecord=new JButton("删除日志");
readDailyRecord=new JButton("读取日志");
saveDailyRecord.addActionListener(this);
deleteDailyRecord.addActionListener(this);
readDailyRecord.addActionListener(this);
JPanel pSouth=new JPanel();
pSouth.add(saveDailyRecord);
pSouth.add(deleteDailyRecord);
pSouth.add(readDailyRecord);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);//根据参数 b 的值显示或隐藏此 Window
setBounds(60,60,660,480);
validate();//验证此容器及其所有子组件
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==nextYear){
year++;
showYear.setText(""+year);
calendarMessage.setYear(year);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setShowMessage(year,month,day);
doMark();
}
else if(e.getSource()==previousYear){
year--;
showYear.setText(""+year);
calendarMessage.setYear(year);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setShowMessage(year,month,day);
doMark();
}
else if(e.getSource()==nextMonth){
month++;
if(month12) month=1;
showMonth.setText(" "+month);
calendarMessage.setMonth(month);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setShowMessage(year,month,day);
doMark();
}
else if(e.getSource()==previousMonth){
month--;
if(month1) month=12;
showMonth.setText(" "+month);
calendarMessage.setMonth(month);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setShowMessage(year,month,day);
doMark();
}
else if(e.getSource()==showYear){
String s=showYear.getText().trim();
char a[]=s.toCharArray();
boolean boo=false;
for(int i=0;ia.length;i++)
if(!(Character.isDigit(a[i])))
boo=true;
if(boo==true) //弹出“警告”消息对话框
JOptionPane.showMessageDialog(this,"您输入了非法年份","警告", JOptionPane.WARNING_MESSAGE);
else if(boo==false)
year=Integer.parseInt(s);
showYear.setText(""+year);
calendarMessage.setYear(year);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.showMonthCalendar();
notePad.setShowMessage(year,month,day);
doMark();
}
else if(e.getSource()==saveDailyRecord){
notePad.save(dir,year,month,day);
doMark();
}
else if(e.getSource()==deleteDailyRecord){
notePad.delete(dir,year,month,day);
doMark();
}
else if(e.getSource()==readDailyRecord)
notePad.read(dir,year,month,day);
}
public void mousePressed(MouseEvent e){
JTextField text=(JTextField)e.getSource();
String str=text.getText().trim();
try{ day=Integer.parseInt(str);
}
catch(NumberFormatException exp){
}
calendarMessage.setDay(day);
notePad.setShowMessage(year,month,day);
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void focusGained(FocusEvent e){
Component com=(Component)e.getSource();
com.setBackground(Color.red);
}
public void focusLost(FocusEvent e){
Component com=(Component)e.getSource();
com.setBackground(backColor);
}
public void doMark(){
for(int i=0;ishowDay.length;i++){
showDay[i].removeAll();
String str=showDay[i].getText().trim();
try{
int n=Integer.parseInt(str);
if(isHaveDailyRecord(n)==true){ //见后面的isHaveDailyRecord()方法
JLabel mess=new JLabel("存");
mess.setFont(new Font("TimesRoman",Font.PLAIN,11));
mess.setForeground(Color.black) ;
showDay[i].add(mess);
} }
catch(Exception exp){}
}
calendarPad.repaint();
calendarPad.validate();
}
public boolean isHaveDailyRecord(int n){
String key=""+year+""+month+""+n;
String [] dayFile=dir.list();
boolean boo=false;
for(int k=0;kdayFile.length;k++){
if(dayFile[k].equals(key+".txt")){
boo=true;
break;
} }
return boo;
}
public static void main(String args[]){
new CalendarWindow();
}
}
//CalendarPad类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class CalendarPad extends JPanel{
int year,month,day;
CalendarMessage calendarMessage;
JTextField [] showDay;
JLabel title[];
String [] 星期={"SUN/日","MON/一","TUE/二","WED/三","THU/四","FRI/五","SAT/六"};
JPanel north,center;
public CalendarPad(){
setLayout(new BorderLayout());
north=new JPanel();
north.setLayout(new GridLayout(1,7));
center=new JPanel();
center.setLayout(new GridLayout(6,7));
add(center,BorderLayout.CENTER);
add(north,BorderLayout.NORTH);
title=new JLabel[7];
for(int j=0;j7;j++){
title[j]=new JLabel();
title[j].setFont(new Font("TimesRoman",Font.BOLD,12));
title[j].setText(星期[j]);
title[j].setHorizontalAlignment(JLabel.CENTER);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
north.add(title[j]);
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.blue);
}
public void setShowDayTextField(JTextField [] text){
showDay=text;
for(int i=0;ishowDay.length;i++){
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,15));
showDay[i].setHorizontalAlignment(JTextField.CENTER);
showDay[i].setEditable(false);
center.add(showDay[i]);
}
}
public void setCalendarMessage(CalendarMessage calendarMessage){
this.calendarMessage=calendarMessage;
}
public void showMonthCalendar(){
String [] a=calendarMessage.getMonthCalendar();
for(int i=0;i42;i++)
showDay[i].setText(a[i]);
validate();
}
}
java编写一个记事本小程序,接着下面的代码写,求大神解救!实现新建,保存,打开功能就行!
package tuxingjiemian;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.PrintStream;
public class jishiben extends JFrame {
JPanel jp=new JPanel();
JFrame find_replace=new JFrame();
JMenu file=new JMenu("文件");
JMenu edit=new JMenu("编辑");
JMenu help=new JMenu("帮助");
JMenuBar menubar=new JMenuBar();
JTextArea aa=new JTextArea();
class Open implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser jf= new JFileChooser();
jf.showOpenDialog(jishiben.this);
try {
PrintStream p=new PrintStream(jf.getSelectedFile().getPath());
} catch (Exception e2) {
}
}
}
class Save implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser jf= new JFileChooser();
jf.showSaveDialog(jishiben.this);
try {
PrintStream p=new PrintStream(jf.getSelectedFile().getPath());
} catch (Exception e2) {
}
}
}
public jishiben(){
this.setTitle("记事本");
this.setSize(500, 500);
this.setLayout(new BorderLayout());
JMenuItem open=new JMenuItem("打开");
open.addActionListener(new Open());
JMenuItem save=new JMenuItem("保存");
save.addActionListener(new Save());
JMenuItem exit=new JMenuItem("退出");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
menubar.add(file);
this.add(new JScrollPane(aa),BorderLayout.CENTER);
JMenuItem copy=new JMenuItem("复制");
JMenuItem past=new JMenuItem("粘贴");
JMenuItem delete=new JMenuItem("删除");
JMenuItem find=new JMenuItem("查找");
JMenuItem replace=new JMenuItem("替换");
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jishiben.this.aa.copy();
}
});
past.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jishiben.this.aa.paste();
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jishiben.this.aa.replaceSelection(null);
}
});
edit.add(copy);
edit.add(past);
edit.add(delete);
edit.add(find);
edit.add(replace);
menubar.add(edit);
help.add(new JMenuItem("帮助"));
menubar.add(help);
this.add(menubar,BorderLayout.NORTH);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new jishiben();
}
};
用java编写 我的日记 的程序,并实现其功能,重点是要用java与数据库连接
你可以写4个界面,1登录,2主界面(里边显示所有日记题目),3写日记(insert就行),4查看日记(点击事件select查询),我最近忙考试,只能给你数据库连接类,其他你照书上在点击事件中调用对应的数据库方法就ok了。
package csm.db.conn;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBconn {
static String DBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String ConnStr="jdbc:sqlserver://localhost:1433;DatabaseName=cms;username=sa;password=saas";
private Statement stmt;
private Connection conn;
static{
try {
Class.forName(DBDriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
*
* 获得Statment对象
*/
public void getStatement()throws Exception{
try
{
conn = DriverManager.getConnection(ConnStr);
stmt=conn.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
/**
*
* 关闭所有连接对象
*/
public void closeAll() throws SQLException {
try {
stmt.close();
conn.close();
}
catch(SQLException se) {
throw se;
}
}
/**
*
* @param sql查询语句
* @return ResultSet对象
*/
public ResultSet getResultSet(String sql) throws Exception{
getStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
/**
*
* @param sql插入更新语句
* @return 受影响行数
*/
public int executeSql(String sql)throws Exception{
int records=0;
getStatement();
records=stmt.executeUpdate(sql);
return records;
}
}
我想用java编写windows记事本的新建功能怎么写?要有代码演示。
这是一个我以前写的简单的记事本,里面有新建,保存,另存,打开等功能,但是只是逻辑最简单的那种,你看看吧,希望对你有帮助;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class NotePad {
public static void main(String[] args) {
NotePadFrame notPadFrame = new NotePadFrame();
notPadFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
notPadFrame.setVisible(true);
}
}
class NotePadFrame extends JFrame {
private JMenu jmb, jmb1;
private JMenuBar Jmenu = new JMenuBar();
private JMenuItem fm, fm1, fm2, fm3, fm4, fe1, fe2, fe3, fe4;
String fileName, copy, paste, cut;
NotePadPanel notePadPanel = new NotePadPanel(this);
private NotePadFrame f;
public NotePadFrame() {
jmb = new JMenu("文件");
this.setJMenuBar(Jmenu);
fm = new JMenuItem("新建");
jmb.add(fm);
jmb.addSeparator();
// 新建
fm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm) {
if (!(notePadPanel.getTa().getText()).equals("")) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(null,
"你是否保存", "提示", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
FileDialog d = new FileDialog(f, "保存文件",
FileDialog.SAVE);
d.setVisible(true);
fileName = d.getDirectory() + d.getFile();
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
notePadPanel.getTa().setText("");
}
if (response == 1) {
JOptionPane.showMessageDialog(null, "你选择了取消");
notePadPanel.getTa().setText("");
}
}
}
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
});
fm1 = new JMenuItem("打开");
jmb.add(fm1);
jmb.addSeparator();
// 打开文件
fm1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm1) {
FileDialog d = new FileDialog(f, "打开文件",
FileDialog.LOAD);
d.setVisible(true);
File file = new File(d.getDirectory() + d.getFile());
for (int i = 0; i = file.length(); i++) {
char[] ch = new char[1024];
FileReader fr = new FileReader(file);
fr.read(ch);
String str = new String(ch);
notePadPanel.getTa().setText(str);
}
}
} catch (IOException e3) {
System.out.println(e3.getMessage());
}
}
});
fm2 = new JMenuItem("保存");
jmb.add(fm2);
jmb.addSeparator();
// 保存文件
fm2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm2) {
if (fileName == null) {
fileName = JOptionPane.showInputDialog("请输入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
}
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
});
fm3 = new JMenuItem("另存为");
jmb.add(fm3);
jmb.addSeparator();
// 另存为
fm3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fm3) {
try {
FileDialog d = new FileDialog(f, "另存为", FileDialog.SAVE);
d.setVisible(true);
fileName = d.getDirectory() + d.getFile();
FileOutputStream fout = new FileOutputStream(fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText().getBytes();
fout.write(bb);
// 关闭
fout.close();
} catch (Exception e4) {
System.out.println(e4.getMessage());
}
}
}
});
fm4 = new JMenuItem("关闭");
jmb.add(fm4);
fm4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fm4) {
System.exit(0);
}
}
});
jmb1 = new JMenu("编辑");
fe1 = new JMenuItem("复制");
jmb1.add(fe1);
jmb1.addSeparator();
fe1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fe1) {
copy = notePadPanel.getTa().getSelectedText();
}
}
});
fe2 = new JMenuItem("粘贴");
jmb1.add(fe2);
jmb1.addSeparator();
fe2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fe2) {
System.out.println("copy="+copy);
notePadPanel.getTa().setText(copy);
}
}
});
fe3 = new JMenuItem("剪切");
jmb1.add(fe3);
jmb1.addSeparator();
fe3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fe3) {
copy = notePadPanel.getTa().getSelectedText();
notePadPanel.getTa().setText("");
}
}
});
fe4 = new JMenuItem("版本");
jmb1.add(fe4);
fe4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==fe4){
JOptionPane.showMessageDialog(f, "NotePad 1.0");
}
}
});
Jmenu.add(jmb);
Jmenu.add(jmb1);
}
}
class NotePadPanel extends JPanel {
private JButton jb1, jb;
private JTextArea ta;
String fileName;
private JScrollPane jsp;
public JTextArea getTa() {
return ta;
}
public void setTa(JTextArea ta) {
this.ta = ta;
}
public NotePadPanel(NotePadFrame notePadFrame) {
ta = new JTextArea();
ta.setWrapStyleWord(true);
jsp = new JScrollPane(ta);
jb = new JButton("保存");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == jb) {
if (fileName == null) {
fileName = JOptionPane.showInputDialog("请输入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = ta.getText().getBytes();
fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = ta.getText().getBytes();
fout.write(bb);
// 关闭
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
}
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
});
jb1 = new JButton("关闭");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
System.exit(0);
}
}
});
this.add(jb);
this.add(jb1);
notePadFrame.add(this, "South");
notePadFrame.setSize(600, 400);
notePadFrame.add(jsp);
notePadFrame.setTitle("记事本");
int W = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int H = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
notePadFrame.setLocation((W - notePadFrame.getWidth()) / 2,
(H - notePadFrame.getHeight()) / 2);
}
}
网站栏目:java代码写日记本功能 Java 实现日记软件
分享URL:http://cdiso.cn/article/doohhpg.html