package com;import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
class   LoginWindow extends JDialog implements ActionListener
{    JPanel p1=new JPanel(); //定义并建立面板
     JPanel p2=new JPanel();
     JPanel p3=new JPanel();
     JPanel p4=new JPanel();
     JPanel p5=new JPanel();
     JTextField txtUserName=new JTextField(15); //用户名文本框
     JPasswordField txtPwd=new JPasswordField(15);//密码框
     JButton ok=new JButton("确定");
     JButton cancel=new JButton("取消");
     public LoginWindow(){
      setModal(true); //设置模态
      setBackground(Color.LIGHT_GRAY);//设置背景色
      Container contentPane=this.getContentPane();//取出内容面板
      contentPane.setLayout(new GridLayout(5,1)); //设置布局为5行1列
      p2.add(new JLabel("用户名:"));p2.add(txtUserName); //将组件添加到中间容器
      p3.add(new JLabel("密     码:"));p3.add(txtPwd);
      p4.add(ok);p4.add(cancel);
      ok.addActionListener(this);
      cancel.addActionListener(this);
      txtUserName.addActionListener(this);
      txtPwd.addActionListener(this);
      contentPane.add(p1);   //将面板添加到内容面板
      contentPane.add(p2);
      contentPane.add(p3);
      contentPane.add(p4);
      contentPane.add(p5);
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置自动关闭窗口
      setSize(300,220);
      Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
      setLocation((screen.width-300)/2,(screen.height-220)/2);
      setTitle("登录窗口");
      setResizable(false); //不让用户改变窗口大小
      setVisible(true);
    }
public void actionPerformed(ActionEvent e){
    if(e.getSource()==ok||e.getSource()==txtPwd)   //单击确定按钮后
    
   { 
     char[] passwordChar=txtPwd.getPassword();     for(;txtPwd.getPassword()!=null;)
     if(txtUserName.getText().trim().equals("wdx") && txtPwd.getText().equals("wdx"))
     {dispose();   //关闭登录窗口
           //new MainWindow(); //调出主窗口
     }
            else
            { 
             JOptionPane.showMessageDialog(null,"用户名或密码错误!");             txtUserName.requestFocus(); //设置焦点
             txtUserName.setSelectionStart(0); //设置选中文本开始位置
             txtUserName.setSelectionEnd(txtUserName.getText().length());
            }
    }
       else if(e.getSource()==cancel) //单击取消
       {   
        dispose();
        System.exit(0);
      }
       else if(e.getSource()==txtUserName) //在用户名文本框按回车移动焦点到密码框
      { txtPwd.requestFocus();
   }
}
public static void main(String[] args) 
{
   JDialog.setDefaultLookAndFeelDecorated(true);
   Font font=new Font("JFrame",Font.PLAIN,14);
   Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
   while(keys.hasMoreElements())
   { Object key=keys.nextElement();
     if(UIManager.get(key) instanceof Font)UIManager.put(key,font);
   }
   new LoginWindow();
}
}为什么弹出窗口关不了?

解决方案 »

  1.   

    看错了,不好意思... for(;txtPwd.getPassword()!=null;)
    (这行没什么用啊?)
    这个for循环删了就没问题了..
      

  2.   

    2L说法完全正确。那一行没用。
    这是我见过最个性的FOR循环
      

  3.   

    看看这个地方  http://www.jooplay.com/?374827
      

  4.   

    char[] passwordChar=txtPwd.getPassword();
            for(;txtPwd.getPassword()!=null;)
    都没有用。
    txtPwd.getText()也是过时方法。
    并且  if(e.getSource()==ok||e.getSource()==txtPwd)   //单击确定按钮后
    这个判断为什么要e.getSource()==txtPwd进行这个判断呢?