package notebook;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
public class NotePad extends JFrame implements ActionListener {
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenuItem jmi1=null;
JMenuItem jmi2=null;
public NotePad()
{
jta=new JTextArea();
jmb=new JMenuBar();
jm1=new JMenu("文件");
//设置助记符
jm1.setMnemonic('F');
this.add(jta);
jmi1=new JMenuItem("打开",new ImageIcon("file.png"));
jmi1.addActionListener(this);
jmi1.setActionCommand("open");
jmi2=new JMenuItem("保存",new ImageIcon("save.png"));
jmi2.addActionListener(this);
jmi2.setActionCommand("save");
this.setJMenuBar(jmb);
jmb.add(jm1);
jm1.add(jmi1);
jm1.add(jmi2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,300);
setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open"))
{
JFileChooser jfc1=new JFileChooser();
jfc1.setDialogTitle("请选择文件");
jfc1.showOpenDialog(null);
jfc1.setVisible(true);
String filename=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try
{
fr=new FileReader(filename);
br=new BufferedReader(fr);
String s="";
String allCon="";
while((s=br.readLine())!=null)
{
allCon+=s+"\r\n";
}
jta.setText(allCon);
}
catch (Exception e2)
{
// TODO: handle exception
}
finally
{
try
{
br.close();
fr.close();
}
catch (Exception e3)
{
}
}
}
if(e.getActionCommand().equals("save"))
{
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为");
jfc.showSaveDialog(null);
jfc.setVisible(true);
String file=jfc.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try
{
fw=new FileWriter(file);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
}
catch (Exception e2)
{
// TODO: handle exception
}
finally
{
try {
bw.close();
fw.close();
} catch (Exception e3) {
// TODO: handle exception
}
}
}
}
}
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
public class NotePad extends JFrame implements ActionListener {
JTextArea jta=null;
JMenuBar jmb=null;
JMenu jm1=null;
JMenuItem jmi1=null;
JMenuItem jmi2=null;
public NotePad()
{
jta=new JTextArea();
jmb=new JMenuBar();
jm1=new JMenu("文件");
//设置助记符
jm1.setMnemonic('F');
this.add(jta);
jmi1=new JMenuItem("打开",new ImageIcon("file.png"));
jmi1.addActionListener(this);
jmi1.setActionCommand("open");
jmi2=new JMenuItem("保存",new ImageIcon("save.png"));
jmi2.addActionListener(this);
jmi2.setActionCommand("save");
this.setJMenuBar(jmb);
jmb.add(jm1);
jm1.add(jmi1);
jm1.add(jmi2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,300);
setVisible(true);
}
public static void main(String[] args) {
new NotePad();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open"))
{
JFileChooser jfc1=new JFileChooser();
jfc1.setDialogTitle("请选择文件");
jfc1.showOpenDialog(null);
jfc1.setVisible(true);
String filename=jfc1.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try
{
fr=new FileReader(filename);
br=new BufferedReader(fr);
String s="";
String allCon="";
while((s=br.readLine())!=null)
{
allCon+=s+"\r\n";
}
jta.setText(allCon);
}
catch (Exception e2)
{
// TODO: handle exception
}
finally
{
try
{
br.close();
fr.close();
}
catch (Exception e3)
{
}
}
}
if(e.getActionCommand().equals("save"))
{
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为");
jfc.showSaveDialog(null);
jfc.setVisible(true);
String file=jfc.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try
{
fw=new FileWriter(file);
bw=new BufferedWriter(fw);
bw.write(this.jta.getText());
}
catch (Exception e2)
{
// TODO: handle exception
}
finally
{
try {
bw.close();
fw.close();
} catch (Exception e3) {
// TODO: handle exception
}
}
}
}
}