代码:主框架
package Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class TextFrame extends JFrame{
public TextFrame()
{
super("简易文本编辑器");
//创建菜单栏(JMenuBar)对象
JMenuBar mBar = new JMenuBar();
//在JFrame等容器中设置菜单栏对象,即将菜单栏添加到框架容器中
this.setJMenuBar(mBar);
//创建菜单对象
JMenu file = new JMenu("文件");
JMenu edit = new JMenu("编辑");
JMenu form = new JMenu("格式");
JMenu help = new JMenu("帮助");
//将菜单添加到菜单栏中
mBar.add(file);
mBar.add(edit);
mBar.add(form);
mBar.add(help);
JTextArea workArea = new JTextArea();
JScrollPane imgScrollPane = new JScrollPane(workArea);
//add(workArea);
add(imgScrollPane,BorderLayout.CENTER);
//定义打开和保存对话框
FileDialog openDia;
FileDialog saveDia;
//默认模式为 FileDialog.LOAD
openDia = new FileDialog(this,"打开(O)",FileDialog.LOAD);
saveDia = new FileDialog(this,"另存为(A)",FileDialog.SAVE);
JMenuItem item1_1 = new JMenuItem("新建(N)");
item1_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
workArea.setText("");//清空文本
}
});
JMenuItem item1_2 = new JMenuItem("打开(O)");
item1_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
openDia.setVisible(true);
String dirPath = openDia.getDirectory();//获取文件路径
String fileName = openDia.getFile();//获取文件名称
//如果打开路径 或 目录为空 则返回空
if(dirPath == null || fileName == null){
return;
}
workArea.setText("");//清空文本
File fileO = new File(dirPath,fileName);
try{
BufferedReader bufr = new BufferedReader(new FileReader(fileO));
String line = null;
while((line = bufr.readLine()) != null){
workArea.append(line + "\r\n");
}
bufr.close(); //关闭文本
}catch(IOException er1){
throw new RuntimeException("文件读取失败!");
}
}
});
JMenuItem item1_3 = new JMenuItem("保存(S)");
item1_3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File fileS = null;
if(fileS == null){
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath == null || fileName == null)
return;
fileS = new File(dirPath,fileName);
}
try{
BufferedWriter bufw = new BufferedWriter(new FileWriter(fileS));
String text = workArea.getText();
bufw.write(text);
bufw.close();
}catch(IOException er){
throw new RuntimeException("文件保存失败!");
}
}
});
JMenuItem item1_4 = new JMenuItem("另存为(A)");
item1_4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File fileS = null;
if(fileS == null){
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath == null || fileName == null)
return;
fileS = new File(dirPath,fileName);
}
try{
BufferedWriter bufw = new BufferedWriter(new FileWriter(fileS));
String text = workArea.getText();
bufw.write(text);
bufw.close();
}catch(IOException er){
throw new RuntimeException("文件保存失败!");
}
}
});
JMenuItem item1_5 = new JMenuItem("退出(Q)");
item1_5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JMenuItem item2_1 = new JMenuItem("剪切(X)");
JMenuItem item2_2 = new JMenuItem("复制(C)");
JMenuItem item2_3 = new JMenuItem("粘贴(V)");
JRadioButtonMenuItem item3_1 = new JRadioButtonMenuItem("自动换行(W)",false);
item3_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == item3_1)
workArea.setLineWrap(true); //自动换行
else if(source != item3_1)
workArea.setLineWrap(false);
}
});
JMenuItem item4_1 = new JMenuItem("查看帮助(H)");
item4_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
new LookHelp();
}
});
JMenuItem item4_2 = new JMenuItem("关于记事本(A)");
item4_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
new AboutBook();
}
});
//在菜单中添加菜单项
file.add(item1_1);file.add(item1_2);file.add(item1_3);file.add(item1_4);file.add(item1_5);
edit.add(item2_1);edit.add(item2_2);edit.add(item2_3);
form.add(item3_1);
help.add(item4_1);help.add(item4_2);
}//构造方法结束
public static void main(String args[])
{
TextFrame app = new TextFrame();
app.setSize(600, 400);
app.setLocation(200,200);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
查看帮助:
package Text;
import javax.swing.*;
import java.awt.*;
public class LookHelp extends JFrame{
public LookHelp(){
super("查看帮助");
this.setSize(500, 300);
this.setLocation(200,300);
this.setResizable(false);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
Container c = this.getContentPane();
c.setLayout(new GridLayout(5,0));
JLabel label1 = new JLabel("1、菜单栏的菜单项可点击来选择功能。");
JLabel label2 = new JLabel("2、“编辑”里的剪切、复制、粘贴可用快捷键Ctrl + x、Ctrl + c、Ctrl + v实现。");
JLabel label3 = new JLabel("3、“格式”里的“自动换行”需要点中才可进行自动换行,否则会一直往后写。");
JLabel label4 = new JLabel("4、菜单栏下面的格式是用来调节文本的字体。");
JLabel label5 = new JLabel("5、本记事本的缺点是无法改变字体的样式,大小和形态。");
c.add(label1);c.add(label2);c.add(label3);c.add(label4);c.add(label5);
}
}
关于记事本:
package Text;
import javax.swing.*;
public class AboutBook extends JFrame{
public AboutBook(){
super("关于记事本");
this.setSize(220, 100);
this.setLocation(200,300);
this.setResizable(false);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
JPanel panel = new JPanel();
JLabel label = new JLabel("制作者:Kingsely");
panel.add(label);
this.add(panel);
}
}