代碼如下,自己測(cè)試哦
創(chuàng)新互聯(lián)公司是一家專業(yè)提供封丘企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為封丘眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
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("復(fù)制");
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("設(shè)置");
//item6=new JMenuItem("密碼設(shè)置");
item63=new JMenuItem("設(shè)置字體顏色");
item64=new JMenuItem("設(shè)置背景顏色");
item61=new JMenuItem("鎖定編輯區(qū)");
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("狀態(tài)欄");
menu4.add(item7);
menubar.add(menu4);
menu5=new JMenu("幫助");
item8=new JMenuItem("我的日記本信息");
menu5.add(item8);
menubar.add(menu5);
setJMenuBar(menubar); //把菜單條添加到窗口頂端
Container con=getContentPane(); //調(diào)用getContentPane()方法獲的內(nèi)容面板
root=new DefaultMutableTreeNode("日歷記事本"); //結(jié)合樹(shù)的輸入與輸出建立一個(gè)日歷記事本
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創(chuàng)建一個(gè)面板
p.add(b_save);p.add(b_del);p.add(b3);p.add(b4); //把這4個(gè)按鈕組件假如面板中
scroll1=new JScrollPane(text,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); //把文本框放入滾動(dòng)窗格中
b_save.addActionListener((ActionListener) this); //按鈕的監(jiān)聽(tīng)器
b_del.addActionListener((ActionListener) this);
scroll2=new JScrollPane(tree);
split1=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,p,scroll1); //水平拆分這4個(gè)按鈕和文本區(qū)
split2=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll2,split1); //豎直拆分樹(shù)行日歷和文本按鈕區(qū)
item1.addActionListener((ActionListener) this); //菜單欄的監(jiān)聽(tīng)器
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); //樹(shù)形日歷的監(jiān)聽(tīng)器
con.setLayout(new FlowLayout()); //設(shè)置布局
setSize(600,500); //設(shè)置窗體的大小
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-300)/2,(screen.height-220)/2);
setResizable(false); //設(shè)置窗口不可以調(diào)節(jié)大小
setVisible(true); //設(shè)置窗口為可視
con.add(split2); //把樹(shù)形日歷和按鈕,文本區(qū)都加入到內(nèi)容面板中
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //單擊關(guān)閉圖標(biāo)后關(guān)閉窗口
}
public void valueChanged(TreeSelectionEvent e) //處理樹(shù)形事件的接口
{
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) //保存按鈕的實(shí)現(xiàn)方法
{
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, "該文件還沒(méi)有保存,確定要?jiǎng)h除嗎?","確認(rèn)對(duì)話框", 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("退出")){ //執(zhí)行"退出"命令
dispose();
}
else if(selected.equals("新建")){
text.setText("");
}
else if(selected.equals("復(fù)制"))
{
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("設(shè)置字體顏色")){
Color newColor=JColorChooser.showDialog(this, "選擇字體顏色", text.getForeground());
if(newColor !=null)
{
text.setForeground(newColor);
}
}
else if(selected.equals("設(shè)置背景顏色"))
{
Color newColor=JColorChooser.showDialog(this, "選擇背景顏色", text.getBackground());
if(newColor !=null)
{
text.setBackground(newColor);
}
}
else if(selected.equals("鎖定編輯區(qū)"))
{
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); //設(shè)置背景顏色
Container con=getContentPane(); //取出內(nèi)容面板
con.setLayout(new GridLayout(5,1)); //設(shè)置布局為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); //注冊(cè)事件監(jiān)聽(tīng)器
cancel.addActionListener(this);
text1.addActionListener(this);
text2.addActionListener(this);
con.add(p1); con.add(p2); con.add(p3); con.add(p4); con.add(p5); //將面板添加到內(nèi)容面板
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //單擊關(guān)閉圖標(biāo)后關(guān)閉窗口
setSize(300,220); //設(shè)置窗口的大小
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(); //關(guān)閉登陸窗口
}
else{
JOptionPane.showMessageDialog(null, "用戶名或密碼錯(cuò)誤!");
text1.requestFocus(); //設(shè)置焦點(diǎn)
text1.setSelectionStart(0); //設(shè)置選中文本開(kāi)始位置
text1.setSelectionEnd(text1.getText().length());
}
}
else if(e.getSource()==cancel){ //單擊取消按鈕
dispose();
//System.exit(0);
}
else if(e.getSource()==text1) //在用戶名文本框按回車(chē)焦點(diǎn)移到密碼域
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(); //枚舉風(fēng)格關(guān)鍵字
while(keys.hasMoreElements())
{
Object key=keys.nextElement();
// if(((String)key).equals("Menu.foreground")||((String)key).equals("MenuItem.foreground"))
// UIManager.put(key,Color.DARK_GRAY); //設(shè)置菜單文字顏色
if(UIManager.get(key)instanceof Font)UIManager.put(key,font);
}
Diary win=new Diary();
win.validate();
}
}
詳細(xì)代碼
//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(); //給有日志的號(hào)碼做標(biāo)記,見(jiàn)后面的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);//根據(jù)參數(shù) b 的值顯示或隱藏此 Window
setBounds(60,60,660,480);
validate();//驗(yàn)證此容器及其所有子組件
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置用戶在此窗體上發(fā)起 "close" 時(shí)默認(rèn)執(zhí)行的操作
}
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) //彈出“警告”消息對(duì)話框
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){ //見(jiàn)后面的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();
}
}
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("打開(kāi)");
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("復(fù)制");
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();
}
};
你可以寫(xiě)4個(gè)界面,1登錄,2主界面(里邊顯示所有日記題目),3寫(xiě)日記(insert就行),4查看日記(點(diǎn)擊事件select查詢),我最近忙考試,只能給你數(shù)據(jù)庫(kù)連接類,其他你照書(shū)上在點(diǎn)擊事件中調(diào)用對(duì)應(yīng)的數(shù)據(jù)庫(kù)方法就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對(duì)象
*/
public void getStatement()throws Exception{
try
{
conn = DriverManager.getConnection(ConnStr);
stmt=conn.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
/**
*
* 關(guān)閉所有連接對(duì)象
*/
public void closeAll() throws SQLException {
try {
stmt.close();
conn.close();
}
catch(SQLException se) {
throw se;
}
}
/**
*
* @param sql查詢語(yǔ)句
* @return ResultSet對(duì)象
*/
public ResultSet getResultSet(String sql) throws Exception{
getStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
/**
*
* @param sql插入更新語(yǔ)句
* @return 受影響行數(shù)
*/
public int executeSql(String sql)throws Exception{
int records=0;
getStatement();
records=stmt.executeUpdate(sql);
return records;
}
}
這是一個(gè)我以前寫(xiě)的簡(jiǎn)單的記事本,里面有新建,保存,另存,打開(kāi)等功能,但是只是邏輯最簡(jiǎn)單的那種,你看看吧,希望對(duì)你有幫助;
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);
// 關(guān)閉
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("打開(kāi)");
jmb.add(fm1);
jmb.addSeparator();
// 打開(kāi)文件
fm1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == fm1) {
FileDialog d = new FileDialog(f, "打開(kāi)文件",
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("請(qǐng)輸入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 關(guān)閉
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = notePadPanel.getTa().getText()
.getBytes();
fout.write(bb);
// 關(guān)閉
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);
// 關(guān)閉
fout.close();
} catch (Exception e4) {
System.out.println(e4.getMessage());
}
}
}
});
fm4 = new JMenuItem("關(guān)閉");
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("復(fù)制");
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("請(qǐng)輸入文件名",
"java");
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = ta.getText().getBytes();
fout.write(bb);
// 關(guān)閉
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
} else {
FileOutputStream fout = new FileOutputStream(
fileName + ".txt");
byte[] bb = ta.getText().getBytes();
fout.write(bb);
// 關(guān)閉
fout.close();
JOptionPane.showMessageDialog(null, "已保存");
}
}
} catch (IOException e1) {
System.out.println(e1.getMessage());
}
}
});
jb1 = new JButton("關(guān)閉");
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);
}
}
新聞標(biāo)題:java代碼寫(xiě)日記本功能 Java 實(shí)現(xiàn)日記軟件
文章出自:http://jinyejixie.com/article36/doohhpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)