一个登陆界面,
我知道的界面设计有2种,一种为纯代码设计的,一种为GUI窗体设计的。
本人认为用GUI窗体设计比较简单方便。
本文以图书馆管理系统为例,介绍图书馆管理系统的登陆界面。
先用GUI设计如图界面:
生成代码自动如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.derun.iframe;
//import com.derun.dao.User;
//import com.derun.dao.UserDAO;
//import com.derun.xuejava.Library;
//import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class LoginJFrame extends javax.swing.JFrame {
/**
* Creates new form LoginJFrame
*/
public LoginJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
password = new javax.swing.JPasswordField();
username = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
btn_login = new javax.swing.JButton();
btn_reset = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setPreferredSize(new java.awt.Dimension(279, 150));
jLabel2.setText("用户名:");
jLabel3.setText("密 码:");
username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usernameActionPerformed(evt);
}
});
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/login.jpg"))); // NOI18N
jLabel1.setPreferredSize(new java.awt.Dimension(200, 60));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
.addComponent(password))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(44, 44, 44))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel2.setPreferredSize(new java.awt.Dimension(277, 60));
btn_login.setText("登陆");
btn_login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_loginActionPerformed(evt);
}
});
btn_reset.setText("重置");
btn_reset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_resetActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap(68, Short.MAX_VALUE)
.addComponent(btn_login)
.addGap(42, 42, 42)
.addComponent(btn_reset)
.addGap(55, 55, 55))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_login)
.addComponent(btn_reset))
.addContainerGap(27, Short.MAX_VALUE))
);
getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LoginJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LoginJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_login;
private javax.swing.JButton btn_reset;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration
}
然后双击重置按钮:可以对此按钮进行编程:
private void btn_resetActionPerformed(java.awt.event.ActionEvent evt) { //用户单击了“重置”按钮,则清空用户名和密码文本框 username.setText(""); password.setText(""); }
而对于登陆按钮 如果是普通的不用连接数据库的设计同样先双击“登陆”按钮出现编程代码地方:private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) { String user_name = username.getText().trim(); String pass_word = new String(password.getPassword());
if ('admin'.equals(user_name)&&'123456'.equals(pass_word)) { Library ly = new Library(); ly.setVisible(true); this.setVisible(false); } }
如果是连接数据库,首先实现好数据库部分,然后可以用DAO作为连接实现。