案例:企业进销存系统
抽象类:声明抽象类:abstract + class +类名 。同时必须声明抽象方法:abstract +void +方法名();子类:继承抽象类的子类必须重写抽象方法。
package com.lovoinfo.ui;
/**
/*登录器与主窗口分段隐藏
*/
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
/*登录窗口
@SuppressWarnings("serial")
public class LoginFrame extends JDialog {
private JLabel uidLabel, pwdLabel;
private JTextField uidField;
private JPasswordField pwdField;
private JButton loginButton, cancelButton;
private List userList = new ArrayList();//用户容器
public LoginFrame() {
this.setTitle("用户登录");
this.setSize(300, 180);
this.setResizable(false);
this.setLocationRelativeTo(null);
// this.setDefaultCloseOperation(HIDE_ON_CLOSE);//隐藏窗口
initComponents();
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
private boolean checkLogin(String username, String password) {
for(User user : userList) {
if(user.getUsername().equals(username)) {
return user.getPassword().equals(password);
}
}
return false;
}
private void initComponents() {
userList.add(new User("admin", "123456"));
userList.add(new User("hellokitty", "1qaz2wsx"));
userList.add(new User("jack", "123123"));
uidLabel = new JLabel("用户名: ");
pwdLabel = new JLabel("密码: ");
uidField = new JTextField();
pwdField = new JPasswordField();
loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = uidField.getText().trim();
String password = new String(pwdField.getPassword());
if(checkLogin(username, password)) {
LoginFrame.this.setVisible(false);
new MainFrame(LoginFrame.this).setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误", 0);
}
}
});
cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setLayout(null);
uidLabel.setBounds(40, 20, 50, 30);
pwdLabel.setBounds(40, 60, 50, 30);
uidField.setBounds(100, 25, 150, 20);
pwdField.setBounds(100, 65, 150, 20);
loginButton.setBounds(80, 100, 60, 30);
cancelButton.setBounds(160, 100, 60, 30);
this.add(uidLabel);
this.add(pwdLabel);
this.add(uidField);
this.add(pwdField);
this.add(loginButton);
this.add(cancelButton);
}
}
用户类
package com.lovoinfo.ui;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
主窗口
package com.lovoinfo.ui;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.lovoinfo.biz.impl.UserServiceImpl;
import com.lovoinfo.domain.User;
/**
* 注册窗口(单例类)
* @author jackfrued
*
*/
@SuppressWarnings("serial")
public class RegInternalFrame extends JInternalFrame {
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == regButton) {
String username = uidField.getText().trim();
String password = new String(pwdField.getPassword());
String email = emailField.getText().trim();
User user = new User(username, password, email);
user.setUserService(new UserServiceImpl());
if(user.register()) {
JOptionPane.showMessageDialog(null, "注册成功!");
reset();
RegInternalFrame.this.setVisible(false);
}
else {
JOptionPane.showMessageDialog(null, "注册失败!", "错误提示", 0);
}
}
else if(e.getSource() == resetButton) {
reset();
}
}
}
private static Font defaultFont = new Font("微软雅黑", Font.PLAIN, 14);
private String[] labelNames = {"用户名: ", "密码: ", "确认密码: ", "电子邮箱: "};
private JLabel[] labels = new JLabel[4];
private JTextField uidField, emailField;
private JPasswordField pwdField, rePwdField;
private JButton regButton, resetButton;
private RegInternalFrame() {
super("用户注册", false, true, false, false);
this.setSize(270, 300);
this.setLocation(50, 50);
this.setVisible(true);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
initComponents();
}
private static RegInternalFrame instance = new RegInternalFrame();
public static RegInternalFrame getInstance() {
return instance;
}
private void initComponents() {
this.setLayout(null);
for(int i = 0; i < labels.length; i++) {
labels[i] = new JLabel(labelNames[i]);
labels[i].setBounds(20, 30 + 48 * i, 70, 24);
this.add(labels[i]);
}
uidField = new JTextField();
uidField.setBounds(100, 30, 120, 24);
this.add(uidField);
pwdField = new JPasswordField();
pwdField.setBounds(100, 78, 120, 24);
this.add(pwdField);
rePwdField = new JPasswordField();
rePwdField.setBounds(100, 126, 120, 24);
this.add(rePwdField);
emailField = new JTextField();
emailField.setBounds(100, 174, 150, 24);
this.add(emailField);
ActionListener handler = new ButtonHandler();
regButton = new JButton("注册");
regButton.setBounds(60, 230, 70, 24);
regButton.addActionListener(handler);
this.add(regButton);
resetButton = new JButton("重置");
resetButton.setBounds(150, 230, 70, 24);
resetButton.addActionListener(handler);
this.add(resetButton);
// 为窗口上的所有控件设置统一的字体
for(Component c : this.getContentPane().getComponents()) {
c.setFont(defaultFont);
}
}
private void reset() {
// 为窗口上的所有控件设置统一的字体
for(Component c : RegInternalFrame.this.getContentPane().getComponents()) {
if(c instanceof JTextField) {
((JTextField) c).setText("");
}
else if(c instanceof JPasswordField) {
((JPasswordField) c).setText("");
}
}
}
}
package com.lovoinfo.ui;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class SysManPanel extends JPanel {
private class MouseHandler extends MouseAdapter {
private int index;
public MouseHandler(int index) {
this.index = index;
}
@Override
public void mouseEntered(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonRollImageNames[index]));
}
@Override
public void mouseExited(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonImageNames[index]));
}
@Override
public void mousePressed(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonDownImageNames[index]));
}
@Override
public void mouseReleased(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonRollImageNames[index]));
}
@Override
public void mouseClicked(MouseEvent e) {
RegInternalFrame frame = RegInternalFrame.getInstance();
if(mainFrame.containsInternalFrame(frame)) {
mainFrame.showInternalFrame(frame);
}
else {
mainFrame.addInternalFrame(frame);
}
}
}
private static String[] buttonImageNames = {
"操作员管理.png", "更改密码.png", "权限管理.png"
};
private static String[] buttonRollImageNames = {
"操作员管理_roll.png", "更改密码_roll.png", "权限管理_roll.png"
};
private static String[] buttonDownImageNames = {
"操作员管理_down.png", "更改密码_down.png", "权限管理_down.png"
};
private JLabel[] labels = new JLabel[3];
private MainFrame mainFrame;
public SysManPanel(MainFrame mainFrame) {
this.mainFrame = mainFrame;
this.setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i = 0; i < labels.length; i++) {
labels[i] = new JLabel(new ImageIcon(buttonImageNames[i]));
labels[i].addMouseListener(new MouseHandler(i));
this.add(labels[i]);
}
}
}
package com.lovoinfo.ui;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
@SuppressWarnings("serial")
public class MyDesktopPane extends JDesktopPane {
private static ImageIcon bgImage = new ImageIcon("welcome.jpg");
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage.getImage(), 0, 0, this.getWidth(), this.getHeight(), null);
}
}
用户类及服务类
package com.lovoinfo.domain;
import com.lovoinfo.biz.UserService;
public class User {
private String username;
private String password;
private String email;
private UserService userService;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public boolean login() {
return userService.doLogin(this);
}
public boolean register() {
return userService.doRegister(this);
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.lovoinfo.biz;
import com.lovoinfo.domain.User;
/**
* 用户业务逻辑接口
* @author jackfrued
*
*/
public interface UserService {
/**
* 登录
* @param user 用户对象
* @return 登录成功返回true否则返回false
*/
public boolean doLogin(User user);
/**
* 注册
* @param user 用户对象
* @return 注册成功返回true否则返回false
*/
public boolean doRegister(User user);
}
package com.lovoinfo.biz.impl;
import java.util.ArrayList;
import java.util.List;
import com.lovoinfo.biz.UserService;
import com.lovoinfo.domain.User;
/**
* 用户业务逻辑实现类
* @author jackfrued
*
*/
public class UserServiceImpl implements UserService {
private static List userList = new ArrayList();
static {
userList.add(new User("admin", "123456"));
userList.add(new User("jack", "abcdefg"));
}
public UserServiceImpl() {
}
@Override
public boolean doLogin(User user) {
User temp = findUserByUsername(user.getUsername());
if(temp != null) {
return temp.getPassword().equals(user.getPassword());
}
return false;
}
@Override
public boolean doRegister(User user) {
User temp = findUserByUsername(user.getUsername());
if(temp == null) {
userList.add(user);
return true;
}
return false;
}
// 根据用户名查找用户
private User findUserByUsername(String username) {
for(User temp : userList) {
if(temp.getUsername().equals(username)) {
return temp;
}
}
return null;
}
}