1.项目布局(供新手参考)
学校老师任务,因为我也是新手所以写的不是特别的好,所以可以提供参考。
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
//工具类
public class Head {
public static JFrame frame = new JFrame("登录");
public static final int width = 900;
public static final int height = 600;
public static String UserName=null;
//动态切换面板
//获取屏幕分辨率 设置窗体在屏幕居中的位置
public static void initFrame() {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取一个与系统相关的工具类对象
Dimension size = toolkit.getScreenSize();//获取屏幕分辨率
int x = (int) size.getWidth();//获取中心位置的宽
int y = (int) size.getHeight();//获取中心位置的高
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);//设置窗口位置,大小
frame.setVisible(true);//设置窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
}
public static void initFrame(JFrame frame) {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取一个与系统相关的工具类对象
Dimension size = toolkit.getScreenSize();//获取屏幕分辨率
int x = (int) size.getWidth();//获取中心位置的宽
int y = (int) size.getHeight();//获取中心位置的高
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);//设置窗口位置,大小
frame.setVisible(true);//设置窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
}
//设置子窗口 获取屏幕分辨率 设置窗体在屏幕居中的位置
public static void initJDialog(JDialog frame) {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取一个与系统相关的工具类对象
Dimension size = toolkit.getScreenSize();//获取屏幕分辨率
int x = (int) size.getWidth();//获取中心位置的宽
int y = (int) size.getHeight();//获取中心位置的高
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);//设置窗口位置,大小
frame.setVisible(true);//设置窗口可见
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
}
//修改用户密码页面
public static void Update() {
UpdatePasswd.Update(frame);
}
}
//测试运行代码块
package GUI_Object;
import GUI_Object.GUI.Head;
import GUI_Object.GUI.Login;
public class dome {
public static void main(String[] args) {
Login.LoginWind();
Head.initFrame();
}
}
2.登录注册页面
登录页面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Login {
private static Boolean Judge = true;
//账户密码登录窗口loginWind
public static void LoginWind() {
JPanel panel = new JPanel();
Head.frame.setBackground(new Color(0xd4f6ff));
panel.setOpaque(false);
panel.setLayout(new FlowLayout());
panel.setLayout(null);
// 设置背景图片
//添加标签【学生管理系统】
ImageIcon imageIcon=new ImageIcon("src/GUI_Object/images/logo.jpg");
JLabel textStudentManage = new JLabel(imageIcon);//创建一个标签,并命名为“学生管理系统“
textStudentManage.setForeground(new Color(0XFFAEB9));//设置字体颜色
textStudentManage.setFont(new Font("黑体", Font.PLAIN, 50));//设置字体大小
textStudentManage.setBounds(0, 10, 900, 100);//设置标签的绝对位置
panel.add(textStudentManage);//向框架中添加组件【标签(学生管理系统)】
//添加标签【用户名】
JLabel textUser = new JLabel("用户名:");
textUser.setForeground(new Color(0Xdbb4f7));
textUser.setFont(new Font("黑体", Font.PLAIN, 30));
textUser.setBounds(200, 140, 200, 100);
panel.add(textUser);
//添加输入框【用户名输入框】
JTextField user = new JTextField(20);
user.setFont(new Font("黑体", Font.PLAIN, 18));
user.setSelectedTextColor(new Color(0xFFF5EE));
user.setBounds(330, 170, 280, 40);
panel.add(user);
//添加标签【密码】
JLabel textPassword = new JLabel("密码 :");
textPassword.setForeground(new Color(0Xdbb4f7));
textPassword.setFont(new Font("黑体", Font.PLAIN, 30));
textPassword.setBounds(200, 200, 200, 100);
panel.add(textPassword);
//添加密码输入框【密码】
JPasswordField password = new JPasswordField(20);
password.setBounds(330, 230, 280, 40);
panel.add(password);
//添加按钮【登录】
JButton jButton = new JButton("登录");
jButton.setForeground(new Color(0xffcbe1));
jButton.setBackground(new Color(0xfffde4));
jButton.setFont(new Font("黑体", Font.PLAIN, 20));
jButton.setBorderPainted(false);
jButton.setBounds(300, 330, 100, 50);
panel.add(jButton);
//添加按钮【注册】
JButton register = new JButton("注册");
register.setForeground(new Color(0xffcbe1));
register.setBackground(new Color(0xfffde4));
register.setFont(new Font("黑体", Font.PLAIN, 20));
register.setBorderPainted(false);
register.setBounds(500, 330, 100, 50);
panel.add(register);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = user.getText();//获取账户
String passwd = password.getText();//获取密码
int i = Database.QueryUser(name, passwd);
switch (i) {
case 0:
JOptionPane.showMessageDialog(panel, "登录成功", "通知", JOptionPane.INFORMATION_MESSAGE);
AverageUser.Subpage();
Head.UserName=name;
break;//普通用户登录页面
case 1:
JOptionPane.showMessageDialog(panel, "登录成功", "通知", JOptionPane.INFORMATION_MESSAGE);
Admin.Subpage();
SwingUtilities.updateComponentTreeUI(Head.frame);
Head.UserName=name;
break;//管理员用户登录页面
default:
JOptionPane.showMessageDialog(panel, "警告密码或者账户输入错误", "警告", JOptionPane.WARNING_MESSAGE);
}
}
});
// JButton button1 = new JButton("注册");
// panel.add(button1);
register.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//创建页面2;
if (Judge) {
Database.CreateAccount();
Judge = false;
}
GUI_Object.GUI.register.Subpage(Head.frame);
}
});
Head.frame.setContentPane(panel);
}
}
注册页面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* 注册页面
* */
public class register {
//创建子页面
public static void Subpage(JFrame jFrame){
JDialog jDialog = new JDialog(jFrame,"注册页面",true);//设置由谁弹出,名字,鼠标选中对话框时不能对其他对话框改变
jDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
panel.setLayout(null);
panel.setBackground(new Color(0xe7fafe));
ImageIcon icon =new ImageIcon("src/GUI_Object/images/RegisterLogo.jpg");
JLabel textStudentManage = new JLabel(icon);//创建一个标签,并命名为“学生管理系统“
textStudentManage.setBounds(0, 0, 900, 90);//设置标签的绝对位置
panel.add(textStudentManage);
//用户名
JLabel name = new JLabel("用户名:");
name.setForeground(new Color(0xa5d478));
name.setFont(new Font("黑体", Font.PLAIN, 30));
name.setBounds(200, 140, 200, 100);
panel.add(name);
//用户输入框
JTextField NameField = new JTextField(12);
NameField.setFont(new Font("黑体", Font.PLAIN, 18));
// NameField.setSelectedTextColor(new Color(0xFF0000));
NameField.setBounds(330, 170, 280, 40);
panel.add(NameField);
//标签密码
JLabel passwd = new JLabel("密码:");
passwd.setForeground(new Color(0xa5d478));
passwd.setFont(new Font("黑体", Font.PLAIN, 30));
passwd.setBounds(200, 200, 200, 100);
panel.add(passwd);
//密码框
JPasswordField passwordField= new JPasswordField(12);
passwordField.setBounds(330, 230, 280, 40);
panel.add(passwordField);
//下拉列表
Object[] arr = {"普通用户","管理员用户"};
JLabel label = new JLabel("用户权限:");
label.setForeground(new Color(0xa5d478));
label.setFont(new Font("黑体", Font.PLAIN, 30));
label.setBounds(200, 260, 200, 100);
JComboBox jComboBox = new JComboBox(arr);
jComboBox.setBounds(340, 290, 80, 40);
panel.add(label);
panel.add(jComboBox);
//确认按钮
JButton register = new JButton("确认");
register.setForeground(new Color(0xa5d478));
register.setBackground(new Color(0xfffed5));
register.setFont(new Font("黑体", Font.PLAIN, 20));
register.setBorderPainted(false);
register.setBounds(430, 290, 80, 40);
panel.add(register);
// JButton button = new JButton("确认");
register.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int i=10;
i = JOptionPane.showConfirmDialog(panel, "您确认注册用户吗?");
if (i==0) {
String text = NameField.getText();
String password = passwordField.getText();
int selectedIndex = jComboBox.getSelectedIndex();//获取选到的下拉向
Boolean aBoolean = Database.AddUser(text, password, selectedIndex);
if (aBoolean) {
JOptionPane.showMessageDialog(panel, "恭喜你,用户注册成功", "通知", JOptionPane.INFORMATION_MESSAGE);
jDialog.setVisible(false);
jFrame.setVisible(true);
} else {
JOptionPane.showMessageDialog(panel, "抱歉,用户注册失败", "警告", JOptionPane.ERROR_MESSAGE);
}
}
}
});
//返回键
JButton register1 = new JButton("返回");
register1.setForeground(new Color(0xfddfeb));
register1.setBackground(new Color(0x84d696));
register1.setFont(new Font("黑体", Font.PLAIN, 20));
register1.setBorderPainted(false);
register1.setBounds(0, 0, 80, 40);
register1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {