回复:(xuerong1220)说点自己的看法第一你的类名Cai...
package caidan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Caidan implements ActionListener{
static JFrame kuang=new JFrame("记事本");
static JTextArea wenben=new JTextArea();
static JMenuBar caidan=new JMenuBar();
static JMenu wenjian=new JMenu("文件");
static JMenu bianji=new JMenu("编辑");
static JMenu geshi=new JMenu("格式");
static JMenu chakan=new JMenu("查看");
String wen[]={"新建","打开","保存","另存为","退出"};
String bian[]={"撤消","剪切","复制","粘贴","删除"};
JMenuItem a[]=new JMenuItem[wen.length];
JMenuItem a1[]=new JMenuItem[bian.length];
public Caidan(){
for(int i=0;i<5;i++)
{
a[i]=new JMenuItem(wen[i]);
a1[i]=new JMenuItem(bian[i]);
wenjian.add(a[i]);
bianji.add(a1[i]);
a[i].addActionListener(this);
a1[i].addActionListener(this);
}
caidan.add(wenjian);
caidan.add(bianji);
caidan.add(geshi);
caidan.add(chakan);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("新建"))
{
wenben.setText("");
}else if(e.getActionCommand().equals("打开"))
{
FileDialog fd=new FileDialog(kuang,"打开",FileDialog.LOAD);
fd.show();
String strFile=fd.getDirectory()+fd.getFile();
if(strFile!=null)
{
try {
FileReader fr=new FileReader(strFile);
BufferedReader fis=new BufferedReader(fr);
String line=fis.readLine();
while(line!=null)
{
wenben.append(line);
wenben.setLineWrap(true);
line=fis.readLine();
}
fis.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}else if(e.getActionCommand().equals("保存"))
{
FileDialog ff=new FileDialog(kuang,"打开",FileDialog.SAVE);
ff.show();
try{
FileWriter Fr=new FileWriter(ff.getDirectory()+ff.getFile());
BufferedWriter FF=new BufferedWriter(Fr);
FF.write(wenben.getText());
FF.close();
}catch(Exception ex){
ex.printStackTrace();
}
}else if(e.getActionCommand().equals("另存为"))
{
FileDialog ff=new FileDialog(kuang,"打开",FileDialog.SAVE);
ff.show();
try{
FileWriter Fr=new FileWriter(ff.getDirectory()+ff.getFile());
BufferedWriter FF=new BufferedWriter(Fr);
FF.write(wenben.getText());
FF.close();
}catch(Exception ex){
ex.printStackTrace();
}
}else if(e.getActionCommand().equals("退出"))
{
System.exit(0);
}else if(e.getActionCommand().equals("撤消"))
{}else if(e.getActionCommand().equals("剪切"))
{
wenben.cut();
}else if(e.getActionCommand().equals("复制"))
{
wenben.copy();
}else if(e.getActionCommand().equals("粘贴"))
{
wenben.paste();
}else if(e.getActionCommand().equals("删除"))
{}
}
public static void main(String[]args)
{ Caidan n=new Caidan();
kuang.setSize(600,600);
kuang.setLocation(300,300);
kuang.setBackground(Color.white);
kuang.add(wenben);
kuang.setJMenuBar(caidan);
kuang.setVisible(true);
}
}
是这样吗??