package com.rijiben;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.border.BevelBorder;
@SuppressWarnings({ "serial", "unused" })
class FontPopup extends JPopupMenu implements ActionListener{
String[] item;
Font[] font;
JMenuItem mt;
JTextArea t;
FontPopup(JTextArea t){
this.t=t;
this.setBorder(new BevelBorder(BevelBorder.RAISED));
item=new String[]{"标准仿宋","粗放行楷","规矩新魏","精制宋体","灵感隶书"};
font =new Font[]{
new Font("",0,18),
new Font("",0,30),
new Font("",0,22),
new Font("",0,14),
new Font("",0,20)
};
for(int i=0;i<item.length;i++){
mt=new JMenuItem(item[i]);
mt.setFont(new Font("仿宋",0,14));
this.add(mt);
mt.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
int i;
for(i=0;i<item.length;i++)
if(s.equals(item[i])) break;
t.setFont(font[i]);
}
}
@SuppressWarnings("serial")
class JTextAreaWithMenu extends JTextArea implements MouseListener{
FontPopup fp;
JTextAreaWithMenu(int i,int j){
super(i,j);
fp=new FontPopup(this);
this.addMouseListener(this);
this.add(fp);
}
@SuppressWarnings("static-access")
public void mousePressed(MouseEvent e){
if(e.getButton()==e.BUTTON3)
fp.show(this,e.getX(),e.getY());
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}
@SuppressWarnings("serial")
public class NotePad extends JPanel implements ActionListener
{
//JTextArea text;
JTextAreaWithMenu text;
JButton 保存日志,删除日志;
@SuppressWarnings("unchecked")
Hashtable table;
JLabel 信息条;
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();
信息条=new JLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);
信息条.setFont(new Font("TimesRoman",Font.BOLD,16));
信息条.setForeground(Color.blue);
text=new JTextAreaWithMenu (30,70);
//text=new JTextArea(35,90);
保存日志=new JButton("保存日志") ;
删除日志=new JButton("删除日志") ;
保存日志.addActionListener(this);
删除日志.addActionListener(this);
setLayout(new BorderLayout());
JPanel pSouth=new JPanel();
add(信息条,BorderLayout.NORTH);
pSouth.add(保存日志);
pSouth.add(删除日志);
add(pSouth,BorderLayout.SOUTH);
add(new JScrollPane(text),BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==保存日志)
{
保存日志(year,month,day);
}
else if(e.getSource()==删除日志)
{
删除日志(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 设置信息条(int year,int month,int day)
{
信息条.setText(""+year+"年"+month+"月"+day+"日");
}
public void 设置文本区(String s)
{
text.setText(s);
}
@SuppressWarnings("unchecked")
public void 获取日志内容(int year,int month,int day)
{
String key=""+year+""+month+""+day;
try
{
FileInputStream inOne=new FileInputStream(file);
ObjectInputStream inTwo=new ObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee)
{
}
if(table.containsKey(key))
{
String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
text.setText((String)table.get(key));
}
else
{
text.setText("");
}
}
else
{
text.setText("无记录");
}
}
@SuppressWarnings("unchecked")
public void 保存日志(int year,int month,int day)
{
String 日志内容=text.getText();
String key=""+year+""+month+""+day;
String m=""+year+"年"+month+"月"+day+"保存日志吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStream inOne=new FileInputStream(file);
ObjectInputStream inTwo=new ObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.put(key,日志内容);
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
}
catch(Exception ee)
{
}
}
}
@SuppressWarnings("unchecked")
public void 删除日志(int year,int month,int day)
{String key=""+year+""+month+""+day;
if(table.containsKey(key))
{
String m="删除"+year+"年"+month+"月"+day+"日的日志吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStream inOne=new FileInputStream(file);
ObjectInputStream inTwo=new ObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.remove(key);
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
text.setText(null);
}
catch(Exception ee)
{
}
}
}
else
{
String m=""+year+"年"+month+"月"+day+"无日志记录";
JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);
}
}
}