package cn.itcast.day20.pm;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Notepad {
private Frame frame; // 由于frame在多个方法中都要访问, 所以定义成成员变量
private TextArea textArea;
public Notepad() {
// 创建窗体
createFrame();
// 给窗体添加菜单栏
createMenuBar();
// 给窗体添加文本域
createTextArea();
// 显示
frame.setVisible(true);
}
public Notepad(File file) {
createFrame();
createMenuBar();
createTextArea();
frame.setVisible(true);
loadFile(file);
}
private void createTextArea() {
textArea = new TextArea();
frame.add(textArea); // 默认BorderLayout, 默认CENTER
textArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_O) // ctrl + o
open();
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_S) // ctrl + s
save();
}
});
}
private void createMenuBar() {
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("文件");
MenuItem openMenuItem = new MenuItem("打开");
MenuItem saveMenuItem = new MenuItem("保存");
MenuItem exitMenuItem = new MenuItem("退出");
menu.add(openMenuItem);
menu.add(saveMenuItem);
menu.addSeparator(); // 添加分割线
menu.add(exitMenuItem);
menuBar.add(menu);
frame.setMenuBar(menuBar);
exitMenuItem.addActionListener(new ActionListener() { // 给关闭菜单项添加事件处理
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
openMenuItem.addActionListener(new ActionListener() { // 给打开菜单项添加事件处理
public void actionPerformed(ActionEvent e) {
open(); // 调用打开方法
}
});
saveMenuItem.addActionListener(new ActionListener() { // 给保存菜单项添加事件处理
public void actionPerformed(ActionEvent e) {
save(); // 调用保存方法
}
});
}
private void open() {
FileDialog openFileDialog = new FileDialog(frame, "打开", FileDialog.LOAD); // 创建一个对话框, 父级窗口是frame, 标题是"打开", 模式是LOAD
openFileDialog.setVisible(true); // 显示对话框
String path = openFileDialog.getDirectory(); // 获取文件夹路径
String name = openFileDialog.getFile(); // 获取文件名
if (path != null && name != null) {
File file = new File(path, name); // 根据路径和文件名创建文件
loadFile(file); // 将file中的内容加载到TextArea中
}
}
private void loadFile(File file) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
textArea.setText(""); // 每次读取之前, 先设置文本域内容为空
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
sb.append(line + "/r/n"); // 从文件中读, 先添到StringBuilder中
textArea.setText(sb.toString()); // 都读完之后, 再一次性写入到textArea
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void save() {
FileDialog saveFileDialog = new FileDialog(frame, "保存", FileDialog.SAVE);
saveFileDialog.setVisible(true);
String path = saveFileDialog.getDirectory(); // 获取文件夹路径
String name = saveFileDialog.getFile(); // 获取文件名
if (path != null && name != null) {
File file = new File(path, name); // 根据路径和文件名创建文件
saveFile(file);
}
}
private void saveFile(File file) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText()); // 从textArea中获取, 写到文件中
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void createFrame() {
frame = new Frame("记事本");
frame.setSize(600, 400);
frame.setLocation(200, 100);
frame.setIconImage(Toolkit.getDefaultToolkit().createImage("src/300png059.png"));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
});
}
public static void main(String[] args) {
new Notepad();
}
}
package cn.itcast.day20.pm;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
public class Explorer {
private Frame frame;
private TextField textField;
private List list;
public Explorer() {
createFrame(); // 创建Frame
createPanel(); // 创建Panel, 其中放入了TextField和Button
createList(); // 创建List
frame.setVisible(true);
}
private void createList() {
list = new List();
frame.add(list);
list.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oldPath = textField.getText();
String newPath = list.getSelectedItem(); // 获取list中被选中的文本
File file = new File(oldPath, newPath);
textField.setText(file.getAbsolutePath()); // 将文本框中的内容更新
turn();
}
});
}
private void createPanel() {
Panel panel = new Panel();
textField = new TextField(50); // 创建文本框, 指定列数
panel.add(textField);
Button turnButton = new Button("转到");
panel.add(turnButton);
Button upButton = new Button("向上");
panel.add(upButton);
frame.add(panel, BorderLayout.NORTH);
turnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
turn();
}
});
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
turn();
}
});
upButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oldPath = textField.getText(); // 获取之前文本框中的内容
File file = new File(oldPath); // 封装成File对象
String newPath = file.getParent(); // 获取父级路径
textField.setText(newPath); // 将文本框中的内容设置为父级路径
turn(); // 跳转
}
});
}
private void turn() {
list.removeAll(); // 每次跳转之前先删除以前的内容
String path = textField.getText(); // 获取文本框中文本
if (path != null && !"".equals(path)) {
File file = new File(path);
if (file.isDirectory()) { // 判断如果是存在的文件夹
String[] names = file.list();
for (String name : names)
list.add(name);
} else if (file.isFile()) { // 判断如果是存在的文件
if (file.getName().endsWith(".java") || file.getName().endsWith(".txt")) {
new Notepad(file);
} else { // 除了txt和java, 其他都用系统默认打开
try {
Runtime.getRuntime().exec("cmd /c " + file.getAbsolutePath()); // 使用系统默认工具打开
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private void createFrame() {
frame = new Frame("Explorer");
frame.setSize(800, 600);
frame.setLocation(200, 100);
frame.setIconImage(Toolkit.getDefaultToolkit().createImage("src/png-1487.png"));
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
}
public static void main(String[] args) {
new Explorer();
}
}