1.用java写一个登录界面的代码,哪位大神会啊,谢谢import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Test26 { public static void main(String[] args) { final String userName = "abc"; final String passwrod = "123"; JFrame jFrame = new JFrame("登陆界面"); Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 200, 150); jFrame.setResizable(false); jFrame.setLayout(null); jFrame.(JFrame.EXIT_ON_CLOSE); JLabel label1 = new JLabel("姓名"); label1.setBounds(10, 10, 100, 30); jFrame.add(label1); JLabel label2 = new JLabel("密码"); label2.setBounds(10, 40, 100, 30); jFrame.add(label2); final JTextField text1 = new JTextField(); text1.setBounds(50, 15, 130, 20); jFrame.add(text1); final JPasswordField text2 = new JPasswordField(); text2.setBounds(50, 45, 130, 20); jFrame.add(text2); JButton button = new JButton("Login"); button.setBounds(10, 75, 170, 40); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(userName.equals(text1.getText()) && passwrod.equals(text2.getText())) { JOptionPane.showMessageDialog(null,"登陆成功误","提示",JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null,"错误","提示",JOptionPane.ERROR_MESSAGE); text1.setText(""); text2.setText(""); } } }); jFrame.add(button); jFrame.setVisible(true); } }我有一个微信公众号,经常会分享一些Java技术相关的干货,还有一些学习资源 。
如果你喜欢我的分享,可以用微信搜索“Java团长”或者“javatuanzhang”关注 。
2.怎么用JAVA写一个用户登入程序同意楼上的说法,具体点可以这样:创建一个用户表,里边包括LoginName(登录名),UserName(用户名),Password(密码),Age(年龄),Address(地址) 。
然后编写Java程序(用MVC架构)模型层(M):DBConnection.java(负责连接数据库)import java.sql.Connection;import java.sql.DriverManager;import java.sql.*;public class DBConnection { private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver"; private static final String DB_URL = "jdbc:odbc:text"; public DBConnection() { } public static Connection getConnection() { Connection conn = null; try { Class.forName(DRIVER_CLASS); conn = DriverManager.getConnection(DB_URL); } catch (SQLException ex) { System.out.println(ex.getMessage()); } catch ( ex) { System.out.println(ex.getMessage()); } return conn; }}第2个负责数据库查询操作的类:DBUserManager.javaimport edu.systop.text.model.entity.User;import edu.systop.text.model.dao.DBConnection;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.DriverManager;import java.sql.*;public class DBUserManager { private static final String SQL_SELECT = "SELECT LoginName,UserName,PassWord,Age,Address FROM UserInfo WHERE LoginName = ? AND PassWord = ?"; public DBUserManager() { } public boolean checkDB(User u) { boolean b = false; Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; conn = DBConnection.getConnection(); try { psmt = conn.prepareStatement(SQL_SELECT); psmt.setString(1, u.getLoginName()); psmt.setString(2, u.getPassWord()); rs = psmt.executeQuery(); b = rs.next(); if (rs.next()) { b = true; } } catch (SQLException ex) { System.out.println(ex.getMessage()); } finally { cleanDB(rs, psmt, conn); } return b; } public User checkBC(User u) { Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; User tmp = new User(); conn = DBConnection.getConnection(); try { psmt = conn.prepareStatement(SQL_SELECT); psmt.setString(1, u.getLoginName()); psmt.setString(2, u.getPassWord()); rs = psmt.executeQuery(); if (rs.next()) { tmp.setLoginName(rs.getString(1)); tmp.setUserName(rs.getString(2)); tmp.setAge(rs.getInt(4)); tmp.setAddress(rs.getString(5)); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } finally { cleanDB(rs, psmt, conn); } return tmp; } public void cleanDB(ResultSet rs, PreparedStatement psmt, Connection conn) { try { if (rs != null) { rs.close(); } if (psmt != null) { psmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } }第3个实体用户类:User.javapackage edu.systop.text.model.entity;public class User { private String loginName; private String userName; private String passWord; private int age; private String address; public User() { } public void setLoginName(String loginName) { this.loginName = loginName; } public void setUserName(String userName) { this.userName = userName; } public void setPassWord(String passWord) { this.passWord = passWord; } public void setAge(int age) { this.age = age; } public void setAddress(String address) { this.address = address; } public String getLoginName() { return loginName; } public String getUserName() { return userName; } public String getPassWord() { return passWord; } public int getAge() { return age; } public String getAddress() { return address; }}然后编写控制层(C):GetInfoServlet.javapackage edu.systop.text.control;import javax.servlet.*;import javax.servlet.; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String loginName = request.getParameter("loginName"); String passWord = request.getParameter("passWord"); User u = new User(); u.setLoginName(loginName); u.setPassWord(passWord); UserManager m = new UserManager();RequestDispatcher d; if (m.checkUser(u)) { User o = m.checkBC(u); request.setAttribute("JavaBEAN",o); d = request.getRequestDispatcher("GetInfoUser.jsp"); } else { d = request 。. 。
- 电脑没声音怎么弄 电脑没声音怎么办win7
- cf怎么截图 CF游戏中怎么截图
- qq空间访问权限 手机怎么设置qq空间访问权限
- 鱼肉馅饺子怎么做 鱼肉馅饺子做法视频
- 怎么开葡萄酒 怎么开葡萄酒木塞小妙招
- 洗龙沟是什么意思 洗龙沟是什么意思衣机里面太脏了怎么办
- wow使命召唤怎么做 使命召唤怎么弄
- 文件隐藏了怎么恢复 windows文件隐藏了怎么恢复
- 英雄联盟查战斗力 英雄联盟怎么查战斗力
- 安卓手机如何省电 安卓手机怎么节省电量