import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MainMenu extends JFrame {
JMenuBar jmb=null;
JMenu file=null;
JMenuItem newfile=null;
JMenuItem open=null;
JMenuItem save=null;
JMenuItem saveAs=null;
JMenu edit=null;
JMenuItem cut=null;
JMenuItem copy=null;
JMenuItem paste=null;
JMenu format=null;
JMenu view=null;
JMenu help=null;
JTextArea jta=null;
MainMenu(){
this.setBounds(200, 200, 500, 500);
this.setTitle("记事本");
initComponent();
addComponent();
jta.setEditable(false);
MenuListener ml=new MenuListener(this);
//给事件项加入事件命令符
newfile.setActionCommand("newfile");
open.setActionCommand("open");
save.setActionCommand("save");
saveAs.setActionCommand("saveAs");
copy.setActionCommand("copy");
cut.setActionCommand("cut");
paste.setActionCommand("paste");
//给菜单加上监听
newfile.addActionListener(ml);
open.addActionListener(ml);
save.addActionListener(ml);
saveAs.addActionListener(ml);
copy.addActionListener(ml);
cut.addActionListener(ml);
paste.addActionListener(ml);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void addComponent() {
this.setJMenuBar(jmb);
jmb.add(file);
file.add(newfile);
file.add(open);
file.add(save);
file.add(saveAs);
//添加快捷键
file.setMnemonic(KeyEvent.VK_F);
jmb.add(edit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.setMnemonic(KeyEvent.VK_E);
jmb.add(format);
jmb.add(view);
jmb.add(help);
//添加文本域和滚动面板
this.add(new JScrollPane(jta));
}
private void initComponent() {
jmb=new JMenuBar();
file=new JMenu("File");
newfile=new JMenuItem("新建");
open=new JMenuItem("打开");
save=new JMenuItem("保存");
saveAs=new JMenuItem("另存为");
edit=new JMenu("Edit");
cut=new JMenuItem("剪切");
copy=new JMenuItem("复制");
paste=new JMenuItem("粘贴");
format=new JMenu("Format");
view=new JMenu("View");
help=new JMenu("Help");
jta=new JTextArea();
}
}
---------------------------------------------------------------------------------------------
//监听函数
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
public class MenuListener implements ActionListener {
MainMenu mm=null;
MenuListener (MainMenu mm){
this.mm=mm;
}
JFileChooser jfc=new JFileChooser();
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
if(command.equals("newfile")){
//新建文档
mm.jta.setEditable(true);
}
if(command.equals("open")){
openmethod();
}
if (command.equals("save")){
//保存
savemethod();
}
if (command.equals("saveAs")){
saveAs();
}
if(command.equals("copy")){
mm.jta.copy();
}
if(command.equals("paste")){
mm.jta.paste();
}
if(command.equals("cut")){
mm.jta.cut();
}
}
private void saveAs() {
int n=jfc.showSaveDialog(null);
if(n==JFileChooser.APPROVE_OPTION){
//获取文件对象
File fileName=jfc.getSelectedFile();
//定义一个文件输出流
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(fileName));
String data=mm.jta.getText();
data=data.replaceAll( "\n","\r\n");
bw.write(data);
if(bw!=null){
bw.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
private void savemethod() {
int n=jfc.showSaveDialog(null);
if(n==JFileChooser.APPROVE_OPTION){
//获取文件对象
File fileName=jfc.getSelectedFile();
//定义一个文件输出流
BufferedWriter bw=null;
try {
bw=new BufferedWriter(new FileWriter(fileName));
String data=mm.jta.getText();
data=data.replaceAll( "\n","\r\n");
bw.write(data);
if(bw!=null){
bw.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
private void openmethod() {
//弹出一个文件选择框
mm.jta.setEditable(true);
int t=jfc.showOpenDialog(null);
if (t==JFileChooser.APPROVE_OPTION){
File fileName=jfc.getSelectedFile();
//定义一个字符输出流
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String data=null;
//mm.jta.append(data);
try {
while((data=br.readLine())!=null){
mm.jta.append(data+"\r\n");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
if(br!=null)
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
----------------------------------------------------------------------------------------
//测试函数
public class TestMainMenu {
public static void main(String[] args) {
new MainMenu();
}
}
------------------------------------------------------------------------------------------
//界面
---------------------------------------------------------------------------------------------------