import javax.swing.*; import java.awt.*; import java.awt.event.*; class test extends JFrame { public test() { super( "Test" );
JButton b = new JButton( "Click"); b.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { JOptionPane.showMessageDialog( test.this, "Just a test", "test", JOptionPane.PLAIN_MESSAGE ); } });
getContentPane().add( b ); setSize( 300, 400 ); setVisible( true ); } public static void main( String [] args ) { test f = new test(); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } --------------------------------------------------------------------- 直接用this将传递一个匿名内置类(匿名内部类)ActionListener,用test.this则传递test的实例对象。 参考!!!!
(1)指代对象本身。
(2)访问本类的成员变量或成员方法。
如:this.<变量名>
this.<方法名>
(3)调用本类的构造方法
没有这个写法吧?
这是在一个类的构造器里的
如Class b=new Class(Class c.this);
1、在自身的构造方法内部调用自身的其他构造方法
public Test(){
this(10);
} public Test(int n){ }
2、代表自身类的对象
3、使用this.属性名引用被覆盖掉的属性
import java.awt.*;
import java.awt.event.*;
class test extends JFrame
{
public test()
{
super( "Test" );
JButton b = new JButton( "Click");
b.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
JOptionPane.showMessageDialog( test.this, "Just a test", "test", JOptionPane.PLAIN_MESSAGE );
}
});
getContentPane().add( b );
setSize( 300, 400 );
setVisible( true );
}
public static void main( String [] args )
{
test f = new test();
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
---------------------------------------------------------------------
直接用this将传递一个匿名内置类(匿名内部类)ActionListener,用test.this则传递test的实例对象。
参考!!!!
this()表示调用自己类的构造函数
this.xx表示调用本类的变量,变量名是xx