从JDialog继承一个类出来,添加用户输入的控件,添加事件响应就好了。

解决方案 »

  1.   

    我做的登陆窗口
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.JTextComponent;public class LoginUI extends JDialog
    {    JTextField userFiled;
        JPasswordField passwordFiled;
        Container c;    public LoginUI()
        {
            userFiled = null;
            passwordFiled = null;
            c = null;
            String title = "System Login";
            JFrame f = new JFrame();
            new LoginUI(f, title);
        }    public LoginUI(JFrame f, String title)
        {
            super(f, title, true);
            userFiled = null;
            passwordFiled = null;
            c = null;
            setSize(400, 160);
            setResizable(false);
            Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
            setLocation(ds.width / 3, ds.height / 4);
            c = getContentPane();
            c.setLayout(new FlowLayout(0));
            addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent ew)
                {
                    System.exit(0);
                }        });
            JPanel p = new JPanel();
            p.setLayout(new GridLayout(2, 1));
            JLabel label1 = new JLabel(new ImageIcon("Key.gif"));
            p.add(label1);
            JPanel p1 = new JPanel();
            p1.setLayout(new GridLayout(5, 1));
            JPanel p2 = new JPanel();
            p2.setLayout(new GridLayout(4, 1));
            JButton okButton = new JButton("OK");
            ActionListener okAction = new ActionListener() {            public void actionPerformed(ActionEvent ea)
                {
                    System.exit(0);
                }        };
            JButton cancelButton = new JButton("Cancel");
            ActionListener cancelAction = new ActionListener() {            public void actionPerformed(ActionEvent ea)
                {
                    System.exit(0);
                }        };
            cancelButton.addActionListener(cancelAction);
            p2.add(okButton);
            p2.add(cancelButton);
            JLabel label = new JLabel("\u8BF7\u60A8\u8F93\u5165\u6709\u6548\u7684\u7CFB\u7EDF\u767B\u9646\u53E3\u4EE4\u3002");
            JPanel p3 = new JPanel();
            p3.setLayout(new FlowLayout(0));
            JLabel passwordName = new JLabel("\u53E3\u4EE4:");
            JPanel p4 = new JPanel();
            p4.setLayout(new FlowLayout(0));
            JLabel userName = new JLabel("\u7528\u6237:");
            userFiled = new JTextField(16);
            passwordFiled = new JPasswordField(16);
            ActionListener ok_Action = new ActionListener() {            public void actionPerformed(ActionEvent ea)
                {
                    String user_str = userFiled.getText();
                    String password_str = passwordFiled.getText();
                    if(user_str.trim() == null || user_str.equals(""))
                    {
                        JOptionPane.showMessageDialog(c, "\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A!", "System Message!", 0);
                        return;
                    }
                    if(password_str.trim() == null || password_str.equals(""))
                    {
                        JOptionPane.showMessageDialog(c, "\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A!", "System Message!", 0);
                        return;
                    }
                    if(!user_str.trim().equals("yanglongfei"))
                    {
                        JOptionPane.showMessageDialog(c, "\u7528\u6237\u540D\u9519\u8BEF!", "System Message!", 0);
                        return;
                    }
                    if(!password_str.trim().equals("yanglongfei"))
                    {
                        JOptionPane.showMessageDialog(c, "\u5BC6\u7801\u9519\u8BEF!", "System Message!", 0);
                        return;
                    } else
                    {
                        setVisible(false);
                        dispose();
                        return;
                    }
                }        };
            okButton.addActionListener(ok_Action);
            p3.add(userName);
            p3.add(userFiled);
            p4.add(passwordName);
            p4.add(passwordFiled);
            p1.add(label);
            p1.add(p3);
            p1.add(p4);
            c.add(p);
            c.add(p1);
            c.add(p2);
            setVisible(true);
        }
    }