这是我模仿QQ2015版界面,实现的基本功能有登陆验证,重置等,当然直接复制代码运行是不一样的,还要注意自己插入自己的图片。
结果截图如下所示:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;public class QQ2015 extends JFrame implements ActionListener {
JFrame loginFrame;
// 复选框
private JCheckBox jc1;
private JCheckBox jc2;
// 用户名
private JTextField username;
// 密码
private JPasswordField password;
// 错误消息提示
JLabel mes;
// 创建一个HashMap,用以保存帐号和密码
HashMap<String, String> users = new HashMap<String, String>(); // 主函数入口
public static void main(String[] args) {
QQ2015 qq2015 = new QQ2015();
qq2015.showLoginFrame();
} // 构造方法
public QQ2015() {
users.put("123456", "123456");
users.put("302795036", "123456");
users.put("123", "123456");
} // 创建登陆界面方法
public void showLoginFrame() {
loginFrame = new JFrame();
// 设置大小,位置,标题
loginFrame.setSize(410, 380);
loginFrame.setTitle("修衡-QQ2015");
loginFrame.setLocationRelativeTo(null);
// 创建边界布局对象
loginFrame.setLayout(new BorderLayout());
JPanel panelTop = new JPanel(new FlowLayout());
JPanel panelCenter = new JPanel(new GridLayout(3, 1));
JPanel panelFooter = new JPanel(new GridLayout(2, 1));
JPanel panelFooter1 = new JPanel(new FlowLayout());
JPanel panelFooter2 = new JPanel(new FlowLayout());
panelFooter.add(panelFooter1);
panelFooter.add(panelFooter2);
JPanel panelLeft = new JPanel(new GridLayout(1, 1));
JPanel panelRight = new JPanel(new GridLayout(3, 1));
loginFrame.add("North", panelTop);
loginFrame.add("Center", panelCenter);
loginFrame.add("South", panelFooter);
loginFrame.add("West", panelLeft);
loginFrame.add("East", panelRight);
(new BorderLayout()).setHgap(50);
// 插入图片
Image im = new ImageIcon("imgs/qq.png").getImage();
im = im.getScaledInstance(-1, 90, DO_NOTHING_ON_CLOSE);
loginFrame.setIconImage(im);
JLabel tl = new JLabel(new ImageIcon("imgs/top.png"));
panelTop.add(tl);
// 可以设置panelTop背景颜色
// panelTop.setBackground(Color.YELLOW);
JLabel ll = new JLabel(new ImageIcon("imgs/left.png"));
panelLeft.add(ll);
// 创建注册账号和找回密码标签
JLabel reg_name = new JLabel(" 注册账号 ");
JLabel find_password = new JLabel(" 找回密码 ");
reg_name.setForeground(Color.BLUE);
find_password.setForeground(Color.BLUE);
// 创建帐号和密码输入框
username = new JTextField(18);
password = new JPasswordField(18);
// 复选框
jc1 = new JCheckBox("记住密码 ");
jc2 = new JCheckBox("自动登录");
JLabel nul1 = new JLabel(" ");
JLabel nul2 = new JLabel(" ");
// 错误消息提示标签
mes = new JLabel();
mes.setForeground(Color.RED);
// 部分布局
JPanel pancener = new JPanel(new FlowLayout());
pancener.add(jc1);
pancener.add(nul1);
pancener.add(jc2);
// 创建登陆,重置按钮
JButton button_reset = new JButton(" 重 置 ");
JButton button_login = new JButton(" 登 陆 ");
button_login.setBackground(Color.pink);
// 窗体大小不能改变
loginFrame.setResizable(false);
// 设置窗体可见
loginFrame.setVisible(true);
// 将文本输入框,按钮,事件监听对象等添加
panelCenter.add(username);
panelCenter.add(password);
panelCenter.add(pancener);
panelRight.add(reg_name);
panelRight.add(find_password);
panelFooter1.add(button_reset);
panelFooter1.add(nul2);
panelFooter1.add(button_login);
panelFooter2.add(mes);
button_reset.addActionListener(this);
button_login.addActionListener(this);
// reg_name.addActionListener(this);
} public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
boolean f = false;
String pw = null;
// 重置事件
if (str.equals(" 重 置 ")) {
username.setText("");
password.setText("");
mes.setText("");
}
// 登录事件
if (str.equals(" 登 陆 ")) {
mes.setText("");
Set<Map.Entry<String, String>> set = users.entrySet();
for (Map.Entry<String, String> me : set) {
if (username.getText().trim().equals(me.getKey())) {
f = true;
pw = me.getValue();
break;
}
}
if (f) {
// if (password.getPassword().equals(pw)) {
if (password.getText().trim().equals(pw)) {
JOptionPane.showMessageDialog(null, "登录成功");
} else {
mes.setText("温馨提示:密码为空或错误,请重新输入!");
}
} else {
mes.setText("温馨提示:帐号为空或错误,请重新输入!");
}
}
}
}