写记事本,监听菜单。CMD显示找不到文本实例

fengzhiquxiang1 2008-03-22 02:09:56
//我的代码
//制作记事本

import java.awt.*;
import java.awt.event.*;

//frame
public class Notepad extends Frame{

TextArea ta;
MyMenuBar mmb;

public Notepad(){ //构造器

//mmb=new MyMenuBar(new String[]{"file","edit","format","color","help"});
mmb=new MyMenuBar();
this.setMenuBar(mmb);

ta=new TextArea();
this.add(ta);

this.addWindowListener(new NListener());
}

public static void main(String[] args){
Notepad n=new Notepad();
n.setTitle("Notdpad");
n.setBounds(200,200,700,500);
n.setVisible(true);
}
}
//MyMenuBar
class MyMenuBar extends MenuBar{

private Menu file=new Menu("file");
private Menu edit=new Menu("edit");
private Menu format=new Menu("format");
private Menu color=new Menu("color");
private Menu help=new Menu("help");

private MenuItem file_new=new MenuItem("New");
private MenuItem file_open=new MenuItem("Open");
private MenuItem file_save=new MenuItem("Save");
private MenuItem file_exit=new MenuItem("Exit");

private MenuItem edit_copy=new MenuItem("Copy");
private MenuItem edit_plaster=new MenuItem("Plaster");

private MenuItem format_font=new MenuItem("Font");

private MenuItem color_yellow=new MenuItem("Yellow");

private NListener nl=new NListener();

public MyMenuBar(){
file.add(file_new);
file.add(file_open);
file.add(file_save);
file.add(file_exit);

edit.add(edit_copy);
edit.add(edit_plaster);

format.add(format_font);

color.add(color_yellow);

this.add(file);
this.add(edit);
this.add(format);
this.add(color);
this.add(help);

file_new.addActionListener(nl);
file_open.addActionListener(nl);
file_save.addActionListener(nl);
file_exit.addActionListener(nl);

edit_copy.addActionListener(nl);
edit_plaster.addActionListener(nl);

format_font.addActionListener(nl);

color_yellow.addActionListener(nl);

}


/*public MyMenuBar(String[] s){
for(int i=0;i<s.length;i++){
this.add(new Menu(s[i]));

}
}*/

}

//listener
class NListener implements WindowListener,ActionListener{

public void windowActivated(WindowEvent e){}//WindowListener
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}

public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="file_new"){
ta.setText(null);//---------------------这里找不到ta
}
}

/*public void itemStateChanged(ItemEvent e){

}*/


}
大哥大姐给个提示就好

...全文
90 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengzhiquxiang1 2008-03-22
  • 打赏
  • 举报
回复
再顶!!!!!!!!!
fengzhiquxiang1 2008-03-22
  • 打赏
  • 举报
回复
自己顶了,别沉了
fengzhiquxiang1 2008-03-22
  • 打赏
  • 举报
回复
那不用内部类怎么解决呢
fengzhiquxiang1 2008-03-22
  • 打赏
  • 举报
回复
以前是看过内部类,可是你不说我都忘记了
fengzhiquxiang1 2008-03-22
  • 打赏
  • 举报
回复
是啊,谢谢你
zapdos 2008-03-22
  • 打赏
  • 举报
回复
大佬
你的ta在另外一个类里面,要传过去才行
redsand_sgh 2008-03-22
  • 打赏
  • 举报
回复
写成内部类不是很好解决吗!
fengzhiquxiang1 2008-03-22
  • 打赏
  • 举报
回复
怎么传啊

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧