java编的记事本的一个问题

[code="java"]package com.gui;

import java.awt.CheckboxMenuItem;
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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class SampleMenu implements ActionListener, ItemListener {
private Frame f;
private String r,stitle;
private MenuBar mb;
private TextArea TA;
FileDialog FD,FS;

int newflag = 1;

private Menu m1;

private Menu m2;

private Menu m3;
private Menu m4;
private Menu m5;

private MenuItem mi1;

private MenuItem mi2;

private MenuItem mi3;

private MenuItem mi4;
private MenuItem mi5;
private MenuItem mi6;
private MenuItem mi7;
private CheckboxMenuItem mi8;

public void go() {
    f = new Frame("记事本");
    mb = new MenuBar();
    m1 = new Menu("文件(F)");
    m2 = new Menu("编辑(E)");
    m3 = new Menu("格式(O)");
    m4 = new Menu("查看(V)");
    m5 = new Menu("帮助(H)");
    mb.add(m1);
    mb.add(m2);
    mb.add(m3);
    mb.add(m4);
    mb.setHelpMenu(m5);
    f.setMenuBar(mb);

    mi1 = new MenuItem("新建(N)       CTRL+N");
    mi2 = new MenuItem("打开(O)       CTRL+O");
    mi3 = new MenuItem("保存(S)       CTRL+S");
    mi4 = new MenuItem("另存为(A)..");
    mi5 = new MenuItem("页面设置(U)..   ");
    mi6 = new MenuItem("打印(P)       CTRL+P");
    mi7 = new MenuItem("退出(X)");
    mi1.addActionListener(this);
    mi2.addActionListener(this);
    mi3.addActionListener(this);
    mi4.addActionListener(this);
    mi5.addActionListener(this);
    mi6.addActionListener(this);
    mi7.addActionListener(this);
    m1.add(mi1);
    m1.add(mi2);
    m1.add(mi3);
    m1.add(mi4);
    m1.addSeparator();
    m1.add(mi5);
    m1.add(mi6);
    m1.addSeparator();
    m1.add(mi7);

    mi8 = new CheckboxMenuItem("Persistent");
    mi8.addItemListener(this);
    m1.add(mi8);
    TA = new TextArea();
    f.add(TA);
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);

        }

    });
    f.setSize(500, 500);
    f.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {
     if(ae.getSource()==mi1)
    { newflag=1;
      TA.setText(" ");
    }


     if(ae.getSource()==mi3)
   {
    if(newflag==1){
     FS=new FileDialog(f,"保存文件",FileDialog.SAVE);
     FS.setDirectory("c:\\");
     FS.setFile("*.txt");
     FS.setVisible(true);
     stitle=FS.getDirectory()+FS.getFile();
     newflag=0;
    }
    try{
     File myfile=new File(stitle);
     BufferedWriter out=new BufferedWriter(new FileWriter(myfile));
     out.write(TA.getText());
     out.close();
    }catch(IOException ioe){
     System.out.println(ioe.toString());
     }
   }

    /*if(ae.getSource()==mi2);
     {
     FD=new FileDialog(f,"打开文件",FileDialog.LOAD);
     FD.setDirectory("c:\\");
     FD.setVisible(true);
     try
     {
     TA.setText(" ");
     File mf=new File(FD.getDirectory(),FD.getFile());
     RandomAccessFile raf=new RandomAccessFile(mf,"rw");
     //String s1=null;
     while((r=raf.readLine())!=null){
        // s1 = new String(r.getBytes("GBK"),"ISO8895_1");
     TA.append(r+"\n");
     }
     }catch(IOException ioe)
     {
     System.err.println(ioe.toString());
     }

     }*/




    if (ae.getActionCommand().equals("退出(X)")) {
        System.exit(0);
    }
}


public void itemStateChanged(ItemEvent ie) {
    String state = "deselected";

    if (ie.getStateChange() == ItemEvent.SELECTED) {
        state = "selected";
    }
    System.out.println(ie.getItem() + " " + state);
}

public static void main(String args[]) {
    SampleMenu sampleMenu = new SampleMenu();
    sampleMenu.go();
}

}
[/code]
[size=medium]暂时实现了文件功能的菜单,在子选项中实现了新建,打开,保存功能,但是为什么点击以后都是实现的打开功能,除非把打开那段的代码注释掉才行,大家指点下~谢谢[/size]

1个回答

[code="java"]

if(ae.getSource()==mi2);
{
}[/code]
这段代码是你注释掉的代码,它的含义是:
[code="java"]
if(ae.getSource()==mi2)
; // 没做事情

// 下面是代码段
{
// 打开文件
}
[/code]

详细点说, 如下:
[code="java"]if(ae.getSource()==mi2); [/code]
等价于
[code="java"]
if(ae.getSource()==mi2)
; // 什么都没做
[/code]

下面的写法表示一个代码段, 这个代码段, 在每次都会执行
[code="java"]
{
}
[/code]

修改: 去掉;
提示: 写代码请使用IDE 格式化一下, 能发现很多问题的.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!