// ji2应该是一个菜单变量,这里给它添加了一个Action监听器,监听器使用了匿名类。
ji2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// 构造了一个文件对话框对象
JFileChooser fc=new JFileChooser();
// 显示文件打开对话框,返回值为整型,可判断是否点击了打开按钮
int select=fc.showOpenDialog(frame);
// 如果点击了打开按钮
if (select==JFileChooser.APPROVE_OPTION)
{
// 从文件对话框对象中获取选中的文件对象
File file=fc.getSelectedFile();
try{
// 以文件对象构造文件输入流
FileInputStream fin =new FileInputStream(file);
// 以文件输入流构造字符输入流
InputStreamReader in =new InputStreamReader(fin);
// 以字符输入流构造缓冲字符输入流
BufferedReader reader =new BufferedReader(in);
// 从缓冲字符输入流中读取一行字符串
String s=reader.readLine();
// ta变量应该是JTextArea的实例化对象,将上一行得到的字符串写入多行文本框
ta.setText(s);
// 通过循环持续读取文件中所有行的字符串,(s = reader.readLine())是读取一行字符串并将字符串赋给s变量,后面的!=null表示如果已经到了文件的末尾则跳出循环
while((s=reader.readLine())!=null){
// 给多行文本框追加文本,前面的\n是换行的意思
ta.append("\n"+s);
}
// 关闭输入流
in.close();
// 下面的几个catch是异常捕获,为了防止程序出现意外
}catch(FileNotFoundException fe){}
catch(EOFException fe){}
catch(IOException fe){}
}
}
});
//注册“保存”的监听程序
// ji3应该是菜单对象的变量,给这个对象添加Action事件
ji3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// 构造了一个文件对话框对象
JFileChooser fc=new JFileChooser();
// 显示文件保存对话框,返回值为整型,可判断是否点击了保存按钮
int select=fc.showSaveDialog(frame);
// 如果点击了保存按钮
if (select==JFileChooser.APPROVE_OPTION)
{
// 从文件对话框对象中获取将保存的文件对象
File file=fc.getSelectedFile();
// 构造一个单选按钮菜单
JRadioButtonMenuItem radioMenuItemrow=new JRadioButtonMenuItem();
// 将单选按钮菜单设置为没有选中
radioMenuItemrow.setSelected(false);
try{
// 以文件对象构造文件输出流
FileOutputStream fout =new FileOutputStream(file);
// 以文件输出流构造字符输出流
OutputStreamWriter out =new OutputStreamWriter(fout);
// 如果单选菜单为选中状态
if(radioMenuItemrow.isSelected()){
// 将其改为没有选中
radioMenuItemrow.setSelected(false);
// 将多行文本框的内容写入字符输出流
ta.write(out);
// 将单选菜单的状态改为选中
radioMenuItemrow.setSelected(true);
}
else{ // 否则菜单是未选中状态
// 将单选菜单的状态改为选中
ta.write(out);
}
// 关闭字符输出流
out.close();
// 下面的几个catch是异常捕获,为了防止程序出现意外
}catch(FileNotFoundException fe){}
catch(EOFException fe){}
catch(IOException fe){}
}
}
});
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 Mytext extends JFrame {
// 用来放JTextArea的面板
private JScrollPane myJScrollPane;
// 声明JFrame中的JTextArea和JMenuBar
private JTextArea myTextArea;
private JMenuBar myMenuBar;
// 声明JMenuBar中的JMenu
private JMenu myMenuFile;
private JMenu myMenuEdit;
private JMenu myMenuForm;
private JMenu myMenuCheck;
private JMenu myMenuHelp;
// 声明myMenuFile的JMenuItem
private JMenuItem myMenuItemNew;
private JMenuItem myMenuItemOpen;
private JMenuItem myMenuItemSave;
private JMenuItem myMenuItemSaveAs;
private JMenuItem myMenuItemPageSetup;
private JMenuItem myMenuItemPrint;
private JMenuItem myMenuItemExit;
//**************************************
// 需要自己在这里定义其他JMenu的JMenuItem
//**************************************
public Mytext() {
// 为myTextArea和myMenuBar分配内存
this.myTextArea = new JTextArea();
this.myMenuBar = new JMenuBar();
//把JTextArea放到JScrollPane中去
this.myJScrollPane = new JScrollPane(this.myTextArea);
// 为JMenu分配内存并命名
this.myMenuFile = new JMenu("文件");
this.myMenuEdit = new JMenu("编辑");
this.myMenuForm = new JMenu("格式");
this.myMenuCheck = new JMenu("查看");
this.myMenuHelp = new JMenu("帮助");
// 为JMenuItem分配内存并命名
this.myMenuItemNew = new JMenuItem("新建");
this.myMenuItemOpen = new JMenuItem("打开");
this.myMenuItemSave = new JMenuItem("保存");
this.myMenuItemSaveAs = new JMenuItem("另存为...");
this.myMenuItemPageSetup = new JMenuItem("页面设置...");
this.myMenuItemPrint = new JMenuItem("打印");
this.myMenuItemExit = new JMenuItem("退出");
// 把JMenuItem添加到JMenu中去
this.myMenuFile.add(this.myMenuItemNew);
this.myMenuFile.add(this.myMenuItemOpen);
this.myMenuFile.add(this.myMenuItemSave);
this.myMenuFile.add(this.myMenuItemSaveAs);
this.myMenuFile.add(this.myMenuItemPageSetup);
this.myMenuFile.add(this.myMenuItemPrint);
this.myMenuFile.add(this.myMenuItemExit);
//****************************************************
// 在这里把自己定义的其他的JMenuItem也添加到相应的JMenu中去
//****************************************************
// 把JMenu添加到JMenuBar中去
this.myMenuBar.add(this.myMenuFile);
this.myMenuBar.add(this.myMenuEdit);
this.myMenuBar.add(this.myMenuForm);
this.myMenuBar.add(this.myMenuCheck);
this.myMenuBar.add(this.myMenuHelp);
// 把JMenuBar和JScrollPane添加到JFrame中去
this.setJMenuBar(this.myMenuBar);
this.add(this.myJScrollPane);
//
// 在这里需要给所有JMenuItem对象加上监听
//
// 设置JFrame属性
this.setTitle("记事本");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(200, 200, 800, 500);
}
//******************************************************************************
// 这里需要自己写监听来监听JMenuItem的事件,与给JButton添加监听一样,也 //可以直接在构造函数中给JMenuItem添加内部类监听
//******************************************************************************
public static void main(String[] args) {
new Mytext().setVisible(true);
}
}