当前位置: 代码迷 >> Java相关 >> 有段代码不明白
  详细解决方案

有段代码不明白

热度:116   发布时间:2008-08-16 17:48:09.0
有段代码不明白
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JPasswordFieldDemo extends JFrame
{
    JTextField userName;
    JPasswordField passWord;
    JButton logonButton;
    JButton cancelButton;
    
    public JPasswordFieldDemo()
    {
        super("JPasswordField演示");
        Container container=getContentPane();
        container.setLayout(new GridLayout(3,2,2,2));
        
        userName=new JTextField(16);
        passWord=new JPasswordField(16);
        logonButton=new JButton("登录");
        logonButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                char[] pw=passWord.getPassword();
                String message="您的用户名:"+userName.getText()+"\n你的密码:"+new String(pw);
                JOptionPane.showMessageDialog(JPasswordFieldDemo.this,  message);
            }    
        });
        cancelButton=new JButton("退出");
        cancelButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                System.exit(0);
            }
        });
        
        container.add(new JLabel("   用户名:"));
        container.add(userName);
        container.add(new JLabel("   密码:"));
        container.add(passWord);
        container.add(logonButton);
        container.add(cancelButton);
        setResizable(false);
        setSize(300,120);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args)
    {
        new JPasswordFieldDemo();
    }
}

中的JPasswordFieldDemo.this 指的是什么啊
搜索更多相关的解决方案: import  awt  container  new  java  

----------------解决方案--------------------------------------------------------
指当前的容器吧

可以用一个声明一个Container,可以把JPasswordFieldDemo.this替换成container
----------------解决方案--------------------------------------------------------
showMessageDialog(JFrame j,String message)
一定有这个方法
JPasswordFieldDemo.this 就是当前Swing容器
----------------解决方案--------------------------------------------------------
详见core jave 第1卷 swing 里有说明, 具体那节记不住了。

this 指的是当前的object

JOptionPane.showMessageDialog(JPasswordFieldDemo.this,  message);

这里如果只用 this , this 指的是 logonButton
而 用JPasswordFieldDemo.this  , 则指的是外层的  JPasswordFieldDemo 的一个实体
----------------解决方案--------------------------------------------------------
  相关解决方案