📄 memberlogin.java
字号:
package com.hotdon.mem;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class memberLogin {
//定义组件
JFrame frame;
JLabel lbAccount;
JLabel lbPassword;
JTextField textAccount;
JPasswordField textPassword;
JButton loginButton;
public memberLogin(){
//初始化组件
frame=new JFrame("客户登录");
lbAccount= new JLabel("账号");
lbPassword= new JLabel("密码");
textAccount= new JTextField(15);
textPassword= new JPasswordField(8);
loginButton=new JButton("登录");
//初始化布局
Container contentPane=frame.getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel panel= new JPanel();
GridBagLayout gb=new GridBagLayout();
panel.setLayout(gb);
GridBagConstraints gbc= new GridBagConstraints();
gbc.anchor=GridBagConstraints.WEST;
gbc.insets= new Insets(2,2,2,2);
panel.setBorder(BorderFactory.createTitledBorder(""));
//使窗口们于中心
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=frame.getSize();
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
frame.setVisible(true);
//添加组件到面板
gbc.gridy=1;
gbc.gridx=0;
gb.setConstraints(lbAccount,gbc);
panel.add(lbAccount,gbc);
gbc.gridy=2;
gbc.gridx=0;
gb.setConstraints(lbPassword,gbc);
panel.add(lbPassword,gbc);
gbc.gridy=1;
gbc.gridx=1;
gb.setConstraints(textAccount,gbc);
panel.add(textAccount,gbc);
gbc.gridy=2;
gbc.gridx=1;
gb.setConstraints(textPassword,gbc);
panel.add(textPassword,gbc);
gbc.gridy=3;
gbc.gridx=0;
gb.setConstraints(loginButton,gbc);
panel.add(loginButton,gbc);
//”登录“按钮事件
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(! textAccount.getText().trim().equals("")&&
!textPassword.getPassword().equals("")){
dataDAO dao=null;
dao=(com.hotdon.mem.dataDAO) new com.hotdon.dao.dataDAOImpl();
try{
String QueryString;
QueryString="select * from memberTable";
ResultSet rsl=dao.getData(QueryString);
int flag1=0,flag2=1;
while(rsl.next()){
flag1++;
}
ResultSet rs=dao.getData(QueryString);
while(rs.next()){
String strTemp;
if(rs.getString("memberID").equals(textAccount.getText())){
flag2=0;
if(rs.getString("password").equals(
textPassword.getText())){
String memberID;
memberID=textAccount.getText();
logform logframe= new logform(null,memberID);
logframe.pack();
logframe.setVisible(true);
frame.setVisible(false);
//使窗口位于中心
Dimension screenSize;
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=logframe.getSize();
if(frameSize.height>screenSize.height){
frameSize.height=screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
logframe.setLocation((screenSize.width -
frameSize.width-10),(screenSize.height -
frameSize.height)-25);
logframe.setVisible(true);
}
else{
//消息框代码
final JDialog dialog= new JDialog((Frame)null,"错误",true);
Container dialogContenPane=dialog.getContentPane();
dialogContenPane.setLayout(new FlowLayout());
dialogContenPane.add(new JLabel("密码不正确"));
JButton jbutton= new JButton("正确");
dialogContenPane.add(jbutton);
jbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.dispose();
textPassword.setText("");
}
});
dialog.setBounds(450,300,100,90);
dialog.setVisible(true);
}
}
if(flag2==0){
break;
}
flag2++;//查找不到数据库,flag2标志加1
if(flag2>flag1){
//消息框代码
final JDialog dialog= new JDialog((Frame) null,"错误",true);
Container dialogContenPane=dialog.getContentPane();
dialogContenPane.setLayout(new FlowLayout());
dialogContenPane.add(new JLabel("用户不存在"));
JButton jbutton= new JButton("确认");
dialogContenPane.add(jbutton);
jbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.dispose();
textAccount.setText("");
textPassword.setText("");
}
});
dialog.setBounds(450,300,100,90);
dialog.setVisible(true);
}
}//while
}//try
catch(Exception exception){
exception.printStackTrace();
}
}
else{
//消息框代码
final JDialog dialog= new JDialog((Frame) null,"错误",true);
Container dialogContenPane=dialog.getContentPane();
dialogContenPane.setLayout(new FlowLayout());
dialogContenPane.add(new JLabel("账号或密码不能为空"));
JButton jbutton=new JButton("确认");
dialogContenPane.add(jbutton);
jbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dialog.dispose();
}
});
dialog.setBounds(450,300,130,90);
dialog.setVisible(true);
}
}
});
//"退出“按钮事件
gbc.gridy=3;
gbc.gridx=1;
JButton exitButton=new JButton("退出");
panel.add(exitButton,gbc);
exitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.dispose();
}
});
contentPane.add(panel);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception exception){
exception.printStackTrace();
}
new memberLogin();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -