import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
//import java.util.StringTokenizer;
//import java.io.FileNotFoundException;
class JieMian extends JFrame implements ActionListener
{
JTextArea jt;
JPanel jp;
JMenuBar mb;
JMenu jm1,jm2,jm3,jm4,jm5;
JMenuItem ji1,ji2,ji3,ji4,ji5,ji6,ji7,ji8,ji9,m1,m2,m3,m4,m5;
public JieMian() {
//f.setTitle("记事本");
//setBounds(100,100,420,220);
mb = new JMenuBar();
this.setJMenuBar(mb);
jt = new JTextArea(20,50);
jp = new JPanel();
jm1 = new JMenu("文件");
jm2 = new JMenu("编辑");
jm3 = new JMenu("格式");
jm4 = new JMenu("查看");
jm5 = new JMenu("帮助");
ji1 = new JMenuItem("新建");
ji2 = new JMenuItem("打开");
ji3 = new JMenuItem("保存");
ji4 = new JMenuItem("另存为");
ji5 = new JMenuItem("退出");
ji6 = new JMenuItem("状态栏");
ji7 = new JMenuItem("自动换行");
ji8 = new JMenuItem("关于记事本");
ji9 = new JMenuItem("查看帮助");
m1 = new JMenuItem("剪切");
m2 = new JMenuItem("复制");
m3 = new JMenuItem("粘贴");
m4 = new JMenuItem("全选");
m5 = new JMenuItem("时间和日期");
jm1.add(ji1);
jm1.add(ji2);
jm1.add(ji3);
jm1.add(ji4);
jm1.add(ji5);
jm3.add(ji7);
jm4.add(ji6);
jm5.add(ji8);
jm5.add(ji9);
jm2.add(m1);
jm2.add(m2);
jm2.add(m3);
jm2.add(m4);
jm2.add(m5);
mb.add(jm1);
mb.add(jm2);
mb.add(jm3);
mb.add(jm4);
mb.add(jm5);
jp.add(jt);
add(jp);
ji1.addActionListener(this);
ji2.addActionListener(this);
ji3.addActionListener(this);
ji4.addActionListener(this);
ji5.addActionListener(this);
ji9.addActionListener(this);
m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
m4.addActionListener(this);
ji7.addActionListener(this);
m5.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ji5)
{
System.exit(0);
}
if(e.getSource()==ji2)
{
JFileChooser fc = new JFileChooser(".");
int i = fc.showOpenDialog(null);
duqu();
}
if(e.getSource()==ji3)
{
/*JFileChooser fc = new JFileChooser(".");
int i = fc.showSaveDialog(null);*/
baocun();
}
if(e.getSource()==ji4)
{
/*JFileChooser fc = new JFileChooser(".");
int i = fc.showOpenDialog(null);*/
baocun1();
}
if(e.getSource()==ji9)
{
JOptionPane.showMessageDialog(null,"记事本文档");
}
if(e.getSource()==ji1)
{
JieMian f1 = new JieMian();
f1.setTitle("记事本");
f1.setSize(200,150);
f1.setLocation(400,300);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
f1.pack();
}
if(e.getSource()==m1)
{
jt.cut();
}
if(e.getSource()==m2)
{
jt.copy();
}
if(e.getSource()==m3)
{
jt.paste();
}
if(e.getSource()==m4)
{
jt.selectAll();
}
if(e.getSource()==ji7)
{
jt.setLineWrap(true);
}
if(e.getSource()==m5)
{
data();
}
}
void duqu(){
try{
FileReader fr = new FileReader("a.txt");
BufferedReader in = new BufferedReader(fr);
String line;
while((line=in.readLine())!=null)
{
String s = new String(line);
jt.setText(s+"\n");
}
}
catch(IOException ee){
ee.printStackTrace();
}
}
void baocun() {
JFileChooser fileDialog = new JFileChooser();
int state=fileDialog.showSaveDialog(this);
if(state==JFileChooser.APPROVE_OPTION){
try
{
File dir = fileDialog.getCurrentDirectory();
String name = fileDialog.getSelectedFile().getName();
File file = new File(dir,name);
FileWriter fileWriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fileWriter);
out.write(jt.getText());
out.flush();
out.close();
fileWriter.close();
}
catch (IOException eee)
{
eee.printStackTrace();
}
}
}
void baocun1() {
JFileChooser fileDialog = new JFileChooser();
int state=fileDialog.showSaveDialog(this);
if(state==JFileChooser.APPROVE_OPTION){
try
{
File dir = fileDialog.getCurrentDirectory();
String name = fileDialog.getSelectedFile().getName();
File file = new File(dir,name);
FileWriter fileWriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fileWriter);
out.write(jt.getText());
out.flush();
out.close();
fileWriter.close();
}
catch (IOException eee)
{
eee.printStackTrace();
}
}
}
void data() {
Date d= new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd/hh/mm/ss ");
String s = sf.format(d);
jt.append(s);
}
}
来自:小站www.250rz.com 提供