package txt;
/**
* 记事本
*/
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class Txt extends JFrame implements ActionListener {
private JScrollPane jScrollPane = new JScrollPane();
private JTextArea jTextArea = new JTextArea();
private JMenuBar jMenuBar = new JMenuBar();// 菜单栏
private JMenu jMenu = new JMenu("文件(F)");// 菜单
private JMenu jMenu2 = new JMenu("编辑(E)");
private JMenu jMenu3 = new JMenu("格式(O)");
private JMenu jMenu4 = new JMenu("查看(V)");
private JMenu jMenu5 = new JMenu("帮助(H)");
private JMenuItem jMenuItem = new JMenuItem("新建(N)");
private JMenuItem jMenuItem2 = new JMenuItem("打开(O)");
private JMenuItem jMenuItem3 = new JMenuItem("保存(S)");
private JMenuItem jMenuItem4 = new JMenuItem("退出(X)");
private JMenuItem jMenuItem21 = new JMenuItem("撤销(U)");
private JMenuItem jMenuItem22 = new JMenuItem("剪切(T)");
private JMenuItem jMenuItem23 = new JMenuItem("复制(C)");
private JMenuItem jMenuItem24 = new JMenuItem("粘贴(P)");
public Txt() {
this.setSize(500, 400);
this.setLocationRelativeTo(null);// 设置位置在中间
this.setTitle("我的记事本");// title
this.setJMenuBar(jMenuBar);
this.jScrollPane.getViewport().add(jTextArea);
this.setContentPane(this.jScrollPane);
this.jMenuBar.add(jMenu);
this.jMenuBar.add(jMenu2);
this.jMenuBar.add(jMenu3);
this.jMenuBar.add(jMenu4);
this.jMenuBar.add(jMenu5);
this.jMenu.add(jMenuItem);
this.jMenu.add(jMenuItem2);
this.jMenu.add(jMenuItem3);
this.jMenu.add(jMenuItem4);
this.jMenu2.add(jMenuItem21);
this.jMenu2.add(jMenuItem22);
this.jMenu2.add(jMenuItem23);
this.jMenu2.add(jMenuItem24);
this.jMenuItem.addActionListener(this);
this.jMenuItem2.addActionListener(this);
this.jMenuItem3.addActionListener(this);
this.jMenuItem4.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("新建(N)")) {
this.jTextArea.setText("");
}
if (e.getActionCommand().equals("打开(O)")) {
System.out.println("打开....");
/*
* 步骤 利用 window 文件对话框控件
*/
// 在当前窗体创建1个文件对话框控件
FileDialog fileDialog = new FileDialog(this);
fileDialog.setVisible(true);// 显示出来
// 处理 验证 用户是否点击选择了文件
String filename = fileDialog.getFile();
System.out.println("filename" + filename);
if (filename != null) {
// 选择了文件
// 文件名称 MyNotePad.java
// 文件路径
String path = fileDialog.getDirectory();
// 全路径:路径 +文件名
path = path + filename;
// System.out.println(path);
// 文件操作 <=>内容<=>文本域
File file = new File(path);
if (file.exists()) {
// 字符
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(
fileReader);
char[] c = new char[(int) file.length()];
bufferedReader.read(c);
// char <=>String
String s = new String(c);
// 内容<=>文本域
this.jTextArea.setText(s);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
if (e.getActionCommand().equals("保存(S)")) {
FileDialog fileDialog = new FileDialog(this, "请选择保存的路径",
FileDialog.SAVE);
fileDialog.setVisible(true);
String filename = fileDialog.getFile();
String path2 = fileDialog.getDirectory();
File file2 = null;
if (!filename.contains(".txt")) {
file2 = new File(path2 + filename + ".txt");
} else {
file2 = new File(path2 + filename);
}
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
FileWriter fileWriter = new FileWriter(file2, false);
fileWriter.write(this.jTextArea.getText());
fileWriter.close();
// System.out.println();
JOptionPane.showMessageDialog(this, "写入成功");
}
catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getActionCommand().equals("退出(X)")) {
System.out.println("退出...");
System.exit(0);
}
}
public static void main(String[] args) {
Txt txt = new Txt();
}
/**
* 记事本
*/
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class Txt extends JFrame implements ActionListener {
private JScrollPane jScrollPane = new JScrollPane();
private JTextArea jTextArea = new JTextArea();
private JMenuBar jMenuBar = new JMenuBar();// 菜单栏
private JMenu jMenu = new JMenu("文件(F)");// 菜单
private JMenu jMenu2 = new JMenu("编辑(E)");
private JMenu jMenu3 = new JMenu("格式(O)");
private JMenu jMenu4 = new JMenu("查看(V)");
private JMenu jMenu5 = new JMenu("帮助(H)");
private JMenuItem jMenuItem = new JMenuItem("新建(N)");
private JMenuItem jMenuItem2 = new JMenuItem("打开(O)");
private JMenuItem jMenuItem3 = new JMenuItem("保存(S)");
private JMenuItem jMenuItem4 = new JMenuItem("退出(X)");
private JMenuItem jMenuItem21 = new JMenuItem("撤销(U)");
private JMenuItem jMenuItem22 = new JMenuItem("剪切(T)");
private JMenuItem jMenuItem23 = new JMenuItem("复制(C)");
private JMenuItem jMenuItem24 = new JMenuItem("粘贴(P)");
public Txt() {
this.setSize(500, 400);
this.setLocationRelativeTo(null);// 设置位置在中间
this.setTitle("我的记事本");// title
this.setJMenuBar(jMenuBar);
this.jScrollPane.getViewport().add(jTextArea);
this.setContentPane(this.jScrollPane);
this.jMenuBar.add(jMenu);
this.jMenuBar.add(jMenu2);
this.jMenuBar.add(jMenu3);
this.jMenuBar.add(jMenu4);
this.jMenuBar.add(jMenu5);
this.jMenu.add(jMenuItem);
this.jMenu.add(jMenuItem2);
this.jMenu.add(jMenuItem3);
this.jMenu.add(jMenuItem4);
this.jMenu2.add(jMenuItem21);
this.jMenu2.add(jMenuItem22);
this.jMenu2.add(jMenuItem23);
this.jMenu2.add(jMenuItem24);
this.jMenuItem.addActionListener(this);
this.jMenuItem2.addActionListener(this);
this.jMenuItem3.addActionListener(this);
this.jMenuItem4.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("新建(N)")) {
this.jTextArea.setText("");
}
if (e.getActionCommand().equals("打开(O)")) {
System.out.println("打开....");
/*
* 步骤 利用 window 文件对话框控件
*/
// 在当前窗体创建1个文件对话框控件
FileDialog fileDialog = new FileDialog(this);
fileDialog.setVisible(true);// 显示出来
// 处理 验证 用户是否点击选择了文件
String filename = fileDialog.getFile();
System.out.println("filename" + filename);
if (filename != null) {
// 选择了文件
// 文件名称 MyNotePad.java
// 文件路径
String path = fileDialog.getDirectory();
// 全路径:路径 +文件名
path = path + filename;
// System.out.println(path);
// 文件操作 <=>内容<=>文本域
File file = new File(path);
if (file.exists()) {
// 字符
try {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(
fileReader);
char[] c = new char[(int) file.length()];
bufferedReader.read(c);
// char <=>String
String s = new String(c);
// 内容<=>文本域
this.jTextArea.setText(s);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
if (e.getActionCommand().equals("保存(S)")) {
FileDialog fileDialog = new FileDialog(this, "请选择保存的路径",
FileDialog.SAVE);
fileDialog.setVisible(true);
String filename = fileDialog.getFile();
String path2 = fileDialog.getDirectory();
File file2 = null;
if (!filename.contains(".txt")) {
file2 = new File(path2 + filename + ".txt");
} else {
file2 = new File(path2 + filename);
}
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
FileWriter fileWriter = new FileWriter(file2, false);
fileWriter.write(this.jTextArea.getText());
fileWriter.close();
// System.out.println();
JOptionPane.showMessageDialog(this, "写入成功");
}
catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getActionCommand().equals("退出(X)")) {
System.out.println("退出...");
System.exit(0);
}
}
public static void main(String[] args) {
Txt txt = new Txt();
}
}