神经细胞qwer 2021-01-14 01:02 采纳率: 0%
浏览 939

JOptionPane.showMessageDialog窗口无法弹出

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JOptionPane;

public class Systems extends JFrame implements ActionListener {
    static  Systems ss;
    JPanel panel=new JPanel();
    JLabel label1=new JLabel("账号:");
    JTextField id=new JTextField();
    JLabel label2=new JLabel("密码:");
    JPasswordField pwd=new JPasswordField();
    JButton Enter=new JButton("登录");
    JButton Exit=new JButton("退出");
    ButtonGroup bgp=new ButtonGroup();
    JRadioButton use=new JRadioButton("用户");
    JRadioButton adm=new JRadioButton("管理员");
    public Systems(){
        super("二手车登陆系统");
        this.setResizable(false);
        use.setBounds(165,210,70,20);
        adm.setBounds(265,210,70,20);
        bgp.add(use);
        bgp.add(adm);
        panel.add(use);
        panel.add(adm);
        Enter.setBounds(150,250,80,20);
        Exit.setBounds(270,250,80,20);
        Enter.addActionListener(this);
        Exit.addActionListener(this);
        panel.add(Enter);
        panel.add(Exit);
        panel.setLayout(null);
        this.add(panel);
        label1.setBounds(135,130,100,25);
        panel.add(label1);
        id.setBounds(265,130,100,25);
        panel.add(id);
        label2.setBounds(135,165,100,25);
        panel.add(label2);
        pwd.setBounds(265,165,100,25);
        panel.add(pwd);
        this.setBounds(100,100,500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==Enter){
            String userid,password;
            userid=id.getText();
            password=new String(pwd.getPassword());
            try{
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            }
            catch (ClassNotFoundException ce){
                JOptionPane.showMessageDialog(ss,ce.getMessage());
            }
            if(use.isSelected()) {
                try {
                    Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;", "sa", "liu872603526");
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery("select *from Users");
                    while (rs.next()) {
                        if (rs.getString("Uid").equals(userid)) {
                            if ((rs.getString("Upass").equals(password))) {
                                JOptionPane.showMessageDialog(ss, "登陆成功");
                                new User();
                            } else {
                                JOptionPane.showMessageDialog(ss, "登录失败");
                            }
                            rs.close();
                            stmt.close();
                        }
                    } }catch(SQLException se){
                        JOptionPane.showMessageDialog(ss, se.getMessage());
                    }
                }
            else if(adm.isSelected()){
                try{
                    Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;","sa","liu872603526");
                    Statement stmt=con.createStatement();
                    ResultSet rs=stmt.executeQuery("select *from Admin");
                    while(rs.next()){
                        if(rs.getString("Aid").equals(userid))
                            if((rs.getString("Apass").equals(password))){
                                JOptionPane.showMessageDialog(ss,"登陆成功");
                                new Admins();
                            }
                            else{
                                JOptionPane.showMessageDialog(ss,"登陆失败");
                            }
                    }
                }
                catch (SQLException se) {
                    JOptionPane.showMessageDialog(ss,se.getMessage());
                }
            }
        }
        else{
            System.exit(0);
        }
    }
    public static void main(String[] args){
        new Systems();
    }
}

点击登录按钮没反应,程序也没有报错

 

  • 写回答

2条回答 默认 最新

  • 小宇0926 2021-01-15 16:22
    关注

    应该是进行sql查询的时候出错了 我把你的Sql逻辑删除了,就OK了

    package cn.yu.test;
    
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.*;
    import javax.swing.JOptionPane;
    
    public class Systems extends JFrame implements ActionListener {
        static Systems ss;
        JPanel panel = new JPanel();
        JLabel label1 = new JLabel("账号:");
        JTextField id = new JTextField();
        JLabel label2 = new JLabel("密码:");
        JPasswordField pwd = new JPasswordField();
        JButton Enter = new JButton("登录");
        JButton Exit = new JButton("退出");
        ButtonGroup bgp = new ButtonGroup();
        JRadioButton use = new JRadioButton("用户");
        JRadioButton adm = new JRadioButton("管理员");
    
        public Systems() {
            super("二手车登陆系统");
            this.setResizable(false);
            use.setBounds(165, 210, 70, 20);
            adm.setBounds(265, 210, 70, 20);
            bgp.add(use);
            bgp.add(adm);
            panel.add(use);
            panel.add(adm);
            Enter.setBounds(150, 250, 80, 20);
            Exit.setBounds(270, 250, 80, 20);
            Enter.addActionListener(this);
            Exit.addActionListener(this);
            panel.add(Enter);
            panel.add(Exit);
            panel.setLayout(null);
            this.add(panel);
            label1.setBounds(135, 130, 100, 25);
            panel.add(label1);
            id.setBounds(265, 130, 100, 25);
            panel.add(id);
            label2.setBounds(135, 165, 100, 25);
            panel.add(label2);
            pwd.setBounds(265, 165, 100, 25);
            panel.add(pwd);
            this.setBounds(100, 100, 500, 350);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == Enter) {
                String userid, password;
                userid = id.getText();
                password = new String(pwd.getPassword());
                if (use.isSelected()) {
                    if ("user".equals(userid) && "123".equals(password)) {
                        JOptionPane.showMessageDialog(ss, "登陆成功");
                    } else {
                        JOptionPane.showMessageDialog(ss, "登录失败");
                    }
                } else if (adm.isSelected()) {
                    if ("admin".equals(userid) && "123".equals(password)) {
                        JOptionPane.showMessageDialog(ss, "登陆成功");
                    } else {
                        JOptionPane.showMessageDialog(ss, "登陆失败");
                    }
                }
            } else {
                System.exit(0);
            }
    
        }
    
        public static void main(String[] args) {
            new Systems();
        }
    }
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!