0
package Project;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

 class mydlg extends JDialog
{
    JButton b1,b2;
    JLabel f1;
    public mydlg(JFrame f,String t,boolean b)
    {
        super(f,t,b);
        Container con=getContentPane();
        con.setLayout(new FlowLayout());
        b1=new JButton(new ImageIcon("images/diamond.gif"));
        f1=new JLabel("Oops!!! You Must Enter the correct Password.");
        b2=new JButton("OK");
        con.setLocation(250, 250);
        con.add(b1);
        con.add(f1);
        con.add(b2);
        f.setDefaultCloseOperation(HIDE_ON_CLOSE);
        b2.setSize(250,250);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        con.setBackground(Color.orange);    
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);

            }
   });
    }
}


public class Login extends JFrame implements ActionListener{
String str;
JTextField lt;
TextField pt;
JButton ba;
JLabel ll,pl,jb1;
public Login() throws Exception
{
Container con=getContentPane();
con.setLayout(new FlowLayout());
        jb1=new JLabel(new ImageIcon("images/login1.jpg"));
        con.add(jb1);   
con.setBackground(Color.cyan);
//con.setSize(1300, 1400);
lt=new JTextField(20);
pt=new TextField(20);
pt.setEchoChar('*');
ll=new JLabel("Username:");
pl=new JLabel("Password:");
lt.setText("Admin");
ba=new JButton("LOGIN");
        ll.setBounds(45,80,150,30);
        lt.setBounds(140,80,150,30);
        pl.setBounds(45,140,150,30);
        pt.setBounds(140,140,150,30);
        ba.setBounds(140,200,90,30);

con.add(ll);
con.add(lt);
con.add(pl);
con.add(pt);
con.add(ba);
ba.addActionListener(this);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
    try{

        if(ae.getSource()==ba){
        if(pt.getText().equals("TDJSS")){
        new PrjtMain();
        //rm.setSize(700,700);
        //rm.setVisible(true);

                }
        else{
        mydlg md=new mydlg(this,"Error Login Message",false);
        md.setSize(300,200);
        md.setLocationRelativeTo( null );
        md.setVisible(true);
        String cmd = ae.getActionCommand();
        if(cmd.equals("OK"))
        {
        System.exit(0);
        }
        }             
        }

       }catch(Exception ex){}
}

public static void main(String arg[])throws Exception
{
Login l=new Login();
l.setSize(350,350);
l.setLocationRelativeTo( null );
l.setTitle("LOGIN");
l.setVisible(true);
}
}

海....我成功运行此代码。但是当我单击登录按钮而不输入密码时,它显示一条错误消息。之后,当我单击“确定”按钮时,两个框架同时关闭。我想让主登录页面保持不变..请帮助我..提前致谢。

4

1 回答 1

1

System.exit(0);b2 ActionListener将退出JVM...

相反,我认为你想使用dispose

b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        dispose();
    }
});

我也将停止更改父窗口的默认关闭操作。

我不是 100% 确定,但我认为你假设这是对话框的结果,它不是......你在按钮ActionEvent的上下文中引用当前LoginActionListener

String cmd = ae.getActionCommand();
if (cmd.equals("OK")) {
    System.exit(0);
}

你也不应该忽略异常......

} catch (Exception ex) {
}

这是非常糟糕的,你不应该这样做......

您可能还想查看Java 编程语言的代码约定

于 2013-09-30T05:21:46.803 回答