?? 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 + -