- import java.io.*;
- import java.io.File;
- import java.awt.*;
- import java.awt.event.*;
- class MaMenu {
- public static void main(String args[]){
- final TextArea ta=new TextArea();
- final Frame mf=new Frame("菜单");
- mf.setSize(600,400);
- mf.setLocation(200,200);
- mf.add(ta);
- mf.addWindowListener( new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- MenuBar br=new MenuBar();
- Menu m1=new Menu("文件");
- Menu m2=new Menu("编辑");
- MenuItem mi1=new MenuItem("新建");
- MenuItem mi2=new MenuItem("打开");
- mi2.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e){
- FileDialog fd=new FileDialog(mf,"打开文件",FileDialog.LOAD);
- String str=fd.getDirectory()+fd.getFile();
- fd.show();
- try{
- if(str!=null)
- { System.out.println(str);
- FileInputStream fi=new FileInputStream(str);
- byte by[]=new byte[10*1024];
- int len=fi.read(by);
- ta.append(new String(by,0,len));
- }
- }
- catch(FileNotFoundException ex){
- ex.getStackTrace();
- }
- catch(IOException ex){
- ex.getStackTrace();
- }
- }
- });
- MenuItem mi3=new MenuItem("保存");
- MenuItem mi4=new MenuItem("退出");
- mi4.addActionListener( new ActionListener(){
- public void actionPerformed(ActionEvent e){
- System.exit(0);
- }
- });
- MenuItem mi5=new MenuItem("删除");
- MenuItem mi6=new MenuItem("粘贴");
- m1.add(mi1);
- m1.add(mi2);
- m1.add(mi3);
- m1.add(mi4);
- m2.add(mi5);
- m2.add(mi6);
- br.add(m1);
- br.add(m2);
- mf.setMenuBar(br);
- mf.show();
- }
- }
复制代码 为什么选择打开选项不能读取文件,我在那里面加了一条打印语句,得到的文件路径和文件名却是空的? |