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){}
}
});
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:可能有點大,沒辦法,加了些功能,也是全部代碼