developer
HouXinyu 1 year ago
parent 9afac5cf0b
commit ad3aaccc5c

@ -1,82 +1,94 @@
package com.lingnan.supermarket.view;
import java.awt. *;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import javax.swing. *;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.awt.event.*;
public class Demo4 extends JFrame implements ActionListener
{
static Demo4 tplb=new Demo4();
static JLabel pan=new JLabel();
static ImageIcon[] imgs = {
new ImageIcon("s"),
new ImageIcon("static\\bg\\bg1.jpg"),
new ImageIcon("static\\bg\\bg2.jpg"),
new ImageIcon("static\\bg\\bg3.jpg"),
new ImageIcon("static\\bg\\bg4.jpg"),
new ImageIcon("static\\bg\\bg5.jpg"),
new ImageIcon("static\\bg\\bg6.jpg"),
new ImageIcon("static\\bg\\bg7.jpg"),
};
public static void settplb()/*<2A>ܿ<EFBFBD><DCBF>*/
{
// Demo4类用于创建一个展示图片切换效果的图形界面程序
public class Demo4 extends JFrame implements ActionListener {
// 创建一个静态的Demo4类的实例tplb用于在静态方法中方便地访问该类的相关属性和方法实现类似单例模式的效果
static Demo4 tplb = new Demo4();
// 创建一个静态的JLabel组件pan用于在界面上显示图片等内容作为图片展示的载体
static JLabel pan = new JLabel();
// 创建一个静态的ImageIcon数组imgs用于存储多个图片资源
static ImageIcon[] imgs = {
new ImageIcon("s"),
new ImageIcon("static\\bg\\bg1.jpg"),
new ImageIcon("static\\bg\\bg2.jpg"),
new ImageIcon("static\\bg\\bg3.jpg"),
new ImageIcon("static\\bg\\bg4.jpg"),
new ImageIcon("static\\bg\\bg5.jpg"),
new ImageIcon("static\\bg\\bg6.jpg"),
new ImageIcon("static\\bg\\bg7.jpg"),
};
// settplb方法用于设置JFrame的一些基本属性使其成为一个合适的图形界面窗口展示出来
public static void settplb() {
// 设置窗口的标题
tplb.setTitle("ͼƬ<CDBC>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>");
// 设置窗口的布局管理器为null意味着后续添加组件时需要手动指定组件的位置和大小绝对布局
tplb.setLayout(null);
tplb.setSize(700,800);
// 设置窗口的大小为宽度700像素高度800像素
tplb.setSize(700, 800);
// 设置窗口大小不可调整,用户不能通过拖动边框等方式改变窗口大小
tplb.setResizable(false);
tplb.setLocationRelativeTo(null);/*<2A><><EFBFBD>þ<EFBFBD><C3BE><EFBFBD>*/
tplb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*<2A>رճ<D8B1><D5B3><EFBFBD>*/
// 设置窗口在屏幕上居中显示,使窗口展示位置更美观合理
tplb.setLocationRelativeTo(null);
// 设置当窗口关闭时,整个应用程序随之退出,这是一种常见的关闭窗口行为设置
tplb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口可见,使其显示在屏幕上,否则窗口创建后是不可见的状态
tplb.setVisible(true);
}
// setpan方法用于设置JLabel组件的位置等属性并添加到窗口中同时启动一个定时器来实现图片的定时切换功能
public static void setpan() {
// 设置JLabel组件pan的位置和大小x坐标为50像素y坐标为50像素宽度和高度都为500像素确定其在窗口中的显示区域
pan.setBounds(50, 50, 500, 500);
// 将pan这个JLabel组件添加到tplb这个窗口实例中使其成为窗口内容的一部分
tplb.add(pan);
// 创建一个定时器对象timer设置每隔1秒触发一次事件并且指定事件监听器为L
Timer timer = new Timer(1000, L);
// 启动定时器,开始计时,一旦达到设定的时间间隔就会切换图片
timer.start();
}
// 定义一个实现了ActionListener接口的匿名内部类L用于处理定时器触发的事件实现图片在JLabel组件上的切换效果
static ActionListener L = new ActionListener() {
int index; // 定义一个整型变量index用于记录当前要显示的图片在imgs数组中的索引位置
public static void setpan()
{
pan.setBounds(50, 50, 500, 500);
tplb.add(pan);
Timer timer = new Timer(1000,L);
timer.start();
}
static ActionListener L=new ActionListener()
{
int index;
@Override
public void actionPerformed(ActionEvent e)
{
public void actionPerformed(ActionEvent e) {
// 将pan这个JLabel组件的图标设置为imgs数组中当前索引对应的图片实现图片的切换展示效果
pan.setIcon(imgs[index]);
// 将索引值加1准备切换到下一张图片
index++;
if(index==7)
index=0;
// 如果索引值达到了imgs数组的长度7表示已经到最后一张图片了将索引重置为0实现循环切换图片的效果
if (index == 7)
index = 0;
}
};
public static void main(String[] args) {
settplb();
setpan();
}
// 调用设置窗口和图片展示相关的方法来展示图形界面效果
public static void main(String[] args) {
// 调用settplb方法设置窗口的基本属性并使其显示出来
settplb();
// 调用setpan方法设置图片展示的JLabel组件相关属性并启动图片切换的定时器功能
setpan();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO <20>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ɵķ<C9B5><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
}

@ -1,13 +1,12 @@
package com.lingnan.supermarket.view;
import java.awt. *;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import javax.imageio.ImageIO;
import javax.swing. *;
import javax.swing.*;
import com.lingnan.supermarket.componet.BGPanel;
import com.lingnan.supermarket.dao.impl.inOrderServiceImpl;
import com.lingnan.supermarket.dao.impl.outOrderServiceImpl;
@ -16,161 +15,221 @@ import com.lingnan.supermarket.table.UserTableModel;
import com.lingnan.supermarket.utils.FontUtil;
import com.lingnan.supermarket.utils.TimeAndOrder;
import com.lingnan.supermarket.view.base.BaseView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.awt.event.*;
public class HomeView extends JPanel implements ActionListener
{
private JLabel pan;
private static JLabel pan1;
private static JLabel pan2;
private static JLabel pan3;
private JFrame jFrame;
private JPanel tplbPanel;
private Image bgImage = null;
private ImageIcon[] imgs;
private static Float allInPrice;
private static Float allOutPrice;
private JButton refreshBtn;
private JPanel priceJPanel;
private static String date;
private static inOrderServiceImpl inOrderImpl ;
private static outOrderServiceImpl outOrderImpl;
public HomeView(JFrame jFrame) {
this.setLayout(null);
this.jFrame = jFrame;
initView();
}
private void initView() {
pan = new JLabel();
pan.setBounds(0,0, 1280,351);
/*tplbPanel.add(pan);*/
imgs =new ImageIcon[7];
for(int i =0;i<7;i++) {
imgs[i]=new ImageIcon("static\\轮播\\0"+i+".jpg");
}
pan.setIcon(imgs[6]);
Timer timer = new Timer(2500,L);
timer.start();
this.add(pan);
date= TimeAndOrder.yMdTime();/*获取今天时间*/
inOrderImpl = new inOrderServiceImpl();
outOrderImpl = new outOrderServiceImpl();
allInPrice=inOrderImpl.TodayInPrice(date);
allOutPrice=outOrderImpl.TodayOutPrice(date);
System.out.println("今日allInprice="+allInPrice);
System.out.println("今日allOutprice="+allOutPrice);
pan1 = new JLabel("今日进货总金额:"+allInPrice+"元",new ImageIcon("static\\icon\\money.png"),JLabel.LEFT);
pan2 = new JLabel("今日收银总金额:"+allOutPrice+"元",new ImageIcon("static\\icon\\income.png"),JLabel.LEFT);
pan3 = new JLabel("今日被投诉次数:0次",new ImageIcon("static\\icon\\complaints.png"),JLabel.LEFT);
pan1.setFont(FontUtil.homeFont);
pan2.setFont(FontUtil.homeFont);
pan3.setFont(FontUtil.homeFont);
pan1.setBounds(280,300, 600,200);
pan2.setBounds(280,400, 600,200);
pan3.setBounds(280,500, 600,200);
/* priceJPanel = new JPanel();
priceJPanel.setBounds(100,200,700,500);*/
this.add(pan1);
this.add(pan2);
this.add(pan3);
refreshBtn = new JButton(new ImageIcon("static\\icon\\refresh.png"));
refreshBtn.addActionListener(this);
refreshBtn.setBounds(1050,700, 40,40);
this.add(refreshBtn);
}
ActionListener L=new ActionListener()
{
int index;
@Override
public void actionPerformed(ActionEvent e)
{
pan.setIcon(imgs[index]);
index++;
if(index==7)
index=0;
}
};
// HomeView类用于构建超市管理系统中首页的视图界面展示如图片轮播、关键业务数据以及提供数据刷新功能
public class HomeView extends JPanel implements ActionListener {
// 用于展示图片轮播效果的JLabel组件
private JLabel pan;
// 用于显示今日进货总金额相关信息
private static JLabel pan1;
// 用于显示今日收银总金额相关信息
private static JLabel pan2;
// 用于显示今日被投诉次数相关信息
private static JLabel pan3;
private JFrame jFrame;
private JPanel tplbPanel;
// 用于存储背景图片的Image对象初始化为null
private Image bgImage = null;
// 定义的ImageIcon数组用于存储多张图片资源以实现图片轮播功能
private ImageIcon[] imgs;
// 静态的Float类型变量记录今日进货的总金额
private static Float allInPrice;
// 静态的Float类型变量记录今日收银的总金额
private static Float allOutPrice;
// 用于触发刷新操作的按钮组件
private JButton refreshBtn;
private JPanel priceJPanel;
// 存储当前日期信息,方便在类内不同方法中按日期查询业务数据
private static String date;
// 是实现进货订单相关业务逻辑的服务层接口的类实例,用于获取进货相关数据
private static inOrderServiceImpl inOrderImpl;
// 用于实现销售订单相关业务逻辑,获取收银相关数据
private static outOrderServiceImpl outOrderImpl;
// 构造函数用于创建HomeView实例并初始化界面相关组件
public HomeView(JFrame jFrame) {
this.setLayout(null); //设置布局为null
this.jFrame = jFrame; //保存父窗口引用
initView(); //调用initView方法初始化组件
}
// 初始化视图界面组件的方法,负责创建、配置各组件并设置其属性、添加组件到面板以及为按钮添加事件监听器等操作,构建首页界面展示效果
private void initView() {
// 创建一个JLabel组件pan设置其初始位置和大小用于后续图片轮播展示
pan = new JLabel();
pan.setBounds(0, 0, 1280, 351);
/*tplbPanel.add(pan);*/
// 创建包含7个元素的ImageIcon数组imgs用于存储图片轮播的图片资源通过循环从指定路径加载图片文件创建ImageIcon对象
imgs = new ImageIcon[7];
for (int i = 0; i < 7; i++) {
imgs[i] = new ImageIcon("static\\轮播\\0" + i + ".jpg");
}
// 设置图片轮播起始图片
pan.setIcon(imgs[6]);
// 创建一个定时器对象timer设置时间间隔为2500毫秒2.5秒指定事件监听器为L然后启动定时器实现图片定时轮播
Timer timer = new Timer(2500, L);
timer.start();
// 将pan这个JLabel组件添加到当前面板中使其能在界面上显示出来参与图片轮播
this.add(pan);
// 调用TimeAndOrder工具类的yMdTime方法获取当前日期信息
date = TimeAndOrder.yMdTime();
//用于调用其实现的与进货订单相关业务方法获取数据
inOrderImpl = new inOrderServiceImpl();
// 创建outOrderServiceImpl类的实例用于调用与销售订单相关业务方法获取销售相关数据如获取今日收银总金额
outOrderImpl = new outOrderServiceImpl();
// 调用inOrderImpl的TodayInPrice方法传入当前日期date获取今日进货的总金额用于界面显示及其他业务逻辑处理
allInPrice = inOrderImpl.TodayInPrice(date);
// 调用outOrderImpl的TodayOutPrice方法传入date获取今日收银总金额用于展示销售数据及相关操作
allOutPrice = outOrderImpl.TodayOutPrice(date);
// 在控制台打印今日进货总金额信息,用于调试或简单日志记录
System.out.println("今日allInprice=" + allInPrice);
// 在控制台打印今日收银总金额信息,起到调试或日志记录作用
System.out.println("今日allOutprice=" + allOutPrice);
// 创建一个JLabel组件pan1设置其显示文本为包含今日进货总金额的内容并设置图标及文本在图标的左侧显示用于直观展示进货金额信息
pan1 = new JLabel("今日进货总金额:" + allInPrice + "元", new ImageIcon("static\\icon\\money.png"), JLabel.LEFT);
// 设置pan1这个JLabel组件的字体为通过FontUtil类获取的适用于首页显示的特定字体
pan1.setFont(FontUtil.homeFont);
// 设置pan1在界面中的位置和大小
pan1.setBounds(280, 300, 600, 200);
// 创建JLabel组件pan2设置其显示文本为包含今日收银总金额的内容同样设置图标及文本位置用于展示收银金额信息
pan2 = new JLabel("今日收银总金额:" + allOutPrice + "元", new ImageIcon("static\\icon\\income.png"), JLabel.LEFT);
// 设置pan2的字体为首页特定字体
pan2.setFont(FontUtil.homeFont);
// 设置pan2在界面中的位置和大小
pan2.setBounds(280, 400, 600, 200);
// 创建JLabel组件pan3设置其显示文本为今日被投诉次数相关内容初值为0次设置图标及文本位置用于展示投诉情况信息
pan3 = new JLabel("今日被投诉次数:0次", new ImageIcon("static\\icon\\complaints.png"), JLabel.LEFT);
// 设置pan3的字体为首页特定字体
pan3.setFont(FontUtil.homeFont);
// 设置pan3在界面中的位置和大小
pan3.setBounds(280, 500, 600, 200);
/*priceJPanel = new JPanel();
priceJPanel.setBounds(100, 200, 700, 500);*/
// 将pan1、pan2、pan3这三个用于显示关键信息的JLabel组件添加到当前面板HomeView使其在界面上展示出来供用户查看相关业务数据
this.add(pan1);
this.add(pan2);
this.add(pan3);
// 创建一个按钮组件refreshBtn使用指定图标文件创建按钮图标
refreshBtn = new JButton(new ImageIcon("static\\icon\\refresh.png"));
// 为refreshBtn按钮添加点击事件监听器将点击事件绑定到当前类的actionPerformed方法上
refreshBtn.addActionListener(this);
// 设置refreshBtn按钮在界面中的位置和大小
refreshBtn.setBounds(1050, 700, 40, 40);
// 将refreshBtn按钮添加到当前面板HomeView
this.add(refreshBtn);
}
// 定义一个实现了ActionListener接口的匿名内部类L用于处理定时器触发的事件实现图片在pan这个JLabel组件上的定时切换展示达到图片轮播效果
ActionListener L = new ActionListener() {
int index; // 定义变量index用于记录当前要显示图片在imgs数组中的索引位置初始默认为0
@Override
public void actionPerformed(ActionEvent e) {
// 将pan这个JLabel组件的图标设置为imgs数组中当前索引index对应的图片实现图片切换展示效果定时器触发时更新显示图片
pan.setIcon(imgs[index]);
// 将索引值index加1准备切换到下一张图片
index++;
// 判断索引值是否达到imgs数组长度7若达到则重置为0实现图片循环轮播
if (index == 7)
index = 0;
}
};
// 定义静态方法refreshHome用于刷新首页界面上显示的关键数据进货、收银金额等
public static void refreshHome() {
date= TimeAndOrder.yMdTime();/*获取今天时间*/
allInPrice=inOrderImpl.TodayInPrice(date);
allOutPrice=outOrderImpl.TodayOutPrice(date);
System.out.println("今日allInprice="+allInPrice);
System.out.println("今日allOutprice="+allOutPrice);
pan1.setText("今日进货总金额:"+allInPrice+"元");
pan2.setText("今日收银总金额:"+allOutPrice+"元");
pan3.setText("今日被投诉次数:0次");
/* priceJPanel = new JPanel();
priceJPanel.setBounds(100,200,700,500);*/
/* this.add(pan1);
this.add(pan2);
this.add(pan3);*/
// 再次调用TimeAndOrder工具类的yMdTime方法获取当前最新日期信息
date = TimeAndOrder.yMdTime();
// 调用inOrderImpl的TodayInPrice方法传入最新日期date重新获取今日进货总金额并更新allInPrice变量的值
allInPrice = inOrderImpl.TodayInPrice(date);
// 调用outOrderImpl的TodayOutPrice方法传入date重新获取今日收银总金额并更新allOutPrice变量的值
allOutPrice = outOrderImpl.TodayOutPrice(date);
// 在控制台打印更新后的今日进货总金额信息
System.out.println("今日allInPrice=" + allInPrice);
// 在控制台打印更新后的今日收银总金额信息
System.out.println("今日allOutPrice=" + allOutPrice);
// 更新pan1这个JLabel组件的文本内容显示最新的进货总金额信息实现界面数据实时刷新
pan1.setText("今日进货总金额:" + allInPrice + "元");
// 更新pan2的文本内容显示最新的收银总金额信息保证销售数据在界面上及时更新展示
pan2.setText("今日收银总金额:" + allOutPrice + "元");
// 更新pan3的文本内容开始显示0次投诉
pan3.setText("今日被投诉次数:0次");
/*priceJPanel = new JPanel();
priceJPanel.setBounds(100, 200, 700, 500);*/
/*this.add(pan1);
this.add(pan2);
this.add(pan3);*/
}
// 实现ActionListener接口的actionPerformed方法
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 获取触发被点击的按钮
Object source = e.getSource();
if(source==refreshBtn) {
if (source == refreshBtn) {
// 如果点击的是refreshBtn按钮创建一个新的HomeView实例
new HomeView(jFrame);
// 调用refreshHome方法刷新首页界面上显示的关键数据更新界面展示内容
refreshHome();
}
}
}

@ -24,135 +24,160 @@ import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
// 导入用户相关服务层接口及实现类,用于处理用户登录等业务逻辑
import com.lingnan.supermarket.dao.UserService;
import com.lingnan.supermarket.dao.impl.UserServiceImpl;
// 导入用户数据传输对象相关类,用于传递用户相关的数据信息
import com.lingnan.supermarket.dto.Production;
import com.lingnan.supermarket.dto.User;
import com.lingnan.supermarket.componet.BGPanel;
import com.lingnan.supermarket.view.base.BaseView;
public class LoginView extends BaseView implements ActionListener{
//setLayout(null);
//setBounds(x,y,width,height)
private JPanel containerPanel,namePanel,passwordPanel;
private JLabel nameLabel,pwdLabel;
private JTextField nameTF;
private JPasswordField pwdTF;
private JButton loginBtn;
private User user=null;
/*创建窗口*/
public LoginView() {
// 登录视图类实现ActionListener接口用于处理按钮点击等事件
public class LoginView extends BaseView implements ActionListener {
// 组件声明
// 用于容纳其他面板和组件的主面板,整体布局容器
private JPanel containerPanel;
// 用于放置用户名相关组件的面板
private JPanel namePanel;
// 用于放置密码相关组件的面板
private JPanel passwordPanel;
// 用户名标签
private JLabel nameLabel;
// 密码标签
private JLabel pwdLabel;
// 用于输入用户名的文本框
private JTextField nameTF;
// 用于输入密码的密码框
private JPasswordField pwdTF;
// 登录按钮,点击后触发登录验证等相关操作
private JButton loginBtn;
// 用于存储登录成功后的用户对象信息初始化为null
private User user = null;
/*
*
*
* int width
* int height
* String title
*/
public LoginView() {
// 调用父类BaseView的构造方法来设置窗口的大小和标题
super(350, 250, "新民超市");
ImageIcon icon=new ImageIcon("static\\icon\\新.png"); //xxx代表图片存放路径2.png图片名称及格式
// 创建一个ImageIcon对象用于设置窗口的图标传入的参数是图标图片的路径及文件名
ImageIcon icon = new ImageIcon("static\\icon\\新.png");
// 设置窗口的图标通过获取ImageIcon中的Image对象来设置
this.setIconImage(icon.getImage());
}
/*添加组件*/
@Override/*界面*/
/*
* initView
*/
@Override
protected void initView() {
Image bgImage = null;
try {
// 尝试从指定文件路径读取背景图片若读取失败会抛出IOException异常
bgImage = ImageIO.read(new File("static\\bg\\bg1.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
containerPanel = new BGPanel(bgImage);
//用户名
namePanel = new JPanel();
// 创建一个自定义的背景面板,传入读取到的背景图片,用于作为整个界面的背景容器
containerPanel = new BGPanel(bgImage);
// 用户名相关组件的设置
// 创建放置用户名相关组件的面板
namePanel = new JPanel();
// 创建用户名标签通过传入一个ImageIcon来显示带有图标的提示信息
nameLabel = new JLabel(new ImageIcon("static\\icon\\loginName.png"));
nameTF = new JTextField("z001",22);
// 创建用户名输入文本框,并设置默认显示的文本内容以及文本框的宽度
nameTF = new JTextField("z001", 22);
// 将用户名标签和输入文本框添加到用户名面板中
namePanel.add(nameLabel);
namePanel.add(nameTF);
//密码
// 密码相关组件的设置
// 创建放置密码相关组件的面板
passwordPanel = new JPanel();
// 创建密码标签通过传入一个ImageIcon来显示带有图标的提示信息
pwdLabel = new JLabel(new ImageIcon("static\\icon\\pwd.png"));
pwdTF = new JPasswordField("0.00.0",22);
// 创建密码输入文本框
pwdTF = new JPasswordField("0.00.0", 22);
// 将密码标签和密码输入文本框添加到密码面板中
passwordPanel.add(pwdLabel);
passwordPanel.add(pwdTF);
//登录
// 创建登录按钮,并设置按钮上显示的文本内容
loginBtn = new JButton("登录");
// 为登录按钮添加ActionListener监听器当按钮被点击时会触发相应的事件处理方法
loginBtn.addActionListener(this);
/*添加组件*/
// 将用户名面板、密码面板和登录按钮添加到主容器面板中,确定它们在界面中的布局顺序等
containerPanel.add(namePanel);
containerPanel.add(passwordPanel);
containerPanel.add(loginBtn);
// 获取窗口的内容面板,将主容器面板添加到内容面板中,使得界面组件能够正确显示在窗口内
Container container = getContentPane();
container.add(containerPanel);
}
/*事件处理*/
/*
* ActionListener
*
* ActionEvent e
*/
@Override
public void actionPerformed(ActionEvent e) {
/*如果点击登录*/
if(e.getSource()==loginBtn){
String loginName = nameTF.getText();
String password = new String(pwdTF.getPassword());
System.out.println("点击登录后");
System.out.println("用户名为"+loginName);
System.out.println("密码为"+password);
//TODO 参数校验
UserServiceImpl userService = new UserServiceImpl();
user = userService.login(loginName, password);
if(user==null) {
JOptionPane.showMessageDialog(this,"账号或密码错误");
}else {
//去到主界面
Random random=new Random();
int skin=random.nextInt(10);
System.out.println("skin="+skin);
String iconSkin = "static\\icon\\新.png";/*默认icon*/
new MainView(user,skin,iconSkin);
this.dispose();
}
// 判断事件源是否是登录按钮
if (e.getSource() == loginBtn) {
// 获取用户名输入文本框中的文本内容,作为登录用户名
String loginName = nameTF.getText();
// 获取密码输入文本框中的密码内容通过将char数组转换为字符串的方式获取
String password = new String(pwdTF.getPassword());
System.out.println("点击登录后");
System.out.println("用户名为" + loginName);
System.out.println("密码为" + password);
// 创建用户服务层的实现类对象,用于调用登录相关的业务逻辑方法
UserServiceImpl userService = new UserServiceImpl();
// 调用用户服务层的登录方法,传入用户名和密码进行登录验证,返回验证后的用户对象
user = userService.login(loginName, password);
// 根据登录验证结果进行相应处理
if (user == null) {
// 如果用户对象为null说明登录失败弹出提示框告知用户账号或密码错误
JOptionPane.showMessageDialog(this, "账号或密码错误");
} else {
// 如果登录成功,生成一个随机数,用于后续可能的界面皮肤等相关随机选择逻辑
Random random = new Random();
int skin = random.nextInt(10);
System.out.println("skin=" + skin);
// 设置默认的图标皮肤
String iconSkin = "static\\icon\\新.png";
// 创建主界面对象,传入登录成功的用户对象、随机生成的皮肤编号以及图标皮肤路径,进入主界面展示相关功能
new MainView(user, skin, iconSkin);
// 关闭当前登录窗口,释放资源等
this.dispose();
}
}
}
public static void main(String[] args) {
//设置界面外观
Nimbus.Nimbus();
// 创建登录视图LoginView对象启动登录界面展示
LoginView loginView = new LoginView();
}
}
@Override
protected void initView(User user,int skin) {
protected void initView(User user, int skin) {
// TODO Auto-generated method stub
}
}
}
}

@ -23,163 +23,231 @@ import com.lingnan.supermarket.dto.User;
import com.lingnan.supermarket.table.UserTableModel;
import com.lingnan.supermarket.utils.FontUtil;
public class UserView extends JPanel implements ActionListener{
//上面
// UserView类继承自JPanel并实现ActionListener接口用于构建用户管理界面相关的视图处理用户交互动作
public class UserView extends JPanel implements ActionListener {
// 用于存放整个顶部工具栏相关组件的面板
private JPanel toolBarPanel;
// 用于存放搜索相关组件的面板
private JPanel searchPanel;
private JLabel nameLabel,locationLabel;
// 用于显示"姓名"提示文字的标签
private JLabel nameLabel, locationLabel;
// 用于输入姓名进行搜索的文本框
private JTextField nameSearchTF;
// 用于触发搜索操作的按钮
private JButton searchBtn;
// 用于存放操作按钮(添加、更新、删除等)的面板
private JPanel opePanel;
private JButton addBtn,updateBtn,deleteBtn;
//中间
// 用于触发添加用户操作的按钮
private JButton addBtn, updateBtn, deleteBtn;
// 界面中部相关组件声明
// 用于存放表格并提供滚动功能的滚动面板,以便在表格内容较多时可以滚动查看
private JScrollPane tableScrollPane;
// 用于展示用户数据的表格组件
private JTable userTable;
//删除时选中赋值给id
// 用于记录删除操作时选中记录的用户id初始化为0
private int id;
//下面
// 界面下部相关组件声明
// 用于存放记录数相关提示信息的面板
private JPanel bottomPanel;
// 用于显示用户记录总数相关提示信息的标签
private JLabel countInfoLabel;
// 对父窗口JFrame类型的引用
private JFrame jFrame;
private UserTableModel userTableModel ;
private UserService userService=new UserServiceImpl();
// 用户表格数据模型对象,用于管理表格中的数据,与数据库等数据源进行交互获取和更新数据等
private UserTableModel userTableModel;
// 用户服务接口的实现类实例,通过它可以调用与用户相关的数据库操作方法,如增删改查等功能
private UserService userService = new UserServiceImpl();
// 构造函数接收一个JFrame类型的参数jFrame用于创建用户管理视图并初始化界面组件
public UserView(JFrame jFrame) {
// 设置该面板UserView的布局为BorderLayout将界面划分为北、南、东、西、中五个区域来放置组件
this.setLayout(new BorderLayout());
// 调用初始化视图组件的方法
initView();
// 将传入的父窗口引用保存到成员变量jFrame中
this.jFrame = jFrame;
}
// 初始化用户管理视图界面组件的方法
private void initView() {
toolBarPanel = new JPanel(new BorderLayout());
searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
locationLabel=new JLabel("当前位置>人员管理");
// 创建一个新的面板toolBarPanel设置其布局为BorderLayout用于存放顶部的搜索和操作按钮等组件
toolBarPanel = new JPanel(new BorderLayout());
// 创建一个新的面板searchPanel设置其布局为FlowLayout且组件左对齐用于存放搜索相关的组件
searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
// 创建一个显示当前位置提示信息的标签,这里显示为"当前位置>人员管理"
locationLabel = new JLabel("当前位置>人员管理");
// 设置标签的字体为通过FontUtil类获取的特定字体这里假设FontUtil类用于统一管理字体相关设置
locationLabel.setFont(new FontUtil().userFont);
// 设置标签的前景色文字颜色为特定的蓝色RGB值为18, 150, 219
locationLabel.setForeground(new Color(18, 150, 219));
// 创建一个显示"姓名"文字的标签,用于提示用户在旁边的文本框中输入姓名进行搜索
nameLabel = new JLabel("姓名");
// 创建一个文本框用于用户输入要搜索的姓名设置其可显示的宽度大致为10个字符左右
nameSearchTF = new JTextField(10);
// 创建一个按钮,使用指定路径下的图标文件作为按钮的图标,用于触发搜索操作
searchBtn = new JButton(new ImageIcon("static\\icon\\search.png"));
/*右边功能模块*/
opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
addBtn =new JButton(new ImageIcon("static\\icon\\add.png"));
updateBtn =new JButton(new ImageIcon("static\\icon\\update.png"));
deleteBtn =new JButton(new ImageIcon("static\\icon\\delete.png"));
// 创建一个新的面板opePanel设置其布局为FlowLayout且组件右对齐用于存放操作按钮添加、更新、删除
opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
// 创建一个按钮,使用指定路径下的图标文件("static\\icon\\add.png")作为按钮的图标,用于触发添加用户操作
addBtn = new JButton(new ImageIcon("static\\icon\\add.png"));
// 创建一个按钮,使用指定路径下的图标文件("static\\icon\\update.png")作为按钮的图标,用于触发更新用户操作
updateBtn = new JButton(new ImageIcon("static\\icon\\update.png"));
// 创建一个按钮,使用指定路径下的图标文件("static\\icon\\delete.png")作为按钮的图标,用于触发删除用户操作
deleteBtn = new JButton(new ImageIcon("static\\icon\\delete.png"));
// 为添加、更新、删除和搜索按钮添加点击事件监听器将按钮的点击事件绑定到当前类实现了ActionListener接口的actionPerformed方法上以便处理相应的操作逻辑
addBtn.addActionListener(this);
updateBtn.addActionListener(this);
deleteBtn.addActionListener(this);
searchBtn.addActionListener(this);
// 将添加、更新、删除按钮依次添加到操作按钮面板opePanel中
opePanel.add(addBtn);
opePanel.add(updateBtn);
opePanel.add(deleteBtn);
// 将位置提示标签、姓名标签、姓名搜索文本框和搜索按钮依次添加到搜索面板searchPanel中
searchPanel.add(locationLabel);
searchPanel.add(nameLabel);
searchPanel.add(nameSearchTF);
searchPanel.add(searchBtn);
toolBarPanel.add(searchPanel,"West");
toolBarPanel.add(opePanel,"East");
//中间表格
userTableModel = new UserTableModel();
// 将搜索面板添加到toolBarPanel的西边左侧区域将操作按钮面板添加到toolBarPanel的东边右侧区域
toolBarPanel.add(searchPanel, "West");
toolBarPanel.add(opePanel, "East");
// 创建用户表格数据模型对象
userTableModel = new UserTableModel();
// 调用数据模型的方法获取所有用户数据用于初始化表格显示内容这里假设UserTableModel类中的all方法用于从数据库等数据源获取全部用户数据并填充到模型中
userTableModel.all();
// 创建一个JTable对象使用前面创建的用户表格数据模型userTableModel来管理表格中的数据展示和交互
userTable = new JTable(userTableModel);
// 设置表格的字体为通过FontUtil类获取的适用于表格的特定字体假设FontUtil类中定义了相关字体常量
userTable.setFont(FontUtil.tableFont);
// 设置表格每行的高度为50像素调整表格的显示样式
userTable.setRowHeight(50);
// 创建一个滚动面板,将用户表格添加到滚动面板中,以便在表格内容较多时可以通过滚动条查看全部内容
tableScrollPane = new JScrollPane(userTable);
//下面
// 创建一个新的面板bottomPanel设置其布局为FlowLayout且组件左对齐用于存放记录数相关提示信息
bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
countInfoLabel = new JLabel("总共"+userTableModel.getRowCount()+"条");
// 创建一个显示用户记录总数的标签,初始文本为"总共"加上通过用户表格数据模型获取的行数假设UserTableModel类的getRowCount方法返回当前模型中的数据行数以及"条"字
countInfoLabel = new JLabel("总共" + userTableModel.getRowCount() + "条");
// 将记录数标签添加到bottomPanel面板中
bottomPanel.add(countInfoLabel);
this.add(toolBarPanel,"North");
this.add(tableScrollPane,"Center");/*将表格放到中间*/
this.add(bottomPanel,"South");
// 将顶部工具栏面板添加到当前面板UserView的北部上方区域
this.add(toolBarPanel, "North");
// 将包含用户表格的滚动面板添加到当前面板UserView的中部区域使其在界面中间显示
this.add(tableScrollPane, "Center");
// 将记录数面板添加到当前面板UserView的南部下方区域
this.add(bottomPanel, "South");
// 设置当前面板UserView可见使其在界面上显示出来
setVisible(true);
}
// 处理按钮点击等用户交互动作的方法实现了ActionListener接口中的方法
@Override
public void actionPerformed(ActionEvent e) {
// 获取触发事件的源组件(即被点击的按钮等组件)
Object source = e.getSource();
if(addBtn==source) {
//添加数据的对话框
if (addBtn == source) {
// 如果点击的是添加按钮,执行以下操作:
// 创建一个用于添加用户信息的对话框
UserDialog userDialog = new UserDialog(jFrame);
// 设置该对话框可见,显示在屏幕上,让用户进行添加用户信息的操作
userDialog.setVisible(true);
// 调用刷新用户数据的方法,用于在添加操作完成后更新界面上的用户数据显示
refreshUser();
}else if(updateBtn==source) {
} else if (updateBtn == source) {
// 如果点击的是更新按钮,调用刷新用户数据的方法,用于在更新操作相关逻辑完成后更新界面上的用户数据显示
refreshUser();
}else if(deleteBtn==source) {
//获取选中记录
} else if (deleteBtn == source) {
// 如果点击的是删除按钮,获取用户在表格中选中的行索引,如果没有选中任何行,则返回 -1
int rowIndex = userTable.getSelectedRow();
if(rowIndex==-1) {
JOptionPane.showMessageDialog(this,"请选中一条");
if (rowIndex == -1) {
// 如果没有选中行,弹出提示框提醒用户需要选中一条记录后再进行删除操作
JOptionPane.showMessageDialog(this, "请选中一条");
return;
}
int id = (Integer)userTable.getValueAt(rowIndex,0);
int select=JOptionPane.showConfirmDialog(this,"是否删除id="+id,"提示",JOptionPane.YES_NO_OPTION);
if(select==JOptionPane.YES_OPTION){
if(userService.deleteUser(id)==1) {
JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE);
// 获取选中行的第一列的值并转换为整数类型赋值给id变量用于后续删除操作时确定要删除的用户记录
int id = (Integer) userTable.getValueAt(rowIndex, 0);
// 弹出确认对话框询问用户是否确定删除指定id的用户记录提供"是"和"否"两个选项,返回用户选择的结果
int select = JOptionPane.showConfirmDialog(this, "是否删除id=" + id, "提示", JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
// 如果用户选择了"是"(确认删除),执行以下操作:
// 调用用户服务接口的删除用户方法传入要删除的用户id尝试从数据库中删除对应的用户记录并获取返回结果假设返回1表示删除成功其他值表示失败
if (userService.deleteUser(id) == 1) {
// 如果删除成功,弹出提示框显示"删除成功"的消息,提示框的图标为信息图标
JOptionPane.showMessageDialog(this, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
// 如果删除失败,弹出提示框显示"删除失败"的消息,提示框的图标为错误图标
JOptionPane.showMessageDialog(this, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
}
}
// 无论删除操作是否成功,都调用刷新用户数据的方法,更新界面上的用户数据显示,确保表格等显示内容与数据库中的最新数据一致
refreshUser();
}else{
} else {
// 如果点击的是搜索按钮(即其他未明确匹配的按钮点击情况,这里目前代码逻辑中只存在搜索按钮可能进入此分支),执行以下操作:
System.out.println("搜索");
// 调用刷新按姓名搜索结果的方法,根据用户在姓名搜索文本框中输入的内容进行数据刷新和表格显示更新
refreshFindRname();
}
}
// 根据姓名进行搜索并刷新表格显示内容的方法
public void refreshFindRname() {
// 获取用户在姓名搜索文本框中输入的姓名内容
String rname = nameSearchTF.getText();
User user =new User();
// 创建一个临时的User对象用于传递搜索条件
User user = new User();
// 将获取到的姓名设置到临时User对象中作为搜索条件
user.setRname(rname);
// 创建一个新的用户表格数据模型对象,用于重新获取和设置符合搜索条件的数据
userTableModel = new UserTableModel();
// 调用用户表格数据模型的方法根据传入的包含姓名条件的User对象进行数据查询获取符合条件的用户数据
userTableModel.Byrname(user);
// 将更新后的用户表格数据模型设置给用户表格
userTable.setModel(userTableModel);
//同时更新下面的记录数
refreshCount();
// 同时调用刷新记录数的方法,更新界面下方显示的用户记录总数提示信息
refreshCount();
}
// 刷新用户数据显示
public void refreshUser() {
// 创建一个新的用户表格数据模型对象
userTableModel = new UserTableModel();
// 调用数据模型的方法获取所有用户数据,重新填充模型
userTableModel.all();
// 将更新后的用户表格数据模型设置给用户表格,使表格显示最新的全部用户数据内容
userTable.setModel(userTableModel);
//同时更新下面的记录数
// 同时调用刷新记录数的方法,更新界面下方显示的用户记录总数提示信息
refreshCount();
}
public void refreshCount(){
// 刷新界面下方显示的用户记录总数提示信息的方法
public void refreshCount() {
// 移除bottomPanel面板中之前添加的所有组件更新显示新的记录数
bottomPanel.removeAll();
countInfoLabel = new JLabel("总共"+userTableModel.getRowCount()+"条");
// 创建一个新的显示用户记录总数的标签,文本内容为"总共"加上通过用户表格数据模型获取的当前行数以及"条"字
countInfoLabel = new JLabel("总共" + userTableModel.getRowCount() + "条");
// 将新的记录数标签添加到bottomPanel面板中实现记录数提示信息的更新显示
bottomPanel.add(countInfoLabel);
}
}
}
Loading…
Cancel
Save