创建数据库的语句格式为:
create database 数据库名;
使用数据库的语句格式为:
use 数据库名;
然后在创建数据表
创建数据表的语句格式为:
CREATE TABLE IF NOT EXISTS 表单名字 (
number VARCHAR(30),
username VARCHAR(30),
password VARCHAR(30),
PRIMARY KEY ( number )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
这样子就建立了一个表单
然后我们在插入数据
数据表中插入数据的语句格式为:
INSERT INTO 表单名字 ( number,username,password )
VALUES( 1, 123456,123456);
这时候我们在
得到表单信息的语句格式:
select * from 表单名字;
我们就可以得到一个表单数据
具体实例如下
使用的工具类:
1.界面居中的工具类:
package qqtools;
import java.awt.Component;
import java.awt.Toolkit;
public class GUItools {
// JAVA提供的GUI默认工具类对象
static Toolkit kit = Toolkit.getDefaultToolkit();
// 将指定组件屏幕居中
public static void center(Component c) {
int x = (kit.getScreenSize().width - c.getWidth()) / 2;
int y = (kit.getScreenSize().height - c.getHeight()) / 2;
c.setLocation(x, y);
}
}
2.连接数据库的工具类
package qqtools;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class JDBCUTils {
//连接数据库
public static java.sql.Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
String username = "root";//数据库用户名
String password = "wankun.20011126";//数据库密码
java.sql.Connection conn = DriverManager.getConnection(url, username, password);//获取数据库连接
return conn;
}
//关闭数据库连接,释放资源
public static void release(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
rs = null;
}
release(stmt, conn);
}
}
3.生成验证码的工具类:
在这里插入代码片package qqtools;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class VerificationTools extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
static StringBuilder sb;
public void paint(Graphics g) {
int width = 160; // 定义验证码图片的宽度
int height = 40; // 定义验证码图片的高度
g.setColor(Color.LIGHT_GRAY); // 设置上下文颜色
g.fillRect(0, 0, width, height); // 填充验证码背景
g.setColor(Color.BLACK); // 设置上下文颜色
g.drawRect(0, 0, width - 1, height - 1); // 绘制边框
// 绘制干扰点
Random r = new Random();
for (int i = 0; i < 100; i++) {
int x = r.nextInt(width) - 2;
int y = r.nextInt(height) - 2;
g.drawOval(x, y, 2, 2);
}
g.setFont(new Font("黑体", Font.BOLD, 30)); // 设置验证码字体
g.setColor(Color.BLUE); // 设置验证码颜色
// 产生随机验证码
char[] chars = ("0123456789abcdefghijkmnopqrstuvwxyzABCDEFG" + "HIJKLMNPQRSTUVWXYZ").toCharArray();
sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int pos = r.nextInt(chars.length);
char c = chars[pos];
sb.append(c + " ");
}
g.drawString(sb.toString(), 20, 30); // 写入验证码
}
public static String string() {
return sb.toString().replace(" ", "");
}
}
登录界面主要使用的有
1.LoginFrame.java
package qq.view;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import qq.Component.ButtomPanel;
import qq.Component.TopPanel;
import qq.controller.RegisterFrameController;
import qq.service.LoginService;
import qqtools.GUItools;
@SuppressWarnings("serial")
public abstract class LoginFrame extends JFrame {
public LoginFrame() {
this.init();// 初始化操作
this.addComponent();// 添加组件
this.addListener();// 添加监听器
}
private void init() {
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
this.setSize(532, 405);// 窗体大小与位置
this.setUndecorated(true);// 设置JFrame窗口边框不显示
this.setResizable(false); // 禁止改变窗口大小
GUItools.center(this);// 设置窗口在屏幕上的位置
}
private void addComponent() {
this.add(new TopPanel());// 上半部分的布局
this.add(new ButtomPanel());// 下半部分的布局
}
// 添加监听器
private void addListener() {
TopPanel.getExitbutton().addActionListener(event -> this.dispose());
TopPanel.getMinimizebutton().addActionListener(event -> this.setExtendedState(JFrame.ICONIFIED));
TopPanel.getSetupbutton().addActionListener(event -> this.setExtendedState(JFrame.MAXIMIZED_BOTH));
ButtomPanel.getJb().addActionListener(event -> {
try {
LoginService.Login();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
});
ButtomPanel.getRegister().addActionListener(event ->{
new RegisterFrameController().setVisible(true);
});
}
}
package qq.Component;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class TopPanel extends Panel {
// 上半部分的背景图
private static ImageIcon image = new ImageIcon("QQ.jpg");
private static ImageIcon image1 = new ImageIcon("qqtx.jpg");
private static JLabel background = new JLabel(image);
private static JLabel backgrounds = new JLabel(image1);
// 关闭按钮
private static JButton exitbutton = new JButton();
private static ImageIcon exitsquare = new ImageIcon("exitbutton.jpg");
// 最小化按钮
private static JButton minimizebutton = new JButton();
private static ImageIcon minimizesquare = new ImageIcon("minimizebutton.jpg");
// 设置按钮
private static JButton setupbutton = new JButton();
private static ImageIcon setupsquare = new ImageIcon("setupbutton.jpg");
public TopPanel() {
this.init();// 初始化操作
this.addComponent();// 添加组件
}
private void init() {
this.setLayout(null);
this.setBackground(new Color(255, 255, 255));// 设置背景颜色
this.setBounds(0, 0, image.getIconWidth() + image1.getIconWidth(),
image.getIconHeight() + image1.getIconHeight());// 设置panel的位置
background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置背景图片的位置及尺寸
this.add(background);
backgrounds.setBounds(-5, image.getIconHeight(), image1.getIconWidth(), image1.getIconHeight());// 设置背景图片的位置及尺寸
this.add(backgrounds);
}
private void addComponent() {
// 退出按钮
getExitbutton().setIcon(exitsquare);// 把图片放到按钮上
getExitbutton().setBounds(532 - exitsquare.getIconWidth(), 0, exitsquare.getIconWidth(),
exitsquare.getIconHeight());// 设置按钮的位置和大小
getExitbutton().setBorderPainted(false);// 取消按钮边框效果
this.add(getExitbutton());// 将按钮添加到panel中
// 最小化按钮
getMinimizebutton().setIcon(minimizesquare);
getMinimizebutton().setBounds(532 - exitsquare.getIconWidth() - minimizesquare.getIconWidth() - 6, 0,
minimizesquare.getIconWidth() - 1, minimizesquare.getIconHeight());
getMinimizebutton().setBorderPainted(false);
this.add(getMinimizebutton());
// 设置按钮
getSetupbutton().setIcon(setupsquare);
getSetupbutton().setBounds(
532 - exitsquare.getIconWidth() - minimizesquare.getIconWidth() - setupsquare.getIconWidth(), 0,
setupsquare.getIconWidth(), setupsquare.getIconHeight());
getSetupbutton().setBorderPainted(false);
this.add(getSetupbutton());
}
public static JButton getExitbutton() {
return exitbutton;
}
public void setExitbutton(JButton exitbutton) {
TopPanel.exitbutton = exitbutton;
}
public static JButton getMinimizebutton() {
return minimizebutton;
}
public void setMinimizebutton(JButton minimizebutton) {
TopPanel.minimizebutton = minimizebutton;
}
public static JButton getSetupbutton() {
return setupbutton;
}
public static void setSetupbutton(JButton setupbutton) {
TopPanel.setupbutton = setupbutton;
}
}
package qq.Component;
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ButtomPanel extends Panel {
private static String str[] = { "1169580201", "1519179206", "8436115200" };
private static JComboBox<Object> centerjco = new JComboBox<Object>(str);
private static JPasswordField jpaCenter = new JPasswordField();
private static JCheckBox jch2 = new JCheckBox("自动登录");
private static JCheckBox jch1 = new JCheckBox("记住密码");
private static JLabel label = new JLabel("找回密码");
private static ImageIcon image1 = new ImageIcon("login.jpg");
private static JButton jb = new JButton(image1);
private static JButton register = new JButton("注册账号");
public ButtomPanel() {
this.init();// 初始化操作
this.addComponent();// 添加组件
}
public void init() {
this.setLayout(null);// 设置panel的布局格式
this.setBackground(new Color(255, 255, 255));// 设置panel的背景颜色
this.setBounds(16, 320, 400, 400);// 设置panel的位置和大小
}
public void addComponent() {
// 添加账号下拉框
getCenterjco().setEditable(true);
getCenterjco().setBounds(118, 200, 297, 43);
getCenterjco().setFont(new Font("Calibri ", 15, 27)); // 设置下拉框内容字体
this.add(getCenterjco());
// 创建一个JPasswordField密码框组件
getJpaCenter().setFont(new Font("Calibri ", 15, 27)); // 设置密码框字体
getJpaCenter().setBounds(118, 249, 297, 43);// 设置密码框的位置和大小
this.add(getJpaCenter());
// 创建自动登录和记住密码多选框组件
jch2.setBackground(new Color(255, 255, 255));
jch2.setFocusPainted(false);
jch2.setFont(new Font("宋体", 0, 12));
jch2.setBounds(118, 300, 80, 20);
this.add(jch2);
jch1.setBackground(new Color(255, 255, 255));
jch1.setFocusPainted(false);// 设置选中时不显示边框
jch1.setFont(new Font("宋体", 0, 13));
jch1.setBounds(240, 300, 80, 20);
this.add(jch1);
// 添加注册账号超链接
label.setFont(new Font("宋体", 0, 13));
label.setBounds(363, 300, 80, 20);
this.add(label);
// 添加登录按钮
getJb().setFont(new Font("宋体", 0, 13));
getJb().setBounds(118, 330, image1.getIconWidth(), image1.getIconHeight());
getJb().setFocusPainted(false);
getJb().setBorderPainted(false);
this.add(getJb());
// 添加注册账号超链接
register.setFont(new Font("宋体", 0, 13));
register.setBounds(0, 370, 100, 20);
register.setBackground(new Color(255, 255, 255));
register.setBorderPainted(false);
this.add(register);
}
public static JButton getJb() {
return jb;
}
public static void setJb(JButton jb) {
ButtomPanel.jb = jb;
}
public static JPasswordField getJpaCenter() {
return jpaCenter;
}
public static void setJpaCenter(JPasswordField jpaCenter) {
ButtomPanel.jpaCenter = jpaCenter;
}
public static JComboBox<Object> getCenterjco() {
return centerjco;
}
public static void setCenterjco(JComboBox<Object> centerjco) {
ButtomPanel.centerjco = centerjco;
}
public static JButton getRegister() {
return register;
}
public static void setRegister(JButton register) {
ButtomPanel.register = register;
}
}
package qq.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import qq.Component.ButtomPanel;
import qq.app.QQAPP;
import qq.controller.EnterFrameController;
import qq.controller.UnenterFrameController;
import qqtools.JDBCUTils;
public class LoginService {
public static void Login() throws ClassNotFoundException, SQLException {
char[] values = ButtomPanel.getJpaCenter().getPassword();
String password = new String(values);
String username = ButtomPanel.getCenterjco().getSelectedItem().toString();
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
ResultSet rs = null;
boolean flag = false;
conn = JDBCUTils.getConnection();//连接数据库
stmt = conn.createStatement();
String sql = "select * from QQ";//sql语句
rs = stmt.executeQuery(sql);//执行语句
while (rs.next()) {
if (password.equals(rs.getString("password")) && username.equals(rs.getString("username"))) {
flag = true;
break;
}
}
if (flag) {
System.out.println("登录成功");
QQAPP.getLoginFrame().dispose();
new EnterFrameController().setVisible(true);
} else {
System.out.print("登录失败");
new UnenterFrameController().setVisible(true);
}
}
}
package qq.view;
import java.awt.Color;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
import qq.Component.RegisterPanel;
import qq.service.RegisterService;
import qqtools.GUItools;
@SuppressWarnings("serial")
public class RegisterFrame extends JFrame {
public RegisterFrame() {
this.init();// 初始化操作
this.addComponent();// 添加组件
this.addListener();// 添加监听器
}
private void init() {
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
this.setSize(532, 405);// 窗体大小与位置
this.setUndecorated(true);// 设置JFrame窗口边框不显示
this.setResizable(false); // 禁止改变窗口大小
GUItools.center(this);// 设置窗口在屏幕上的位置
this.setBackground(new Color(255, 255, 255));// 设置背景颜色
}
private void addComponent() {
this.add(new RegisterPanel());
}
// 添加监听器
private void addListener() {
RegisterPanel.getExitbutton().addActionListener(event -> this.dispose());
RegisterPanel.getMinimizebutton().addActionListener(event -> this.setExtendedState(JFrame.ICONIFIED));
RegisterPanel.getSetupbutton().addActionListener(event -> {
try {
RegisterService.Register();
} catch (ClassNotFoundException | SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
});
}
}
package qq.Component;
import java.awt.Color;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import qqtools.VerificationTools;
@SuppressWarnings("serial")
public class RegisterPanel extends JPanel {
private static ImageIcon image = new ImageIcon("Register.jpg");
private static JLabel background = new JLabel(image);
private static JButton exitbutton = new JButton();
private static ImageIcon exitsquare = new ImageIcon("exitbutton2.jpg");
private static JButton minimizebutton = new JButton();
private static ImageIcon minimizesquare = new ImageIcon("minimizebutton2.jpg");
private static JLabel label1 = new JLabel("昵称:");
private static JTextField input = new JTextField(20);
private static JLabel label2 = new JLabel("密码:");
private static JPasswordField jpaCenter = new JPasswordField();
private static JLabel label3 = new JLabel("电话号码:");
private static JTextField phonenumber = new JTextField(11);
private static JLabel label4=new JLabel("验证码:");
private static JTextField Verification= new JTextField(11);
private static VerificationTools vt=new VerificationTools();
private static JButton setupbutton = new JButton();
private static ImageIcon setupsquare = new ImageIcon("register1(1).jpg");
public RegisterPanel() {
this.init();// 初始化操作
this.addComponent();// 添加组件
}
private void init() {
this.setLayout(null);
this.setBackground(new Color(255, 255, 255));// 设置背景颜色
this.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置panel的位置
background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置背景图片的位置及尺寸
this.add(background);
}
private void addComponent() {
getExitbutton().setIcon(exitsquare);// 把图片放到按钮上
getExitbutton().setBounds(532 - exitsquare.getIconWidth() + 5, 2, exitsquare.getIconWidth(),
exitsquare.getIconHeight());// 设置按钮的位置和大小
getExitbutton().setBorderPainted(false);// 取消按钮边框效果
this.add(getExitbutton());// 将按钮添加到panel中
getMinimizebutton().setIcon(minimizesquare);
getMinimizebutton().setBounds(532 - exitsquare.getIconWidth() - minimizesquare.getIconWidth() + 5, 1,
minimizesquare.getIconWidth(), minimizesquare.getIconHeight());
getMinimizebutton().setBorderPainted(false);
this.add(getMinimizebutton());
getSetupbutton().setIcon(setupsquare);
getSetupbutton().setBounds(120, 330, setupsquare.getIconWidth(), setupsquare.getIconHeight());
getSetupbutton().setBorderPainted(false);
this.add(getSetupbutton());
label1.setFont(new Font("Calibri ", 15, 27));
label1.setBounds(0, 40, 100, 100);
this.add(label1);
input.setFont(new Font("Calibri ", 15, 27));
input.setBounds(118, 70, 297, 43);
this.add(input);
label2.setFont(new Font("Calibri ", 15, 27));
label2.setBounds(0, 125, 70, 50);
this.add(label2);
getJpaCenter().setFont(new Font("Calibri ", 15, 27)); // 设置密码框字体
getJpaCenter().setBounds(118, 125, 297, 43);// 设置密码框的位置和大小
this.add(getJpaCenter());
label3.setFont(new Font("Calibri ", 15, 27));
label3.setBounds(0, 155, 170, 100);
this.add(label3);
phonenumber.setFont(new Font("Calibri ", 15, 27));
phonenumber.setBounds(118, 185, 297, 43);
this.add(phonenumber);
label4.setFont(new Font("Calibri ", 15, 27));
label4.setBounds(0, 250, 100, 50);
this.add(label4);
Verification.setFont(new Font("Calibri ", 15, 27));
Verification.setBounds(118, 250, 135, 43);
this.add(Verification);
vt.setBounds(263, 250, 150, 60);
this.add(vt);
}
public static JButton getSetupbutton() {
return setupbutton;
}
public static void setSetupbutton(JButton setupbutton) {
UnenterPanel.setSetupbutton(setupbutton);
}
public static ImageIcon getSetupsquare() {
return setupsquare;
}
public static void setSetupsquare(ImageIcon setupsquare) {
UnenterPanel.setSetupsquare(setupsquare);
}
public static JButton getMinimizebutton() {
return minimizebutton;
}
public static void setMinimizebutton(JButton minimizebutton) {
UnenterPanel.setMinimizebutton(minimizebutton);
}
public static ImageIcon getMinimizesquare() {
return minimizesquare;
}
public static void setMinimizesquare(ImageIcon minimizesquare) {
UnenterPanel.setMinimizesquare(minimizesquare);
}
public static ImageIcon getImage() {
return image;
}
public static void setImage(ImageIcon image) {
UnenterPanel.setImage(image);
}
public static JButton getExitbutton() {
return exitbutton;
}
public static void setExitbutton(JButton exitbutton) {
UnenterPanel.setExitbutton(exitbutton);
}
public static ImageIcon getExitsquare() {
return exitsquare;
}
public static void setExitsquare(ImageIcon exitsquare) {
UnenterPanel.setExitsquare(exitsquare);
}
public static JPasswordField getJpaCenter() {
return jpaCenter;
}
public static void setJpaCenter(JPasswordField jpaCenter) {
RegisterPanel.jpaCenter = jpaCenter;
}
public static void setBackground(JLabel background) {
RegisterPanel.background = background;
}
public static JLabel getLabel1() {
return label1;
}
public static void setLabel1(JLabel label1) {
RegisterPanel.label1 = label1;
}
public static JTextField getInput() {
return input;
}
public static void setInput(JTextField input) {
RegisterPanel.input = input;
}
public static JLabel getLabel2() {
return label2;
}
public static void setLabel2(JLabel label2) {
RegisterPanel.label2 = label2;
}
public static JLabel getLabel3() {
return label3;
}
public static void setLabel3(JLabel label3) {
RegisterPanel.label3 = label3;
}
public static JTextField getPhonenumber() {
return phonenumber;
}
public static void setPhonenumber(JTextField phonenumber) {
RegisterPanel.phonenumber = phonenumber;
}
public static JLabel getLabel4() {
return label4;
}
public static void setLabel4(JLabel label4) {
RegisterPanel.label4 = label4;
}
public static JTextField getVerification() {
return Verification;
}
public static void setVerification(JTextField verification) {
Verification = verification;
}
public static VerificationTools getVt() {
return vt;
}
public static void setVt(VerificationTools vt) {
RegisterPanel.vt = vt;
}
}
package qq.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import qq.Component.RegisterPanel;
import qqtools.JDBCUTils;
import qqtools.VerificationTools;
public class RegisterService {
@SuppressWarnings("unused")
public static void Register() throws ClassNotFoundException, SQLException {
char[] values = RegisterPanel.getJpaCenter().getPassword();
String password = new String(values);
String username = RegisterPanel.getInput().getText();
String phonenumber = RegisterPanel.getPhonenumber().getText();
String code = RegisterPanel.getVerification().getText();
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
ResultSet rs = null;
boolean flag = false;
conn = JDBCUTils.getConnection();// 连接数据库
stmt = conn.createStatement();
String sql = "select * from QQ";// sql语句
rs = stmt.executeQuery(sql);// 执行语句
while (rs.next()) {
if (username.equals(rs.getString("username")) && phonenumber.equals(rs.getString("phonenumber"))) {
flag = true;
break;
}
}
if (!flag && code.equals(VerificationTools.string())) {
Random r = new Random();
StringBuilder sb;
char[] chars = ("0123456789").toCharArray();
sb = new StringBuilder();
for (int i = 1; i <= 10; i++) {
int pos = r.nextInt(chars.length);
char c = chars[pos];
sb.append(c);
}
sql = "select * from QQ";// sql语句
rs = stmt.executeQuery(sql);// 执行语句
boolean flag1 = false, flag2 = false;
String number = null;
while (rs.next()) {
if (username.equals(rs.getString("username"))) {
flag1 = true;
}
if (phonenumber.equals(rs.getString("phonenumber"))) {
flag2 = true;
}
}
System.out.println("账号为"+sb);
sql = "INSERT INTO QQ(number,nickname,username,password,phonenumber)" + "VALUES(" + "15" + "," + "\""
+ username + "\"" + "," + sb + "," + password + "," + phonenumber + ");";
System.out.println(sql);
int re = stmt.executeUpdate(sql);// 执行语句
} else {
System.out.print("注册失败");
}
}
}
package qq.view;
import javax.swing.*;
import qq.Component.EnterPanel;
@SuppressWarnings("serial")
public class EnterFrame extends JFrame {
public EnterFrame() {
this.init();// 初始化操作
this.addComponent();// 添加组件
this.addListener();// 添加监听器
}
private void init() {
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
this.setUndecorated(true);// 设置JFrame窗口边框不显示
this.setResizable(false); // 禁止改变窗口大小
this.setLayout(null);
this.setBounds(1500, 200, 348, 695);
}
private void addComponent() {
this.add(new EnterPanel());
}
// 添加监听器
private void addListener() {
EnterPanel.getExitbutton().addActionListener(event -> this.dispose());
EnterPanel.getMinimizebutton().addActionListener(event -> this.setExtendedState(JFrame.ICONIFIED));
}
}
package qq.Component;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class EnterPanel extends JPanel {
private static ImageIcon image = new ImageIcon("enter.jpg");
private static JLabel background = new JLabel(image);
private static JButton exitbutton = new JButton();
private static ImageIcon exitsquare = new ImageIcon("exitbutton1.jpg");
private static JButton minimizebutton = new JButton();
private static ImageIcon minimizesquare = new ImageIcon("minimizebutton1.jpg");
public EnterPanel() {
this.init();// 初始化操作
this.addComponent();// 添加组件
}
private void init() {
this.setLayout(null);
this.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置panel的位置
background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置背景图片的位置及尺寸
this.add(background);
}
private void addComponent() {
getExitbutton().setIcon(exitsquare);// 把图片放到按钮上
getExitbutton().setBounds(image.getIconWidth() - exitsquare.getIconWidth(), 0, exitsquare.getIconWidth(),
exitsquare.getIconHeight());// 设置按钮的位置和大小
getExitbutton().setBorderPainted(false);// 取消按钮边框效果
this.add(getExitbutton());// 将按钮添加到panel中
// 最小化按钮
getMinimizebutton().setIcon(minimizesquare);
getMinimizebutton().setBounds(
image.getIconWidth() - exitsquare.getIconWidth() - minimizesquare.getIconWidth() + 6, 0,
minimizesquare.getIconWidth() - 1, minimizesquare.getIconHeight());
getMinimizebutton().setBorderPainted(false);
this.add(getMinimizebutton());
}
public static JButton getExitbutton() {
return exitbutton;
}
public static void setExitbutton(JButton exitbutton) {
EnterPanel.exitbutton = exitbutton;
}
public static JButton getMinimizebutton() {
return minimizebutton;
}
public static void setMinimizebutton(JButton minimizebutton) {
EnterPanel.minimizebutton = minimizebutton;
}
}
package qq.view;
import javax.swing.*;
import qq.Component.UnenterPanel;
import qqtools.GUItools;
@SuppressWarnings("serial")
public class UnenterFrame extends JFrame {
public UnenterFrame() {
this.init();// 初始化操作
this.addComponent();// 添加组件
this.addListener();// 添加监听器
}
private void init() {
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
this.setSize(532, 405);// 窗体大小与位置
this.setUndecorated(true);// 设置JFrame窗口边框不显示
this.setResizable(false); // 禁止改变窗口大小
GUItools.center(this);// 设置窗口在屏幕上的位置
}
private void addComponent() {
this.add(new UnenterPanel());
}
// 添加监听器
private void addListener() {
UnenterPanel.getExitbutton().addActionListener(event -> this.dispose());
UnenterPanel.getMinimizebutton().addActionListener(event -> this.setExtendedState(JFrame.ICONIFIED));
UnenterPanel.getSetupbutton().addActionListener(event -> this.dispose());
}
}
package qq.Component;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class UnenterPanel extends JPanel {
private static ImageIcon image = new ImageIcon("unenter.jpg");
private static JLabel background = new JLabel(image);
private static JButton exitbutton = new JButton();
private static ImageIcon exitsquare = new ImageIcon("exitbutton2.jpg");
private static JButton minimizebutton = new JButton();
private static ImageIcon minimizesquare = new ImageIcon("minimizebutton2.jpg");
private static JButton setupbutton = new JButton();
private static ImageIcon setupsquare = new ImageIcon("setupbutton2.jpg");
public UnenterPanel() {
this.init();// 初始化操作
this.addComponent();// 添加组件
}
private void init() {
this.setLayout(null);
this.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置panel的位置
background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());// 设置背景图片的位置及尺寸
this.add(background);
}
private void addComponent() {
getExitbutton().setIcon(exitsquare);// 把图片放到按钮上
getExitbutton().setBounds(532 - exitsquare.getIconWidth(), 0, exitsquare.getIconWidth(),
exitsquare.getIconHeight());// 设置按钮的位置和大小
getExitbutton().setBorderPainted(false);// 取消按钮边框效果
this.add(getExitbutton());// 将按钮添加到panel中
getMinimizebutton().setIcon(minimizesquare);
getMinimizebutton().setBounds(532 - exitsquare.getIconWidth() - minimizesquare.getIconWidth() + 5, 0,
minimizesquare.getIconWidth(), minimizesquare.getIconHeight());
getMinimizebutton().setBorderPainted(false);
this.add(getMinimizebutton());
getSetupbutton().setIcon(setupsquare);
getSetupbutton().setBounds(422, 375, setupsquare.getIconWidth(), setupsquare.getIconHeight());
getSetupbutton().setBorderPainted(false);
this.add(getSetupbutton());
}
public static JButton getSetupbutton() {
return setupbutton;
}
public static void setSetupbutton(JButton setupbutton) {
UnenterPanel.setupbutton = setupbutton;
}
public static ImageIcon getSetupsquare() {
return setupsquare;
}
public static void setSetupsquare(ImageIcon setupsquare) {
UnenterPanel.setupsquare = setupsquare;
}
public static JButton getMinimizebutton() {
return minimizebutton;
}
public static void setMinimizebutton(JButton minimizebutton) {
UnenterPanel.minimizebutton = minimizebutton;
}
public static ImageIcon getMinimizesquare() {
return minimizesquare;
}
public static void setMinimizesquare(ImageIcon minimizesquare) {
UnenterPanel.minimizesquare = minimizesquare;
}
public static ImageIcon getImage() {
return image;
}
public static void setImage(ImageIcon image) {
UnenterPanel.image = image;
}
public static JButton getExitbutton() {
return exitbutton;
}
public static void setExitbutton(JButton exitbutton) {
UnenterPanel.exitbutton = exitbutton;
}
public static ImageIcon getExitsquare() {
return exitsquare;
}
public static void setExitsquare(ImageIcon exitsquare) {
UnenterPanel.exitsquare = exitsquare;
}
}