标签:
1 package com.chuangkohua; 2 3 import java.awt.FileDialog; 4 import java.awt.FlowLayout; 5 import java.awt.Frame; 6 import java.awt.Menu; 7 import java.awt.MenuBar; 8 import java.awt.MenuItem; 9 import java.awt.TextArea; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 import java.awt.event.WindowAdapter; 13 import java.awt.event.WindowEvent; 14 import java.io.BufferedReader; 15 import java.io.File; 16 import java.io.FileNotFoundException; 17 import java.io.FileReader; 18 import java.io.IOException; 19 20 public class Menus { 21 22 23 24 public static void main(String[] args) { 25 // TODO Auto-generated method stub 26 Demo3 demo3 = new Demo3(); 27 demo3.init(); 28 29 30 } 31 32 } 33 34 class Demo3{ 35 private Frame frame; 36 private TextArea area; 37 private MenuBar bar; 38 private Menu menu,submenu,submenu2; 39 private MenuItem item,saveitem,item2,item3,item4,item41,item5,subitem; 40 private FileDialog dialog,dialog2; 41 42 public void init(){ 43 frame = new Frame("我的电脑"); 44 frame.setBounds(300, 100, 500, 600); 45 frame.setLayout(new FlowLayout()); 46 area = new TextArea(); 47 bar = new MenuBar(); 48 menu = new Menu("文件"); 49 submenu = new Menu("子菜单"); 50 subitem = new MenuItem("子条目"); 51 item = new MenuItem("关闭"); 52 item2 = new MenuItem("打开"); 53 saveitem = new MenuItem("保存"); 54 //利用关闭选项关闭对话框 55 item.addActionListener(new ActionListener() { 56 57 @Override 58 public void actionPerformed(ActionEvent e) { 59 // TODO Auto-generated method stub 60 System.exit(0); 61 } 62 }); 63 64 Menu menu1 = new Menu("编辑"); 65 item3 = new MenuItem("复制"); 66 submenu2 = new Menu("粘贴"); 67 item4 = new MenuItem("仅粘贴文字"); 68 item41 = new MenuItem("粘贴格式文字"); 69 70 71 Menu menu2 = new Menu("查看"); 72 item5 = new MenuItem("放大"); 73 74 dialog = new FileDialog(frame, "保存", FileDialog.SAVE); 75 saveitem.addActionListener(new ActionListener() { 76 77 @Override 78 public void actionPerformed(ActionEvent e) { 79 // TODO Auto-generated method stub 80 dialog.setVisible(true); 81 } 82 }); 83 84 dialog2 = new FileDialog(frame, "打开", FileDialog.LOAD); 85 item2.addActionListener(new ActionListener() { 86 87 @Override 88 public void actionPerformed(ActionEvent e) { 89 // TODO Auto-generated method stub 90 dialog2.setVisible(true); 91 String dirpath = dialog2.getDirectory(); 92 String filename = dialog2.getFile(); 93 if(dirpath==null||filename==null){ 94 return; 95 }else{ 96 area.setText("");; 97 File file = new File(dirpath, filename); 98 try { 99 BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); 100 String str =null; 101 while((str = bufferedReader.readLine())!=null){ 102 area.append(str+"\r\n"); 103 } 104 bufferedReader.close(); 105 } catch (FileNotFoundException e1) { 106 // TODO Auto-generated catch block 107 e1.printStackTrace(); 108 } catch (IOException e2){ 109 e2.printStackTrace(); 110 } 111 112 } 113 } 114 }); 115 //文件菜单 116 submenu.add(subitem); 117 menu.add(submenu); 118 menu.add(item2); 119 menu.add(item); 120 menu.add(saveitem); 121 //编辑菜单 122 submenu2.add(item4); 123 submenu2.add(item41); 124 menu1.add(item3); 125 menu1.add(submenu2); 126 //查看菜单 127 menu2.add(item5); 128 129 bar.add(menu); 130 bar.add(menu1); 131 bar.add(menu2); 132 133 frame.setMenuBar(bar); 134 frame.add(area); 135 frame.setVisible(true); 136 137 frame.addWindowListener(new WindowAdapter() { 138 public void windowClosing(WindowEvent e){ 139 System.exit(0); 140 } 141 }); 142 } 143 }
标签:
原文地址:http://www.cnblogs.com/fujilong/p/4734051.html