代码:
1 import java.awt.*; 2 import java.awt.event.*; 3 import java.io.*; 4 class MyMenu 5 { 6 //定义该窗体所需的组件,方便其他函数引用 7 private Frame f; 8 private Button btn; //按钮 9 private Button btn1; 10 private Label label; //标签 11 private TextArea ta ; //文本区域 12 private Menu fileMenu; //菜单 13 private MenuBar bar; 14 private MenuItem openItem,saveItem,closeItem; //子菜单 15 16 private FileDialog openDia,saveDia; //文件对话框 17 private File file; //文件 18 //构造器 19 MyMenu(){ 20 init(); 21 } 22 public void init(){ 23 f = new Frame("我的窗体及菜单框架"); 24 /*(所有静态属性设置)*/ 25 //对窗体进行基本设置 26 f.setBounds(100,50,1200,1000); 27 f.setLayout(new FlowLayout()); 28 f.setBackground(Color.gray); 29 //组件(自定义) 30 btn = new Button("退出"); 31 btn1 = new Button("按钮1"); 32 label = new Label("这是一个文本",Label.CENTER); 33 ta = new TextArea("test",30,70,1);//最后一个参数设置为显示滚动条,1显示垂直滚动条,2显示横向滚动条,0全部显示。 34 35 bar = new MenuBar(); 36 fileMenu = new Menu("文件"); 37 //定义菜单项 38 openItem = new MenuItem("打开"); 39 saveItem = new MenuItem("保存"); 40 closeItem = new MenuItem("退出"); 41 //将菜单项添加进菜单 42 fileMenu.add(openItem); 43 fileMenu.add(saveItem); 44 fileMenu.addSeparator(); 45 fileMenu.add(closeItem); 46 //将菜单已添加进菜单栏 47 bar.add(fileMenu); 48 f.setMenuBar(bar); 49 //添加文件对话框 50 openDia = new FileDialog(f,"我要打开",FileDialog.LOAD); 51 saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE); 52 //组件属性设置 53 label.setBackground(Color.pink); 54 label.setFont(new Font("Dialog", Font.BOLD, 12)); 55 label.setSize(300,200); 56 //将组件添加到frame中 57 f.add(label); 58 f.add(ta); 59 f.add(btn); 60 f.add(btn1); 61 //加载窗体事件(所有响应事件) 62 myEvent(); 63 64 //显示窗体 65 f.setVisible(true); 66 } 67 68 private void myEvent(){ 69 /*规则: 70 xxxListener(new xxxAdapter()) 71 xxxEvent e 72 */ 73 //窗体关闭事件 74 f.addWindowListener(new WindowAdapter() 75 { 76 public void windowClosing(WindowEvent e) 77 { 78 System.exit(0); 79 } 80 }); 81 //按钮响应事件 82 btn.addActionListener(new ActionListener(){ 83 public void actionPerformed(ActionEvent e) 84 { 85 //按钮响应 86 System.out.println("退出,按钮干的"); 87 System.exit(0); 88 } 89 }); 90 //按钮1响应事件 91 btn1.addActionListener(new ActionListener(){ 92 public void actionPerformed(ActionEvent e) 93 { 94 //获取文本内容并显示 95 String taContent =ta.getText(); 96 label.setText(taContent); 97 } 98 }); 99 //菜单事件:添加保存活动监听, 100 saveItem.addActionListener(new ActionListener() 101 { 102 103 public void actionPerformed(ActionEvent e) 104 { 105 //保存就是直接开启对话框 106 //if(file==null) 107 //{ 108 saveDia.setVisible(true); 109 110 String dirPath = saveDia.getDirectory(); 111 String fileName = saveDia.getFile(); 112 if(dirPath==null || fileName==null) 113 return ; 114 file = new File(dirPath,fileName); 115 //} 116 117 try 118 { 119 BufferedWriter bufw = new BufferedWriter(new FileWriter(file)); 120 121 String text = ta.getText(); 122 123 bufw.write(text); 124 //bufw.flush(); 125 bufw.close(); 126 } 127 catch (IOException ex) 128 { 129 throw new RuntimeException(); 130 } 131 132 } 133 }); 134 135 136 openItem.addActionListener(new ActionListener() 137 { 138 public void actionPerformed(ActionEvent e) 139 { 140 openDia.setVisible(true); 141 String dirPath = openDia.getDirectory(); 142 String fileName = openDia.getFile(); 143 // System.out.println(dirPath+"..."+fileName); 144 if(dirPath==null || fileName==null) 145 return ; 146 147 ta.setText(""); 148 file = new File(dirPath,fileName); 149 150 try 151 { 152 BufferedReader bufr = new BufferedReader(new FileReader(file)); 153 154 String line = null; 155 156 while((line=bufr.readLine())!=null) 157 { 158 ta.append(line+"\r\n"); 159 } 160 161 bufr.close(); 162 } 163 catch (IOException ex) 164 { 165 throw new RuntimeException("读取失败"); 166 } 167 168 169 } 170 }); 171 172 173 174 175 176 closeItem.addActionListener(new ActionListener() 177 { 178 public void actionPerformed(ActionEvent e) 179 { 180 System.exit(0); 181 } 182 }); 183 } 184 185 public static void main(String[] args){ 186 new MyMenu(); 187 } 188 }
效果:
时间: 2024-10-10 06:33:29