日期:2014-05-20  浏览次数:20980 次

事件处理
写登录窗口碰到个小问题:
继承了JFrame的类中的一个方法中的用匿名类方式添加事件响应的组件的事件处理器中怎么访问当前类的main方法中定义的当前类的实例呢?
[code=Java][/code]public class LoginFrame extends javax.swing.JFrame {
 private JButton jButtonZC; // 注册按钮
 private JButton jButtonLogin; // 登录按钮
 private JTextField nameTF; // 用户名文本框
 private JTextField passwordTF; // 密码文本框

 public LoginFrame() {
 super();
// LoginFrame lf = new LoginFrame();
 initGUI();
 }

 /**
 * Auto-generated main method to display this JFrame
 */
 public static void main(String[] args) {
 SwingUtilities.invokeLater(new Runnable() {
 public void run() {
 LoginFrame lf = new LoginFrame();
 lf.setLocationRelativeTo(null);// 置中
lf.setVisible(true);
 }
 });
 }

 /*
 * 初始化登录界面
*/
 private void initGUI() {
 try {
 GroupLayout thisLayout = new GroupLayout(
 (JComponent) getContentPane());
 getContentPane().setLayout(thisLayout);
 this.setResizable(false);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setForeground(new java.awt.Color(128, 128, 128));
 this.setTitle("\u97f3\u4e50\u5e73\u53f0");
 /*
 * 注册按钮及事件
*/

jButtonZC = new JButton();
 jButtonZC.setText("\u6ce8\u518c");
 jButtonZC.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent evt) {
// lf.setVisible(false);
 // System.out.println("这里可以显示注册窗口了");
new LogonJFrame().main2();// 真的显示注册窗口了
}
 });
[code=Java][/code]

就看上面的事件处理器就好了,怎样在里面关闭main方法中定义的lf呢,实现点击注册时候可以关闭登录窗口

------解决方案--------------------
Java code
jButtonZC.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
//                        lf.setVisible(false);
                        // System.out.println("这里可以显示注册窗口了");
                         LoginFrame.this.dispose();         
                        new LogonJFrame().main2();// 真的显示注册窗口了
                    }
                });