本帖最后由 王金科 于 2012-8-24 17:28 编辑
- package cn.study.gui;
- import java.awt.Dimension;
- import java.awt.FileDialog;
- import java.awt.Frame;
- import java.awt.Menu;
- import java.awt.MenuBar;
- import java.awt.MenuItem;
- import java.awt.TextArea;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- 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;
- public class MyMenuDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new MyMenu();
- }
- }
- class MyMenu{
- private Frame f;
- private MenuBar menuBar;
- private TextArea ta;
- private Menu fileMenu,editMenu;
- private MenuItem xinjianItem,closeItem,openItem,saveItem;
- private FileDialog openDialog,saveDialog;
- private File file;
- private String dirPath,fileName;
-
- MyMenu(){
- init();
- }
-
- public void init(){
- f = new Frame("my window");
- Dimension dem=Toolkit.getDefaultToolkit().getScreenSize();
- int screenWidth=(int)dem.getWidth(),screenHeight=(int)dem.getHeight();
- int thisWidth=650,thisHeight=600;
- int thisX=(screenWidth-thisWidth)/2,thisY=(screenHeight-thisHeight)/2;
- f.setBounds(thisX,thisY,thisWidth,thisHeight);
-
- ta = new TextArea();
- menuBar = new MenuBar();
- fileMenu = new Menu("文件");
- xinjianItem = new MenuItem("新建");
- closeItem = new MenuItem("退出");
- openItem = new MenuItem("打开");
- saveItem = new MenuItem("保存");
- editMenu = new Menu("编辑");
- openDialog = new FileDialog(f,"打开",FileDialog.LOAD);
- saveDialog = new FileDialog(f,"另存为",FileDialog.SAVE);
-
- fileMenu.add(xinjianItem);
- fileMenu.add(openItem);
- fileMenu.add(saveItem);
- fileMenu.add(closeItem);
- menuBar.add(fileMenu);
- menuBar.add(editMenu);
- f.add(ta);
- f.setMenuBar(menuBar);
- f.setVisible(true);
- myEvent();
- }
-
- private void myEvent(){
- xinjianItem.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- ta.setText("");
- file = null;
- }
- });
-
- saveItem.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
-
- if (file==null) {
- saveDialog.setVisible(true);
- dirPath = saveDialog.getDirectory();
- fileName = saveDialog.getFile();
- if (dirPath == null || fileName == null) {
- <font color="red">return;</font>
- }
- file = new File(dirPath, fileName);
- }
- try {
- BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
- String text = ta.getText();
- bufw.write(text);
- bufw.close();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
-
- openItem.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- openDialog.setVisible(true);
- dirPath = openDialog.getDirectory();
- fileName = openDialog.getFile();
- if(dirPath==null || fileName==null){
- <font color="red">return;</font>
- }
- ta.setText("");
- file = new File(dirPath,fileName);
- try {
- BufferedReader bufr = new BufferedReader(new FileReader(file));
- String line = null;
- while((line=bufr.readLine())!=null){
- ta.append(line+"\r\n");
- }
- bufr.close();
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
-
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
-
- closeItem.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- System.exit(0);
- }
- });
- }
- }
复制代码 在以上一个简单的记事本代码中,这里面的return返回了一个什么?
起到什么作用?
为什么写成return null;编译就不通过?
还有,我这里面"新建"条目的功能这样写对不对?视频中没有,我自己想的- xinjianItem.addActionListener(new ActionListener() {
- 84.
- 85. @Override
- 86. public void actionPerformed(ActionEvent e) {
- 87. // TODO Auto-generated method stub
- 88. ta.setText("");
- 89. file = null;
- 90. }
- 91. });
复制代码 |