爱问知识人 爱问教育 医院库

关于addActionListener( this)能不能把this去掉?

首页

关于addActionListener( this)能不能把this去掉?

关于addActionListener( this)能不能把this去掉?关于addActionListener( this)能不能把this去掉?是这样的,下面是我的一道习题,做完了我觉得mian方法只有一句,有点多余。。
就想把所有的都搬进main方法,但就是这个addActionListener( this)进不来。。我把this换了很多变量名都不对。

请高手指点。怎么样才能不用这个this?

下面是题目。。
//编程练习801
//创建一个框架并将其布局管理设为FlowLayout
//创建两个面板并加入
//每个面板包含三个按键,并使用FlowLayout
//点击按键时,在控制台显示信息。

import  t.*;
import javax.swing.*;
import  t.event.*;

public class Test801 extends JFrame
implements ActionListener

{
       private  JButton j01=new JButton("bu01");
        private  JButton j02=new JButton("bu02");
         private JButton j03=new JButton("bu03");
         private JButton j10=new JButton("bu10");
         private JButton j20=new JButton("bu20");
       private JButton j30=new JButton("bu30");

  public static void main(String[] args)
  {
    Test801 frame=new Test801();
  }
  public Test801()
  {

      JFrame myFrame=new JFrame("Test801");
       tDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       tSize(400,300);
         tVisible(true);
         tLayout(new FlowLayout());


        JPanel p1=new JPanel();
        JPanel p2=new JPanel();
         tLayout(new FlowLayout());
         tLayout(new FlowLayout());



         dActionListener( this);
         dActionListener(this);
         dActionListener( this);
         dActionListener( this);
         dActionListener(this);
         dActionListener( this);



         d(j01);
         d(j02);
         d(j03);
         d(j10);
         d(j20);
         d(j30);

         d(p1);
         d(p2);


  }

    public void actionPerformed(ActionEvent e)
    {
      if( tSource()==j01)
      {
         intln("The bu01 button is clicked");
      }
      else if ( tSource()==j02)
      {
         intln("The bu02 button is clicked");
      }
            else if ( tSource()==j03)
            {
               intln("The bu03 button is clicked");
      }
            else if ( tSource()==j10)
            {
               intln("The bu10 button is clicked");
      }
            else if ( tSource()==j20)
            {
               intln("The bu20 button is clicked");
      }
            else if ( tSource()==j30)
            {
               intln("The bu30 button is clicked");
      }
  }

}

提交回答
好评回答
  • 2008-03-17 14:59:33
    这样写才好,才体现出java的面向对象的思想,在这里this指的是main函数里的new Test801()这个对象,若一定要加在main函数里
     dActionListener(frame); 
    就可以了
    

    p***

    2008-03-17 14:59:33

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 176-195
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):