📄 loginframe.java
字号:
//
//以下是布局GUI的类
//
import java.sql.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
class LoginFrame extends JFrame
{
private JButton loginButton ;
private Connection loginconnection;
private Statement loginstatement;
private ResultSet loginresultSet;
private JTextField userNameTextField;
private JPasswordField passwordField;
private int login = 0 ;
public LoginFrame()
{
//loginconnection = connection ;
setTitle("登录窗口");
setSize(250,150);
setResizable(false);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setLocation(350,200);
GridBagLayout layout = new GridBagLayout();
Container contents = getContentPane();
contents.setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();
userNameTextField = new JTextField(); //needed below
userNameTextField.setPreferredSize(new Dimension(120, 25));
// user name label
JLabel userNameLabel = new JLabel();
//userNameLabel.setDisplayedMnemonic("用户名");
userNameLabel.setLabelFor(userNameTextField);
userNameLabel.setText("用户名");
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(userNameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(userNameTextField, constraints);
passwordField = new JPasswordField(); // needed below
passwordField.setPreferredSize(new Dimension(120, 25));
// password label
JLabel passwordLabel = new JLabel();
passwordLabel.setText("口 令");
passwordLabel.setLabelFor(passwordField);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(passwordLabel, constraints);
// password field
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(passwordField, constraints);
JPanel buttonPanel = createButtonPanel(); // sets global loginButton
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 3;
constraints.gridheight = 1;
contents.add(buttonPanel, constraints);
}
private JPanel createButtonPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, 0));
// login button (global variable)
loginButton = new JButton();
loginButton.setText("确 定");
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
logindispose();
if(login == 1)
{
JFrame f = new MainFrame();
f.show();
dispose();
}
}
});
panel.add(loginButton);
// space
panel.add(Box.createRigidArea(new Dimension(5,0)));
// cancel button
JButton cancelButton = new JButton();
cancelButton.setText("取 消");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dispose();
System.exit(0);
}
});
panel.add(cancelButton);
// space
panel.add(Box.createRigidArea(new Dimension(5,0)));
// help button
JButton helpButton = new JButton();
helpButton.setText("About");
helpButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event) {
initAboutDialog();
}
});
panel.add(helpButton);
Vector buttons = new Vector(3);
buttons.add(cancelButton);
buttons.add(helpButton);
buttons.add(loginButton);
equalizeComponentSizes(buttons);
buttons.removeAllElements(); // simplify gc
return panel;
} // createButtonPanel()
private void equalizeComponentSizes(java.util.List components)
{
// Get the largest width and height
int i = 0;
Dimension maxPreferred = new Dimension(0,0);
JComponent oneComponent = null;
Dimension thisPreferred = null;
for (i = 0; i < components.size(); ++i) {
oneComponent = (JComponent)components.get(i);
thisPreferred = oneComponent.getPreferredSize();
maxPreferred.width =
Math.max(maxPreferred.width, (int)thisPreferred.getWidth());
maxPreferred.height =
Math.max(maxPreferred.height, (int)thisPreferred.getHeight());
}
// reset preferred and maximum size since BoxLayout takes both
// into account
for (i = 0; i < components.size(); ++i) {
oneComponent = (JComponent)components.get(i);
oneComponent.setPreferredSize((Dimension)maxPreferred.clone());
oneComponent.setMaximumSize((Dimension)maxPreferred.clone());
}
} // equalizeComponentSizes()
private void windowAction()
{
JFrame MainFrame = new JFrame();
MainFrame.setSize(new Dimension(550,350));
MainFrame.show();
setVisible(false);
dispose();
}
private void initAboutDialog()
{
JDialog dialog = new About(this,true);
dialog.show();
}
private void logindispose()
{
String url = "jdbc:odbc:VipQuery";
String username = "";
String password = "";
//加载驱动程序以连接数据库
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
loginconnection = DriverManager.getConnection( url, username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex )
{
System.err.println("装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
//捕获连接数据库异常
catch ( SQLException sqlex )
{
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
try
{
String loginquery;
String loginusename = userNameTextField.getText();
String loginpassword = passwordField.getText();
loginquery = "select * from UESR_MANAGER where (用户名='"+loginusename +
"' and 用户口令 = '"+loginpassword+"')";
loginstatement = loginconnection.createStatement();
loginresultSet = loginstatement.executeQuery( loginquery );
boolean Records = loginresultSet.next();
if ( ! Records )
{
JOptionPane.showMessageDialog( this,"登录失败" );
return;
}
else
{
login = 1 ;
}
loginconnection.close();
}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -