📄 newnotepad.java
字号:
package exec.day1016;
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.awt.event.*;
public class NewNotePad extends JFrame implements ActionListener{
private JTextArea area;
private JScrollPane jsp;
private Font font;
private JMenuBar bar;//菜单条
private JMenu file;//菜单
private JMenuItem open;//菜单项
private JMenuItem save;
private JMenuItem color;
private JMenuItem exit;
public void init(){
font = new Font("楷体",Font.BOLD,20);
this.setTitle("NotePad");
//中间添加多行文本域
area = new JTextArea();
area.setFont(font);
jsp = new JScrollPane(area);
this.add(jsp);
//菜单条部分
bar = new JMenuBar();
file = new JMenu("文件");
open = new JMenuItem("打开");
save = new JMenuItem("保存");
color = new JMenuItem("颜色");
exit = new JMenuItem("退出");
//把菜单项添加到菜单上
file.add(open);
file.add(save);
file.add(color);
//添加分割线
file.addSeparator();
file.add(exit);
//把菜单添加到菜单条上
bar.add(file);
//把菜单条添加到顶层容器中
this.setJMenuBar(bar);
}
public void eventHandle(){
open.addActionListener(this);
save.addActionListener(this);
color.addActionListener(this);
exit.addActionListener(this);
}
class FileFilterTest extends FileFilter{
private String name;//文本文件(*.txt)
public FileFilterTest(String name){
this.name = name;
}
public boolean accept(File f){
if(f.isDirectory()){
return true;
}
int startIndex = name.indexOf("(*.");
String sub = name.substring(startIndex+2,name.length()-1);
String fileName = f.getName();
// System.out.println(sub);
// System.out.println(fileName);
return fileName.endsWith(sub);
}
public String getDescription(){
return name;
}
}
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s.equals("打开")){
area.setText("");
//构建文件选择器
JFileChooser fc = new JFileChooser();
//打开文件选择对话框,并指定父容器
// JFrame f = new JFrame();
// f.getContentPane()
fc.addChoosableFileFilter(new FileFilterTest("文本文件(*.txt)"));
fc.addChoosableFileFilter(new FileFilterTest("字节码文件(*.class)"));
fc.addChoosableFileFilter(new FileFilterTest("Java源文件(*.java)"));
fc.addChoosableFileFilter(new FileFilterTest("PPT文件(*.ppt)"));
fc.showOpenDialog(this);
//获取选中的文件
File f = fc.getSelectedFile();
if(f==null) return;
//构建文件输入流
FileReader fr = null;
try {
fr = new FileReader(f);
//读取文件内容,并设置到多行文本域中
int data = fr.read();
while(data!=-1){
area.append((char)data+"");
data = fr.read();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally{
try {
fr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
if(s.equals("保存")){
JFileChooser fc = new JFileChooser();
fc.showSaveDialog(this);
File f = fc.getSelectedFile();
if(f==null) return;
//构建文件输出流
FileWriter fw = null;
try {
fw = new FileWriter(f);
fw.write(area.getText());
} catch (IOException e1) {
e1.printStackTrace();
} finally{
try {
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
if(s.equals("颜色")){
//构建颜色选择器
JColorChooser cc = new JColorChooser();
//打开颜色选择对话框
Color c = cc.showDialog(this,"请选择颜色",Color.RED);
area.setForeground(c);
}
if(s.equals("退出")){
System.exit(0);
}
}
public void showMe(){
this.setSize(800,700);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
NewNotePad n = new NewNotePad();
n.init();
n.eventHandle();
n.showMe();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -