第十四周课程总结&实验报告(简单记事本的实现)
一、记事本
1.实验代码
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
class TextPad implements ActionListener{
public static void main(String[] args) {
new TextPad();
}
public TextPad(){
JFrame frame = new JFrame("记事本");
JMenuBar bar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenu editMenu = new JMenu("编辑");
ImageIcon newIcon = new ImageIcon("d" + File.separator + "Java" + File.separator + "作业" + File.separator + "新建.jpg");
ImageIcon openIcon = new ImageIcon("d" + File.separator + "Java" + File.separator + "作业" + File.separator + "打开.jpg");
ImageIcon saveIcon = new ImageIcon("d" + File.separator + "Java" + File.separator + "作业" + File.separator + "另存为.jpg");
ImageIcon closeIcon = new ImageIcon("d" + File.separator + "Java" + File.separator + "作业" + File.separator + "关闭.jpg");
JMenuItem newItem = new JMenuItem("新建(N)", newIcon);
JMenuItem openItem = new JMenuItem("打开(O)", openIcon);
JMenuItem saveItem = new JMenuItem("另存为(A)", saveIcon);
JMenuItem closeItem = new JMenuItem("关闭(X)", closeIcon);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
newItem.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.CTRL_MASK));
newItem.addActionListener((ActionListener) this);
openItem.addActionListener((ActionListener) this);
saveItem.addActionListener((ActionListener) this);
closeItem.addActionListener((ActionListener) this);
bar.add(fileMenu);
bar.add(editMenu);
frame.add(bar);
frame.setJMenuBar(bar);
frame.setSize(400,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
Object obj = event.getSource();
if (obj instanceof JMenuItem){
JMenuItem item = (JMenuItem) obj;
JMenuItem newItem = new JMenuItem();
JMenuItem openItem = new JMenuItem();
JMenuItem saveItem = new JMenuItem();
JMenuItem closeItem = new JMenuItem();
if (item.equals(newItem)){
new TextPad();
}else if (item == openItem){
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
Object file = chooser.getSelectedFile();
try {
FileInputStream fis = new FileInputStream((File) file);
byte[] b = new byte[fis.available()];
fis.read(b);
PrintWriter area = null;
area.append(new String(b));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if (item == saveItem){
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
File file = chooser.getSelectedFile();
try{
if (!file.exists()){
file.createNewFile();
}
FileInputStream fos = new FileInputStream(file);
AbstractButton area = null;
assert false;
byte[] b =area.getText().getBytes();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}else if (item == closeItem){
System.exit(1);
}
}
}
}
2.实验结果截图
改进后的记事本
1.实验代码
package cn;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
public class TextPad implements ActionListener {
public static void main(String[] args) {
new TextPad();
}
JMenuItem newItem;
JMenuItem openItem;
JMenuItem saveItem;
JMenuItem closeItem;
JTextArea textArea;
public TextPad() {
JFrame frame = new JFrame("记事本");
JMenuBar bar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenu editMenu = new JMenu("编辑");
ImageIcon newIcon = new ImageIcon("D:\Java\1.基础班\untitled2\src\cn\iamges\新建.png");
ImageIcon openIcon = new ImageIcon("D:\Java\1.基础班\untitled2\src\cn\iamges\打开.png");
ImageIcon saveIcon = new ImageIcon("D:\Java\1.基础班\untitled2\src\cn\iamges\另存为.png");
ImageIcon closeIcon = new ImageIcon("D:\Java\1.基础班\untitled2\src\cn\iamges\关闭.png");
newItem = new JMenuItem("新建(N)", newIcon);
openItem = new JMenuItem("打开(O)", openIcon);
saveItem = new JMenuItem("另存为(S)", saveIcon);
closeItem = new JMenuItem("关闭(X)", closeIcon);
//文件菜单里的快捷键
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
//文件
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
//添加文本域与滚动条
textArea = new JTextArea();
frame.getContentPane().add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
textArea.setEditable(true);
//添加菜单选项到菜单栏
bar.add(fileMenu);
bar.add(editMenu);
//添加菜单栏
frame.setJMenuBar(bar);
frame.setSize(800, 500);
frame.setLocation(500, 240);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj instanceof JMenuItem) {
JMenuItem item = (JMenuItem) obj;
if (item.equals(newItem)) {
new TextPad();
} else if (item == openItem) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
Object file = chooser.getSelectedFile();
try {
FileInputStream fis = new FileInputStream((File) file);
byte[] b = new byte[fis.available()];
fis.read(b);
textArea.append(new String(b));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (item == saveItem) { //另存为
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
File file = chooser.getSelectedFile();
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
byte[] b = textArea.getText().getBytes();
fos.write(b);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} else if (item == closeItem) {
System.exit(1);
}
}
}
}
2.实验结果截图
3.改进后:
代码的错误减少了
(1)添加上了小图标
(2)可以打开和建立窗口了
(3)可以另存为
(4)实现了close关闭