您的位置:首页 > 编程语言 > Java开发

一个简单的java记事本程序

2013-10-28 19:06 591 查看
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.util.regex.Pattern;

import java.util.regex.Matcher;

import java.io.*;

public class diary extends JFrame{

static String file="a.ou";

JFrame f=new JFrame("日记本");

Container p=f.getContentPane();

Calendar c1=Calendar.getInstance();

File iconDir = new File("心情表情");

JOptionPane jop=new JOptionPane();

File file1=null;

final File[] iconFiles = iconDir.listFiles(new FileFilter() {

public boolean accept(File file)

{

return file.getName().toLowerCase().endsWith(".gif");

}

});

JLabel jlabel1=new JLabel("标 题"), //建立标签

jlabel2= new JLabel("日 期"),

jlabel3=new JLabel("天 气"),

jlabel4=new JLabel("心 情");

JTextArea

t4=new JTextArea(1,8),

t3=new JTextArea(1,8),

t2=new JTextArea(1,20),

t1=new JTextArea(40,80);

String[] description={

"阴天","晴天","下雨了","起风了", //天气组合框

"下雪了","有点热","有点冷"};

JComboBox c=new JComboBox();

final JComboBox comboBox = new JComboBox(iconFiles);

JPanel

pp=new JPanel(),

pa=new JPanel(),

ppanel=new JPanel();

FileDialog fd;

JButton //建立按钮

save=new JButton("保存日记"),

open=new JButton("查看日记"),

newFile =new JButton("新建日记"),

about=new JButton("关于"),

search=new JButton("检索日记");

JScrollPane jscrollpane=new JScrollPane(t1);//建立滚动条

public diary(){ //把组件加入到面板中

ppanel.add(newFile);

ppanel.add(save);

ppanel.add(open);

ppanel.add(search);

ppanel.add(about);

pa.add(jlabel1);

pa.add(t2);

pa.add(jlabel3);

pa.add(c);

pa.add(jlabel4);

pa.add(comboBox);

pa.add(jlabel2);

pa.add(t3);

pp.add(t4);

t3.append(c1.get(Calendar.YEAR)+

" 年 "+(c1.get(Calendar.MONTH)+1)+

" 月 " + c1.get(Calendar.DATE)+"日");

t4.append(c1.get(Calendar.YEAR)+

"."+(c1.get(Calendar.MONTH)+1)+

"." + c1.get(Calendar.DATE));

newFile.addActionListener(new ActionListener(){ //新建日记

public void actionPerformed(ActionEvent e){

save.setEnabled(true);

t1.setText(" ");

t2.setText(" ");

t1.append(c.getSelectedItem()+"\r\n"); }

});

open.addActionListener(new ActionListener(){ //查看日记

public void actionPerformed(ActionEvent e){

String s1=null;

s1=jop.showInputDialog("请输入你要查看的日期,如2013.6.1\n(默认一天只有一篇日记,只显示第一篇的查找结果)");

try

{

FileReader fr=new FileReader("diary.txt");

BufferedReader br=new BufferedReader(fr); Pattern keyword1=Pattern.compile("[0-9]{4} \\.
\\d{1,2}
\\.\\d{1,1}");

String s3;

String s4="******";//日记之间的分隔符

int x=0;

t1.setText(" ");

t2.setText(" ");

t1.setVisible(true);

while((s3=br.readLine())!=null){

Matcher matcher1=keyword1.matcher(s3);

if(s3.equalsIgnoreCase(s1)||x==1){

if(s3.equalsIgnoreCase(s4))

break;

x=1;

t1.append(s3+"\r\n");

save.setEnabled(false);

}

}

if(x==0)

jop.showMessageDialog(f,"你查找的日期不存在,\n请重新点击'打开日记',再次输入!");

fr.close();

br.close();

}catch (IOException ioe){}

}

});

save.addActionListener(new ActionListener(){ //保存日记

public void actionPerformed(ActionEvent e){

try{

FileWriter fw=new FileWriter("diary.txt",true);

fw.append("****"+"\r\n");

fw.append(t4.getText()+"\r\n");

fw.append("标 题:"+t2.getText()+"\r\n");

fw.append(t1.getText()+"\r\n"+"******"+"\r\n");

fw.close();

save.setEnabled(false);

}

catch (IOException ioe){ }

}

});

about.addActionListener(new ActionListener(){ //关于日记本

public void actionPerformed(ActionEvent e){

t1.setText(" ");

t2.setText(" ");

save.setEnabled(false);

jop.showMessageDialog(f,"感谢你使用小飞日记本\n制作人:张小飞.\n联系电话:1 8 8 8 8 9 9 9 9 8 8\n邮箱:1 1 1 1 1 1 1 1 1 @ 168.com\n版权所有,免费使用.\n若有建议或不满,欢迎致电或发送邮件.\n你的支持与建议一直是我不懈的动力!");

}

});

search.addActionListener(new ActionListener(){ //检索日记

public void actionPerformed(ActionEvent e){

String s2=null; s2=jop.showInputDialog("请输入你要检索的天气\n(该功能只对该天气进行累加,只对一下5种天气提供检索,晴天,阴天,下雨了,有点热,有点冷)\n如晴天"); try

{

FileReader ff=new FileReader("diary.txt");

BufferedReader bb=new BufferedReader(ff);

String s5;

int y=0;

while((s5=bb.readLine())!=null){

if(s2.equalsIgnoreCase(s5))

y++;

}

jop.showMessageDialog(f,s2+"数为:"+y);

ff.close();

bb.close();

}catch (IOException ioe){}

/* while(s2==null)

s2=jop.showInputDialog("请再次准确输入,4种天气,晴天,阴天,下雨了,有点热");

String message=String.format("%s", s2);

jop.showMessageDialog(f,message);*/

}

});

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

c.addItem(description[i]);

c.addActionListener(new ActionListener() { //天气组合框操作

public void actionPerformed(ActionEvent e) {

((JComboBox)e.getSource()).getSelectedItem();

}

});

f.add(jscrollpane); //添加滚动条

p.add(ppanel,BorderLayout.SOUTH); //设置布局

p.add(pa,BorderLayout.SOUTH);

t1.setLineWrap(true); //自动换行

jop.showMessageDialog(f,"注意:在选好天气后,在点击新建日记,\n当窗口中有你选择的天气时,说明天气选择成功。");

final Map<File, ImageIcon> fileIconMap = new HashMap<File, ImageIcon>(); //添加心情图标框

comboBox.setRenderer(new DefaultListCellRenderer() {

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)

{

ImageIcon oldIcon = (ImageIcon)getIcon();

if (oldIcon != null) {

oldIcon.setImageObserver(null);

}

File iconFile = (File) value;

ImageIcon newIcon = fileIconMap.get(iconFile);

if (newIcon == null) {

newIcon = new ImageIcon(iconFile.getPath());

fileIconMap.put(iconFile, newIcon);

}

if (newIcon != null) {

if (index == -1) {

newIcon.setImageObserver(comboBox);

}

else {

newIcon.setImageObserver(list);

}

}

String text = iconFile.getName();

super.getListCellRendererComponent(

list, text, index, isSelected, cellHasFocus);

setIcon(newIcon);

return this;

}

}); //心情图标到此结束

f.setSize(800,600);

f.setVisible(true);

f.pack(); //自动调节尺寸

f. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出

}

public static void main(String[] args)

{

diary mm=new diary();

}

}

关键代码已加红

ps:可能有点大,没办法,加了些功能,也是全部代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: