📄 user_login.java
字号:
package warehouse;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class User_Login extends JPanel//用户登陆界面
{
public User_Login(JTabbedPane tabs)
{
this.tabs=tabs;
this.setLayout(new BorderLayout());
setBackground(new Color(187,242,24));
//this.setBorder(new MatteBorder(new ImageIcon(getClass().getResource("image/5.gif"))));
system.setForeground(Color.red);
system.setFont(new Font("宋体",Font.BOLD,40));
add(system,"North");
JPanel outPanel=new JPanel();
outPanel.setLayout(new GridBagLayout());
outPanel.setBackground(new Color(187,242,24));
GridBagConstraints c=new GridBagConstraints();
c.gridx=0;
c.gridy=0;//表示下一个单元格在前一个添加组件的下方
c.gridwidth=3;
c.gridheight=1;
c.weightx=1.0;
c.weighty=0.0;
c.insets=new Insets(5,5,5,5);//指定组建外围需要填充的空间
c.fill=GridBagConstraints.NONE;
c.anchor=GridBagConstraints.CENTER;//如果组件没有占据单元格内的所有空间,该值将控制组件在单元格中的放置方式
register.setFont(new Font("Serif",Font.BOLD,17));
outPanel.add(register,c);
c.gridx=0;
c.gridy=1;
c.gridwidth=1;
c.gridheight=1;
c.anchor=GridBagConstraints.EAST;
outPanel.add(usernameLabel,c);
c.gridx=1;
c.gridy=1;
c.gridwidth=1;
c.anchor=GridBagConstraints.WEST;
outPanel.add(username,c);
c.gridx=0;
c.gridy=2;
c.gridwidth=1;
c.anchor=GridBagConstraints.EAST;
outPanel.add(passwordLabel,c);
c.gridx=1;
c.gridy=2;
c.gridwidth=1;
c.anchor=GridBagConstraints.WEST;
password.setEchoChar('Q');
password.addActionListener(okAction);
outPanel.add(password,c);
c.gridx=0;
c.gridy=3;
c.gridwidth=3;
c.gridheight=1;
c.anchor=GridBagConstraints.CENTER;
JPanel innerPanel=new JPanel();
innerPanel.setBackground(new Color(187,242,24));
innerPanel.add(new JButton(okAction));
innerPanel.add(new JButton(clearAction));
innerPanel.add(new JButton(cancelAction));
outPanel.add(innerPanel,c);
c.gridx=0;
c.gridy=4;
c.gridwidth=3;
c.gridheight=1;
c.anchor=GridBagConstraints.CENTER;
outPanel.add(icon,c);
add(outPanel,"Center");
author.setFont(new Font("Monospaced", Font.PLAIN,15));
author.setForeground(Color.red);
add(author,"South");
}
public void connDB()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(null,"数据库加载失败!");
}
try
{
con = DriverManager.getConnection("jdbc:odbc:warehouse");
st = con.createStatement();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"数据库连接失败!");
}
}
public void closeDB1()
{
try
{
st.close();
con.close();
}
catch(SQLException e )
{
JOptionPane.showMessageDialog(null,"数据库关闭失败!");
}
}
AbstractAction okAction=new AbstractAction()//各种事件处理机制
{
{
putValue(NAME,"Login");//按钮的文本
putValue(MNEMONIC_KEY,new Integer('L'));//按钮的助记符
putValue(SHORT_DESCRIPTION,"填写正确的用户名和密码后登陆");
}
public void actionPerformed(ActionEvent evt)
{
String user_name=username.getText();
String pass_word=password.getText();
boolean suc=false;
if(user_name.length()==0)
{
JOptionPane.showMessageDialog(null,"请输入用户名!");
username.requestFocus();
}
else
{
connDB();
String query="select * from user_info where username='"+user_name+"' and password='"+pass_word+"';";
try
{
rs=st.executeQuery(query);
while(rs.next())
{
suc=true;
}
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,"数据库操作失败!");
}
if(suc==true)
{
JOptionPane.showMessageDialog(null,"恭喜,登陆成功!");
tabs.setEnabledAt(1,true);
tabs.setEnabledAt(2,true);
tabs.setEnabledAt(3,true);
tabs.setEnabledAt(4,true);
tabs.setEnabledAt(5,true);
tabs.setEnabledAt(6,true);
tabs.setEnabledAt(7,true);
username.setText("");
password.setText("");
}
else
{
JOptionPane.showMessageDialog(null,"用户名和密码不一致,请核实!");
username.setText("");
password.setText("");
username.requestFocus();//获得焦点
}
closeDB1();
}
}
};
AbstractAction clearAction=new AbstractAction()
{
{
putValue(NAME,"Reset");
putValue(MNEMONIC_KEY,new Integer('R'));
putValue(SHORT_DESCRIPTION,"重新填写");
}
public void actionPerformed(ActionEvent evt)
{
username.setText("");
password.setText("");
username.requestFocus();//获得焦点
}
};
AbstractAction cancelAction=new AbstractAction()
{
{
putValue(NAME,"Cancel");
putValue(MNEMONIC_KEY,new Integer('C'));
putValue(SHORT_DESCRIPTION,"退出系统");
}
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
};
private JTabbedPane tabs=null;
private JLabel system=new JLabel("仓 库 管 理 系 统",SwingConstants.CENTER);
private JLabel register=new JLabel("User Login In",SwingConstants.LEFT);
private JLabel usernameLabel=new JLabel("User name:",SwingConstants.RIGHT);
private JTextField username=new JTextField(20);
private JLabel passwordLabel=new JLabel("Password:",SwingConstants.RIGHT);
private JPasswordField password=new JPasswordField(20);
private JLabel icon=new JLabel(new ImageIcon(getClass().getResource("image/3.gif")),SwingConstants.CENTER);
private JLabel author=new JLabel("指导老师:侯有利 学生:周晓青",SwingConstants.CENTER);
Connection con=null;
ResultSet rs=null;
Statement st=null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -