Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 【求助】菜单响应问题让我很迷惑
jialichao





发贴: 12
积分: 0
于 2006-05-24 14:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package mypackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuTest extends JFrame {

  Frame fr;
  public MenuTest(){
    
    fr=this;
    
    MenuBar mb=new MenuBar();
    setMenuBar(mb);
    
    Menu file=new Menu("文件");
    MenuItem itmm=new MenuItem("退出");
    file.add(new MenuItem("新建"));
    file.add(new MenuItem("打开"));
    file.add(new MenuItem("关闭"));
    file.add(new MenuItem("-"));
    file.add(itmm);
    
    MyListener s=new MyListener();
    itmm.addActionListenerMoon;
    
    class MyListener implements ActionListener{

      public void actionPerformed(ActionEvent e) {
        if(e.getSource()=="退出"){
         System.exit(0);
        }
      }
      
    }
    
    /*itmm.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        System.exit(0);
      }
    });*/

    mb.add(file);
    
    this.setSize(300,200);
    this.setVisible(true);
    
    
  }
  public static void main(String[] args) {
    new MenuTest();
    
  }

}

我讲匿名内部类写成自定义的监视类以后就不行了,不知道哪里出错了,望各位大哥指教呀,谢谢了。



作者 Re:【求助】菜单响应问题让我很迷惑 [Re:jialichao]
lisliefor





发贴: 287
积分: 7
于 2006-05-24 18:42 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
首先内隐类的位置,应该放在为它生成对象之前(即放在MyListener s = new MyListener)之前。
使用内隐类的典型做法是,外围的class有一个函数,可以传回一个reference指向这个内部类。你正好用到了这点,一个不错的尝试。 ^_^
有两个问题,不知道你考虑过没有,首先e.getSource()这个方法返回的类型是一个Object,而非字符串,“e.getSource()=="新建"”,好像不能比较吧!而且我也尝试过来。一般用法是e.getSource()==t (t为一个发生鼠标时间的组件)。
第二个问题,内隐类中,如果要涉及到外部类的对象时,如果将当前对象传递进来呢?(以前我为这个问题而苦恼。)那么,这样的做法,跟你使用类的组合有什么区别?
呵呵,你热切希望实践新学的东西的心情,我能理解,跟你一样我也是一个热爱Java的初学者。 :)
如果是我,我会这样写关于菜单的相应问题。

package help;

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

import javax.swing.*;

public class MenuTest extends JFrame{
  
  Frame fr;
  MenuItem newf;
  MenuItem itmm;
  
  ActionListener listen = new ActionListener(){
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==itmm){
        System.exit(0);
      }
    }
  };
  
  public MenuTest(){
   fr=this;
   MenuBar mb=new MenuBar();
   setMenuBar(mb);
  
   Menu file=new Menu("文件");
   itmm=new MenuItem("退出");
   file.add(new MenuItem("新建"));
   file.add(new MenuItem("打开"));
   file.add(new MenuItem("关闭"));
   file.add(new MenuItem("-"));
   file.add(itmm);
   itmm.addActionListener(listen);
  
   mb.add(file);
   this.setSize(300,200);
   this.setVisible(true);
   }  
  public static void main(String[] args) {
      new MenuTest();
  }
}

/*
*     一般简单的程序建议使用awt的组件,效率更高一点。光加载“import javax.swing.*;”
*     这些东西就够受的了。
*     热切希望和所有热爱Java的朋友一起学习,一起进步! ^_^
*
*/



作者 Re:【求助】菜单响应问题让我很迷惑 [Re:jialichao]
lisliefor





发贴: 287
积分: 7
于 2006-05-24 20:41 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
首先自我反省一下! Smile
上面的回答有两个问题:
1. 首先,new MyListener()的位置,与该class的位置无关。我开始复制你的代码,调试的时候,“MyListener s = new MyListener();”报错的原因,可能在于我使用的工具是Eclipes,它一个缺陷是,如果机器性能不够,那么程序保存后,刷新率不够快,原本正确的代码,会报一些莫名的错误,而从新敲一遍(或粘贴)就没有问题。
2. 内隐类与两个类之间组合的优势就在于,使用外部类对象的时候,避免了类传值的问题。(Smile 上面刚好说反了!)

后来我从写了一遍,运行而且没有错误。
package help;

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

import javax.swing.*;

public class MenuTest extends JFrame{
  
  Frame fr;
  MenuItem newf;
  MenuItem itmm;
  
  
  
  public MenuTest(){
   fr=this;
   MenuBar mb=new MenuBar();
   setMenuBar(mb);
  
   Menu file=new Menu("文件");
   itmm=new MenuItem("退出");
   file.add(new MenuItem("新建"));
   file.add(new MenuItem("打开"));
   file.add(new MenuItem("关闭"));
   file.add(new MenuItem("-"));
   file.add(itmm);
  
   itmm.addActionListener(new MyListener());
  
   mb.add(file);
   this.setSize(300,200);
   this.setVisible(true);
   }  
  public static void main(String[] args) {
      new MenuTest();
  }
  
  class MyListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==itmm){
        System.exit(0);
      }
    }
  }
}

你上面代码无法实现的原因可能就在于:e.getSource()使用上,还有就是,加载在Frame上的组件,最好在构造函数外面声明,在里面初始化!(后面函数可能用到)。
误导你了,不好意思啊!呵呵,我水平也很菜啊! 一起学习..... :)



作者 Re:【求助】菜单响应问题让我很迷惑 [Re:jialichao]
jialichao





发贴: 12
积分: 0
于 2006-05-27 11:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵~~ 多谢指教,你比我强多了,我才刚开始学java,觉得很不错想学学,以后有什么问题还希望你多多指教哦!谢谢你。


作者 Re:【求助】菜单响应问题让我很迷惑 [Re:jialichao]
lhawk





发贴: 30
积分: 0
于 2006-05-27 15:58 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
值得学习,顶!


作者 Re:【求助】菜单响应问题让我很迷惑 [Re:jialichao]
pwc_beyond





发贴: 2
积分: 0
于 2006-05-30 22:11 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我也是新学员~共同进步



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923