我wa的一声就哭出来了 2021-06-22 11:44 采纳率: 100%
浏览 200
已结题

IDEA GUI-管理系统登录界面报错

package view;

import dao.UserDao;
import model.User;
import Until.DBUtils;
import Until.StringUtil;

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

public class login {
    private JPanel login;
    private JTextField username;
    private JPasswordField Password;
    private JButton Buttond;
    private JButton Buttonc;


    private DBUtils dbUtil=new DBUtils();
    private UserDao userDao=new UserDao();

    public login() {
        Buttonc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                resetvalueaction(e);
            }
        });
        Buttond.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginaction(e);
            }
        });
    }

//重置事件处理
    private void resetvalueaction(ActionEvent evt) {
        this.Password.setText("");
        this.username.setText("");
    }
//登录事件处理
    private void loginaction(ActionEvent evt) {
       String username=this.username.getText();
       String password=new String(this.Password.getPassword());
        if (StringUtil.isEmpty(username)){
            JOptionPane.showMessageDialog(null,"用户名不能为空!");
            return;
        }
        if(StringUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"密码不能为空!");
            return;
        }
        User user=new User(username,password);
        Connection con=null;
        try {
            con=dbUtil.getConn();
            User cc=userDao.login(con,user);
            if(cc!=null) JOptionPane.showMessageDialog(null,"登录成功");
            else JOptionPane.showMessageDialog(null,"用户名密码错误!");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("login");
        frame.setContentPane(new login().login);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }


    }

下面为报错:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.awt.Container.setLayout(java.awt.LayoutManager)" because "<local1>" is null
	at view.login.$$$setupUI$$$(login.java)
	at view.login.<init>(login.java:22)
	at view.login.main(login.java:71)
  • 写回答

2条回答 默认 最新

  • 关注

    frame.setContentPane(new login().login); 错误

    setContentPane方法里面只能设置布局方式,如

    setContentPane(new BorderLayout());

    整个程序有很多问题,控件都没有创建,也没有放到界面。参考

    package java3.T6;
    
    import java.awt.Button;
    import java.awt.Frame;
    import java.awt.Label;
    import java.awt.TextField;
    
    public class LoginUI extends Frame {
    	
    	public LoginUI() {
    		init();
    	}
    	public LoginUI(String title) {
    		//设置窗口标题
    //		super("用户登录");
    		init();
    	}
    	private void init() {
    		//设置窗口标题
    		setTitle("用户登录");
    		//取消布局
    		setLayout(null);
    		
    		Label lblUser = new Label("用户名称:",Label.RIGHT);
    		Label lblPass = new Label("登录密码:",Label.RIGHT);
    		//文本框
    		TextField txtUser = new TextField(10);
    		TextField txtPass = new TextField(10);
    		//按钮
    		Button btnLogin = new Button("登录");
    		Button btnClose = new Button("关闭");
    		//设置控件在窗口上的位置(x,y,width,height)
    		lblUser.setBounds(100, 110, 100, 20);
    		lblPass.setBounds(100, 160, 100, 20);
    		txtUser.setBounds(200, 110, 200, 20);
    		txtPass.setBounds(200, 160, 200, 20);
    		
    		btnLogin.setBounds(280, 230, 90, 30);
    		btnClose.setBounds(390, 230, 90, 30);
    		add(lblUser);
    		add(lblPass);
    		add(txtUser);
    		add(txtPass);
    		add(btnLogin);
    		add(btnClose);
    		//设置窗口大小
    		setSize(530,300);
    		//显示窗口
    		setVisible(true);
    		
    	}
    	public static void main(String[] args) {
    		new LoginUI();
    	}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月16日

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数