📄 serverlogin.java
字号:
package server;
import javax.swing.*;
import java.sql.Connection;
import java.sql.*;
public class ServerLogin extends javax.swing.JFrame {
Connection connection=null;
PreparedStatement statement=null;
private int number;
public ServerLogin() {
initComponents();
this.setLocation(450,180);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:Account");
String sql="select* from Account";
statement=connection.prepareStatement(sql);
number=0;
}
catch(Exception e)
{
System.out.println("ServerLogin initial"+e.toString());
}
}
private void Validate() //用户名和密码验证
{
number++;
String account=jTextField_Name.getText();
String code=jPasswordField.getText();
if(account.equals("123456")&&code.equals("123456"))
{
Succuss() ;
}
else{
if(number==3)
{
jButton_Login.setEnabled(false);
JOptionPane.showMessageDialog(this,"连续三次服务器名与密码不匹配,登陆失败");
}
else{
jTextField_Name.setText("");
jPasswordField.setText("");
}
}
}
private void Succuss() //登陆成功
{
try
{
if(connection!=null)
connection.close();
if(statement!=null)
statement.close();
}
catch(Exception e)
{
System.out.println("ServerLogin Success"+e.toString());
}
JOptionPane.showMessageDialog(this,"服务器登陆");
this.dispose();
new Server().setVisible(true);
}
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton_Login = new javax.swing.JButton();
jButton_Cancel = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jPasswordField = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jTextField_Name = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle("\u670d\u52a1\u5668\u767b\u9646");
jPanel1.setBackground(new java.awt.Color(204, 204, 204));
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jButton_Login.setText("\u767b\u9646");
jButton_Login.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton_LoginMouseClicked(evt);
}
});
jButton_Cancel.setText("\u9000\u51fa");
jButton_Cancel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton_CancelMouseClicked(evt);
}
});
jLabel2.setText("\u5bc6 \u7801\uff1a");
jPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPasswordFieldKeyPressed(evt);
}
});
jLabel1.setText("\u670d\u52a1\u5668\u540d\uff1a");
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(27, 27, 27)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.add(18, 18, 18)
.add(jButton_Login)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 92, Short.MAX_VALUE)
.add(jButton_Cancel)
.add(42, 42, 42))
.add(jPanel1Layout.createSequentialGroup()
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(jLabel2))
.add(27, 27, 27)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPasswordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
.add(jTextField_Name, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
.add(33, 33, 33))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
.add(34, 34, 34)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(jTextField_Name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 53, Short.MAX_VALUE)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel2)
.add(jPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(49, 49, 49)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton_Cancel)
.add(jButton_Login))
.addContainerGap())
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jPasswordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldKeyPressed
// TODO 将在此处添加您的处理代码:
String account=jTextField_Name.getText();
String code=jPasswordField.getText();
int key=evt.getKeyCode();
if(key==evt.VK_ENTER)
{
if(account.equals("")||code.equals(""))
{
JOptionPane.showMessageDialog(this,"输入错误,请重新输入");
jTextField_Name.setText("");
jPasswordField.setText("");
}
else
{
Validate();
}
}
}//GEN-LAST:event_jPasswordFieldKeyPressed
private void jButton_CancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_CancelMouseClicked
try
{
if(connection!=null)
connection.close();
if(statement!=null)
statement.close();
}
catch(Exception e)
{
System.out.println("ServerLogin Cancel"+e.toString());
}
this.dispose();
}//GEN-LAST:event_jButton_CancelMouseClicked
private void jButton_LoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_LoginMouseClicked
String account=jTextField_Name.getText();
String code=jPasswordField.getText();
if(account.equals("")||code.equals(""))
{
JOptionPane.showMessageDialog(this,"输入错误,请重新输入");
jTextField_Name.setText("");
jPasswordField.setText("");
}
else
{
Validate();
}
}//GEN-LAST:event_jButton_LoginMouseClicked
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JButton jButton_Cancel;
private javax.swing.JButton jButton_Login;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField jPasswordField;
private javax.swing.JTextField jTextField_Name;
// 变量声明结束//GEN-END:variables
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ServerLogin().setVisible(true);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -