当前位置:   article > 正文

Java期末大作业!!用JFormDesiner和jdbc实现一个具有简单的ui界面的鲜花管理系统!!_jformdesigner教程登录界面

jformdesigner教程登录界面

目录

一、JFormDesiner,可视化的窗口设计工具

1.JFormDesiner插件的安装

2.创建一个JFormDesigner Form项目

 2.实现的界面效果

 二、数据库的建立

1.用户表(s_admin)

2、商品表(s_stock)

三、代码实现

1、工具类代码

(一)JdbcUtils,实现与数据库的连接

(二)图片工具类PicUtil

(三)Tools类,弹出窗体的实现

(四)Table类,实现一个简单的表格

2.界面代码

(一)登录界面

(二)注册界面

(三)Manager页面

(四)查找商品

(五)更改商品

(六)修改密码界面

(七)表格形式查找商品 


一、JFormDesiner,可视化的窗口设计工具

本人采用的编译器是idea,JFormDesiner相当于Eclipse中的Windowsbuilder,JformDesiner是一个插件,能够设计自己的界面并且形成界面的代码,但是javaswing比较老旧,所以只是初学的一种工具。建议使用大于11的JDK版本,不然可能无法编译成功!!

1.JFormDesiner插件的安装

File->settings->plugins,在搜索栏中搜索JFormDesigner安装,这个插件是需要付费的,可以申请20天的免费试用期,或者自己去搜索破解方法。

实用申请网址:Get Evaluation License Key | JFormDesigner - Java/Swing GUI Designer

2.创建一个JFormDesigner Form项目

安装完成后,创建项目时就可以创建出一个Form窗体进行设计

name处随便起一个名字,Superclass处选择JFrame,按钮出选择none,布局选择null Layout,创建好后就可以开始设计了。

 2.实现的界面效果

1.登录界面

 2.注册界面

 3.管理界面

此处的图片只是简单的贴图,想要实现从数据库的调取,还有点麻烦,我也只是简单的实现了一下,也只是为了好看。

4.修改密码

 5.增加商品

在点击加号的label的时候,会弹出文件选择器,具体的会在下文讲解。

 6.查找商品

 7.表格的形式查找所有商品和增加库存

8.修改商品

 二、数据库的建立

1.用户表(s_admin)

字段名称

数据类型及长度

允许为空

主键

字段说明

s_account

Varchar(20)

N

账号

s_password

Varchar(20)

Y

密码

s_name

Varchar(20)

Y

姓名

s_adress

Varchar(40)

Y

住址

s_email

Varchar(20)

Y

邮箱

s_pow

Varchar(20)

Y

权限0代表普通员工 1代表管理员

                                用户(账号,密码,姓名,住址,邮箱,用户权限)

2、商品表(s_stock)

字段名称

数据类型及长度

允许为空

主键

字段说明

s_id

Varchar(20)

N

商品编码

s_name

Varchar(20)

N

商品名字

s_price

Decimal(10)

Y

商品价格

s_num

Int

N

数量

s_photona

Varchar(255)

Y

图片名字

s_path

Longblob(20)

Y

存的图片

                      商品(商品编码,商品名,商品价格,数量,图片名,图片存储路径)

三、代码实现

本处只会粘贴监听事件的代码,具体的swing框架过长,主要实现的是通过监听事件与数据库建立的连接

1、工具类代码

(一)JdbcUtils,实现与数据库的连接

  1. import java.sql.*;
  2. public class JdbcUtils {
  3. private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
  4. private static final String URL = "jdbc:mysql://localhost:3306/jdbc?useSSL=false";
  5. private static final String USER = "root";
  6. private static final String PASSWORD = "18342003";
  7. //注册驱动程序放在代码块中,每次只能注册一次
  8. static {
  9. try {
  10. Class.forName(DRIVER);
  11. } catch (ClassNotFoundException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. //创建getConnection对象,用来获得connection对象
  16. public static Connection getConnection(){
  17. Connection conn = null;
  18. try {
  19. conn = DriverManager.getConnection(URL,USER,PASSWORD);
  20. }catch (SQLException e){
  21. e.printStackTrace();
  22. }
  23. return conn;
  24. }
  25. //创建free方法实现关闭连接功能
  26. //两个close用于方法重载
  27. public static void close(Statement st, Connection conn) {
  28. if (st != null) {
  29. try {
  30. st.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. } finally {
  34. if (conn != null) {
  35. try {
  36. conn.close();
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. }
  43. }
  44. public static void close(ResultSet rs, Statement st, Connection conn) {
  45. if (rs != null) {
  46. try {
  47. rs.close();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. } finally {
  51. if (st != null) {
  52. try {
  53. st.close();
  54. } catch (SQLException e) {
  55. e.printStackTrace();
  56. } finally {
  57. if (conn != null) {
  58. try {
  59. conn.close();
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }

(二)图片工具类PicUtil

主要实现图片的转换和缩放,根据路径获得ImageIcon和把BufferImage转换成ImageIcon

  1. import javax.imageio.ImageIO;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. public class PicUtils {
  10. public static ImageIcon ZoomBySize(String srcFile,int width,int height)throws IOException{
  11. BufferedImage img = ImageIO.read(new File(srcFile));
  12. Image _img = img.getScaledInstance(width,height,Image.SCALE_DEFAULT);
  13. BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  14. Graphics2D graphics = image.createGraphics();
  15. graphics.drawImage(_img,0,0,null);
  16. graphics.dispose();
  17. //获取到了图片流 编程imageIcon变量
  18. ImageIcon icon = new ImageIcon(image);//转换格式,将图片流转换成icon
  19. return icon;
  20. }
  21. public static ImageIcon ZoomBySize(BufferedImage img,int width,int height)throws IOException{
  22. Image _img = img.getScaledInstance(width,height,Image.SCALE_DEFAULT);
  23. BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  24. Graphics2D graphics = image.createGraphics();
  25. graphics.drawImage(_img,0,0,null);
  26. graphics.dispose();
  27. //获取到了图片流 编程imageIcon变量
  28. ImageIcon icon = new ImageIcon(image);//转换格式,将图片流转换成icon
  29. return icon;
  30. }
  31. }

(三)Tools类,弹出窗体的实现

  1. import java.awt.Component;
  2. import java.awt.Dimension;
  3. import java.awt.Toolkit;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import javax.swing.JFrame;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JPasswordField;
  9. import javax.swing.table.DefaultTableModel;
  10. public class Tools {
  11. public Tools() {
  12. }
  13. public static void setWindowPos(int WIDTH, int HEIGHT, JFrame jframe) {
  14. Toolkit kit = Toolkit.getDefaultToolkit();
  15. Dimension screenSize = kit.getScreenSize();
  16. int width = screenSize.width;
  17. int height = screenSize.height;
  18. int x = (width - WIDTH) / 2;
  19. int y = (height - HEIGHT) / 2;
  20. jframe.setBounds(x, y, WIDTH, HEIGHT);
  21. }
  22. public static String getPassword(JPasswordField jp) {
  23. String password = new String(jp.getPassword());
  24. return password;
  25. }
  26. public static void messageWindows(String msg) {
  27. JOptionPane.showMessageDialog((Component)null, msg, "消息", 2);
  28. }
  29. public static int addDataTable(ResultSet rs, DefaultTableModel model, int index) {
  30. int count = 0;
  31. model.setNumRows(0);
  32. String[] data = new String[index];
  33. try {
  34. while(rs.next()) {
  35. ++count;
  36. for(int i = 0; i < data.length; ++i) {
  37. data[i] = rs.getString(i + 1);
  38. }
  39. model.addRow(data);
  40. }
  41. rs.close();
  42. return count;
  43. } catch (SQLException var6) {
  44. var6.printStackTrace();
  45. return count;
  46. }
  47. }
  48. }

(四)Table类,实现一个简单的表格

  1. import javax.swing.JScrollPane;
  2. import javax.swing.JTable;
  3. import javax.swing.table.DefaultTableModel;
  4. public class Table {
  5. JTable tableL = null;
  6. JScrollPane jscrollpane;
  7. DefaultTableModel model = null;
  8. public Table(Object[] columns) {
  9. this.Table(columns);
  10. }
  11. void Table(Object[] columns) {
  12. this.tableL = this.getTable(columns);
  13. this.jscrollpane = new JScrollPane(this.tableL);
  14. this.jscrollpane.setVerticalScrollBarPolicy(20);
  15. }
  16. JTable getTable(Object[] columns) {
  17. if (this.tableL == null) {
  18. this.tableL = new JTable();
  19. this.model = new DefaultTableModel() {
  20. public boolean isCellEditable(int row, int column) {
  21. return false;
  22. }
  23. };
  24. this.model.setColumnIdentifiers(columns);
  25. this.tableL.setModel(this.model);
  26. this.tableL.getTableHeader().setReorderingAllowed(false);
  27. this.tableL.getTableHeader().setResizingAllowed(false);
  28. }
  29. return this.tableL;
  30. }
  31. public JTable getTables() {
  32. return this.tableL;
  33. }
  34. public JScrollPane getJScrollPane() {
  35. return this.jscrollpane;
  36. }
  37. public DefaultTableModel getModel() {
  38. return this.model;
  39. }
  40. }

2.界面代码

(一)登录界面

  1. {
  2. public test2() {
  3. initComponents();
  4. }
  5. private void initComponents() {
  6. // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
  7. // Generated using JFormDesigner Evaluation license - 刘
  8. button1 = new JButton();
  9. textField1 = new JTextField();
  10. label1 = new JLabel();
  11. passwordField1 = new JPasswordField();
  12. label2 = new JLabel();
  13. label3 = new JLabel();
  14. label6 = new JLabel();
  15. label5 = new JLabel();
  16. //======== this ========
  17. setForeground(SystemColor.textHighlight);
  18. setIconImage(new ImageIcon(getClass().getResource("/image/login.png")).getImage());
  19. setTitle("鲜花管理系统登录界面");
  20. setBackground(new Color(0xccffff));
  21. var contentPane = getContentPane();
  22. contentPane.setLayout(null);
  23. //---- button1 ----
  24. button1.setText("安全登录");
  25. button1.setBackground(new Color(0x99ffff));
  26. contentPane.add(button1);
  27. button1.setBounds(110, 195, 270, 45);
  28. contentPane.add(textField1);
  29. textField1.setBounds(195, 90, 155, 25);
  30. button1.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
  31. //获取账号 密码
  32. String Account = textField1.getText();
  33. String Password = Tools.getPassword(passwordField1);
  34. if(Account.equals("")){
  35. Tools.messageWindows("账号不能为空");
  36. }else if(Password.equals("")){
  37. Tools.messageWindows("密码不能为空");
  38. }else{
  39. String []date = {
  40. Account,
  41. Password
  42. };
  43. try{
  44. Connection conn = JdbcUtils.getConnection();
  45. PreparedStatement pst = conn.prepareStatement("select *from s_admin where s_account = ? and s_password = ?");
  46. pst.setString(1,date[0]);
  47. pst.setString(2,date[1]);
  48. ResultSet rs = pst.executeQuery();
  49. if(rs.next()){
  50. Tools.messageWindows("登录成功");
  51. if(rs.getString(6).equals("0")){
  52. Tools.messageWindows("普通员工登录成功");
  53. new Manager(Account).setVisible(true);
  54. dispose();
  55. }else{
  56. Tools.messageWindows("管理员登录成功");
  57. new Manager(Account).setVisible(true);
  58. dispose();
  59. }
  60. }else{
  61. Tools.messageWindows("账号或密码错误");
  62. }
  63. }catch (SQLException ex){
  64. ex.printStackTrace();
  65. }
  66. }
  67. }});
  68. //---- label1 ----
  69. label1.setText("\u7528\u6237\u540d");
  70. label1.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
  71. contentPane.add(label1);
  72. label1.setBounds(135, 85, label1.getPreferredSize().width, 32);
  73. contentPane.add(passwordField1);
  74. passwordField1.setBounds(195, 125, 155, 25);
  75. //---- label2 ----
  76. label2.setText("\u5bc6\u7801");
  77. label2.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
  78. contentPane.add(label2);
  79. label2.setBounds(135, 125, 42, label2.getPreferredSize().height);
  80. //---- label3 ----
  81. label3.setText("\u9c9c\u82b1\u7ba1\u7406\u7cfb\u7edf");
  82. label3.setPreferredSize(new Dimension(99, 22));
  83. label3.setHorizontalAlignment(SwingConstants.CENTER);
  84. label3.setHorizontalTextPosition(SwingConstants.CENTER);
  85. label3.setIconTextGap(20);
  86. label3.setFont(label3.getFont().deriveFont(label3.getFont().getStyle() | Font.BOLD, label3.getFont().getSize() + 12f));
  87. contentPane.add(label3);
  88. label3.setBounds(80, 0, 330, 50);
  89. //---- label6 ----
  90. label6.setText("注册>");
  91. contentPane.add(label6);
  92. label6.setBounds(new Rectangle(new Point(440, 240), label6.getPreferredSize()));
  93. label6.addMouseListener(new MouseListener() {@Override public void mouseClicked(MouseEvent e) {
  94. new Regest1().setVisible(true);
  95. }@Override public void mousePressed(MouseEvent e) {
  96. }@Override public void mouseReleased(MouseEvent e) {
  97. }@Override public void mouseEntered(MouseEvent e) {
  98. }@Override public void mouseExited(MouseEvent e) {
  99. }});
  100. //---- label5 ----
  101. label5.setIcon(new ImageIcon(getClass().getResource("/image/background.jpg")));
  102. contentPane.add(label5);
  103. label5.setBounds(0, -5, 500, 295);
  104. {
  105. // compute preferred size
  106. Dimension preferredSize = new Dimension();
  107. for(int i = 0; i < contentPane.getComponentCount(); i++) {
  108. Rectangle bounds = contentPane.getComponent(i).getBounds();
  109. preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  110. preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  111. }
  112. Insets insets = contentPane.getInsets();
  113. preferredSize.width += insets.right;
  114. preferredSize.height += insets.bottom;
  115. contentPane.setMinimumSize(preferredSize);
  116. contentPane.setPreferredSize(preferredSize);
  117. }
  118. setSize(501, 331);
  119. setLocationRelativeTo(getOwner());
  120. // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
  121. }
  122. // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
  123. // Generated using JFormDesigner Evaluation license - 刘
  124. private JButton button1;
  125. private JTextField textField1;
  126. private JLabel label1;
  127. private JPasswordField passwordField1;
  128. private JLabel label2;
  129. private JLabel label3;
  130. private JLabel label6;
  131. private JLabel label5;
  132. // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
  133. //登录账号
  134. }

(二)注册界面

  1. button1.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
  2. String Account = textField1.getText(); // 账户
  3. String Password = textField2.getText();//密码
  4. String Name = textField3.getText();//姓名
  5. String Adress = textField4.getText();//地址
  6. String Email = textField5.getText();//邮箱
  7. String pow = "0";//权限
  8. s_admin admain = new s_admin();
  9. admain.setAccount(Account);
  10. admain.setPassword(Password);
  11. admain.setName(Name);
  12. admain.setEmail(Email);
  13. admain.setEmail(Email);
  14. if(Account.equals("")){
  15. Tools.messageWindows("请输入账号");
  16. }else if(Password.equals("")){
  17. Tools.messageWindows("请输入密码");
  18. }else if(Name.equals("")){
  19. Tools.messageWindows("请输入姓名");
  20. }else if(Adress.equals("")){
  21. Tools.messageWindows("请输入地址");
  22. }else if(Email.equals("")){
  23. Tools.messageWindows("请输入邮箱");
  24. if(Email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$\n")){
  25. Tools.messageWindows("请输入正确格式的邮箱号码");
  26. }
  27. }
  28. else{
  29. //将注册的数据写入到数据库中
  30. try{
  31. Connection conn = JdbcUtils.getConnection();
  32. int i = addAdmin(conn,admain);
  33. if(i==2){
  34. Tools.messageWindows("账户名已存在,请重新注册");
  35. }else if(i==0){
  36. Tools.messageWindows("注册失败");
  37. } else{
  38. Tools.messageWindows("注册成功");
  39. dispose();
  40. }
  41. }catch (SQLException ex){
  42. ex.printStackTrace();
  43. }
  44. }
  45. }});
  46. public int addAdmin(Connection conn,s_admin admin) throws SQLException {
  47. //检查用户名是否存在
  48. conn = JdbcUtils.getConnection();
  49. String sql = "select * from s_admin where s_account = ?";
  50. PreparedStatement pst = conn.prepareStatement(sql);
  51. pst.setString(1,admin.getAccount());
  52. ResultSet rs = pst.executeQuery();
  53. if(rs.next()){
  54. return 2;
  55. }
  56. PreparedStatement pst1 = conn.prepareStatement("insert into s_admin values (?,?,?,?,?,?)");
  57. pst1.setString(1,admin.getAccount());
  58. pst1.setString(2,admin.getPassword());
  59. pst1.setString(3,admin.getName());
  60. pst1.setString(4,admin.getAdress());
  61. pst1.setString(5,admin.getEmail());
  62. pst1.setString(6,"0");
  63. return pst1.executeUpdate();
  64. }
  65. }

(三)Manager页面

  1. public class Manager extends JFrame {
  2. public Manager(String Account) {
  3. initComponents();
  4. this.Account = Account;
  5. }
  6. String Account;
  7. private void menuItem2(ActionEvent e) {
  8. dispose();
  9. //修改自己的密码,打开一个新窗口,输入原来的密码,再输入新的密码
  10. ChangePassword1 changePassword1 = new ChangePassword1(Account);
  11. initComponents();
  12. changePassword1.setVisible(true);
  13. }
  14. private void menuItem3(ActionEvent e) {
  15. dispose();
  16. new test2().setVisible(true);
  17. }
  18. private void button1(ActionEvent e) {
  19. //添加商品,跳出来一个窗口,进行添加
  20. new addStockView().setVisible(true);
  21. }
  22. private void menuItem1(ActionEvent e) {
  23. try{
  24. Connection conn = JdbcUtils.getConnection();
  25. PreparedStatement pst = conn.prepareStatement(" delete from s_admin where s_account = ?");
  26. pst.setString(1,Account);
  27. pst.executeUpdate();
  28. int a = pst.executeUpdate();
  29. if(a==0||a==-1){
  30. Tools.messageWindows("注销成功");
  31. dispose();
  32. new test2().setVisible(true);
  33. }else{
  34. Tools.messageWindows("注销失败");
  35. //更改密码成功,返回登录界面
  36. }}catch (SQLException ex){
  37. ex.printStackTrace();
  38. }
  39. }
  40. private void button2(ActionEvent e) {
  41. String id = textField1.getText();//商品编号
  42. //从数据库中调取
  43. if (id.equals("")) {
  44. Tools.messageWindows("请输入编码");
  45. } else {
  46. try {
  47. Connection conn = JdbcUtils.getConnection();
  48. PreparedStatement pst = conn.prepareStatement("select * from jdbc.s_stock where s_id = ?");
  49. pst.setString(1, id);
  50. ResultSet rs = pst.executeQuery();
  51. if (rs.next()) {
  52. String s_id = rs.getString(1);
  53. String s_name = rs.getString(2);
  54. String s_price = rs.getString(3);
  55. String num = rs.getString(4);
  56. InputStream in = null;
  57. in = rs.getBinaryStream(6);
  58. byte[] b;
  59. BufferedImage img = null;
  60. try {
  61. b = new byte[in.available()];//定义字节储存图片
  62. in.read(b);
  63. InputStream buffin = new ByteArrayInputStream(b, 0, b.length);
  64. img = ImageIO.read(buffin);
  65. ImageIcon icon = PicUtils.ZoomBySize(img, 180, 180);
  66. new findStock1(s_id, s_name, num, s_price, icon).setVisible(true);
  67. } catch (IOException ex1) {
  68. ex1.printStackTrace();
  69. }
  70. } else {
  71. Tools.messageWindows("未找到该商品");
  72. }
  73. } catch (SQLException ex) {
  74. ex.printStackTrace();
  75. }
  76. setVisible(true);
  77. }
  78. }

(四)查找商品

  1. import com.tools.Tools;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.image.BufferedImage;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8. import java.util.*;
  9. import javax.swing.*;
  10. /**
  11. * @author LENOVO
  12. */
  13. public class findStock1 extends JFrame {
  14. public findStock1(String id, String name, String price, String num, ImageIcon icon) {
  15. this.id = id;
  16. this.name = name;
  17. this.price = price;
  18. this.num = num;
  19. this.icon = icon;
  20. initComponents();
  21. }
  22. private void button1(ActionEvent e) {
  23. //连接数据库进行删除
  24. try{
  25. Connection conn = JdbcUtils.getConnection();
  26. PreparedStatement pst = conn.prepareStatement("delete from jdbc.s_stock where jdbc.s_stock.s_id = ?");
  27. pst.setString(1,id);
  28. pst.executeUpdate();
  29. dispose();
  30. Tools.messageWindows("删除成功");
  31. JdbcUtils.close(pst,conn);
  32. }catch (SQLException ex){
  33. ex.printStackTrace();
  34. }
  35. }
  36. private void button2(ActionEvent e) {
  37. dispose();
  38. new ModifyStock(id,name,price,icon).setVisible(true);
  39. }
  40. String id;
  41. String name;
  42. String price;
  43. String num;
  44. ImageIcon icon;
  45. private void initComponents() {
  46. // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
  47. // Generated using JFormDesigner Evaluation license - 刘
  48. setAlwaysOnTop(true);
  49. ResourceBundle bundle = ResourceBundle.getBundle("Flower.Flower");
  50. label1 = new JLabel();
  51. label2 = new JLabel();
  52. label5 = new JLabel();
  53. button1 = new JButton();
  54. button2 = new JButton();
  55. label6 = new JLabel();
  56. label7 = new JLabel();
  57. //======== this ========
  58. var contentPane = getContentPane();
  59. contentPane.setLayout(null);
  60. contentPane.add(label1);
  61. label1.setBounds(5, 5, 180, 180);
  62. label1.setIcon(icon);
  63. //---- label2 ----
  64. label2.setText("商品编码:"+id);
  65. contentPane.add(label2);
  66. label2.setBounds(195, 10, 140, label2.getPreferredSize().height);
  67. //---- label5 ----
  68. label5.setText("商品名称"+name);
  69. contentPane.add(label5);
  70. label5.setBounds(190, 45, 145, label5.getPreferredSize().height);
  71. //---- button1 ----
  72. button1.setText("\u5220\u9664");
  73. contentPane.add(button1);
  74. button1.setBounds(190, 155, 70, 30);
  75. button1.addActionListener(this::button1);
  76. //---- button2 ----
  77. button2.setText("\u66f4\u6539");
  78. contentPane.add(button2);
  79. button2.setBounds(275, 155, 75, 30);
  80. button2.addActionListener(this::button2);
  81. //---- label6 ----
  82. label6.setText("商品价格:"+price);
  83. contentPane.add(label6);
  84. label6.setBounds(195, 80, 145, 22);
  85. //---- label7 ----
  86. label7.setText("商品库存:"+num);
  87. contentPane.add(label7);
  88. label7.setBounds(195, 115, 145, 22);
  89. {
  90. // compute preferred size
  91. Dimension preferredSize = new Dimension();
  92. for(int i = 0; i < contentPane.getComponentCount(); i++) {
  93. Rectangle bounds = contentPane.getComponent(i).getBounds();
  94. preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  95. preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  96. }
  97. Insets insets = contentPane.getInsets();
  98. preferredSize.width += insets.right;
  99. preferredSize.height += insets.bottom;
  100. contentPane.setMinimumSize(preferredSize);
  101. contentPane.setPreferredSize(preferredSize);
  102. }
  103. pack();
  104. setLocationRelativeTo(getOwner());
  105. // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
  106. }
  107. // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
  108. // Generated using JFormDesigner Evaluation license - 刘
  109. private JLabel label1;
  110. private JLabel label2;
  111. private JLabel label5;
  112. private JButton button1;
  113. private JButton button2;
  114. private JLabel label6;
  115. private JLabel label7;
  116. // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
  117. }

(五)更改商品

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.util.*;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.filechooser.FileNameExtensionFilter;
  11. import javax.swing.filechooser.FileSystemView;
  12. import com.jgoodies.forms.factories.*;
  13. import com.tools.Tools;
  14. /**
  15. * @author LENOVO
  16. */
  17. public class ModifyStock extends JFrame {
  18. String id;
  19. String name;
  20. String price;
  21. ImageIcon icon;
  22. int start = 1;//当动图片时start自增,看看是不是第一次执行
  23. public ModifyStock(String id, String name, String price,ImageIcon icon) {
  24. this.id = id;
  25. this.name = name;
  26. this.price = price;
  27. this.icon = icon;
  28. this.start = 1;
  29. initComponents();
  30. }
  31. String path = null;//图片路径
  32. private void label5MouseClicked(MouseEvent e) {
  33. this.path= getSelectPath();
  34. if(path != null){
  35. //用path进行读取存入数据库
  36. Tools.messageWindows("成功读取");
  37. label5.setIcon(new ImageIcon(path));
  38. start++;
  39. }
  40. }
  41. private void label6MouseClicked(MouseEvent e) {
  42. Tools.messageWindows("您删除了图片");
  43. start++;
  44. path = null;
  45. label5.setIcon(new ImageIcon(getClass().getResource("/image/\u52a0\u53f7 (2).png")));
  46. }
  47. private void button1(ActionEvent e){
  48. String id = textField1.getText();
  49. String name = textField2.getText();
  50. String price = textField3.getText();
  51. //图片的路径
  52. String filename = null;
  53. if(start==1){
  54. //第一次执行没有动图片
  55. }else{
  56. if(path!=null) {
  57. File tempFile = new File(path.trim());//提取路径中的最后一个斜杠
  58. filename = tempFile.getName();
  59. }
  60. }
  61. InputStream in = null;//用于将图片读取出来
  62. String picName;
  63. String path1 = path;//路径;
  64. if(start==1) {
  65. //没有动图片
  66. if (id.equals("")) {
  67. Tools.messageWindows("请输入编码");
  68. } else if (name.equals("")) {
  69. Tools.messageWindows("请输入商品名字");
  70. } else if (price.equals("")) {
  71. Tools.messageWindows("请输入价格");
  72. } else {
  73. try {
  74. Connection conn = JdbcUtils.getConnection();
  75. PreparedStatement pst = conn.prepareStatement("update jdbc.s_stock set s_name = ?,jdbc.s_stock.s_price = ? where s_id = ?");
  76. pst.setString(3, id);
  77. pst.setString(1, name);
  78. pst.setString(2, price);
  79. int a = pst.executeUpdate();//执行更新语句
  80. if (a == 0 || a == -1) {
  81. Tools.messageWindows("更改失败");
  82. } else {
  83. Tools.messageWindows("更新成功");
  84. dispose();
  85. }
  86. } catch (SQLException ex) {
  87. ex.printStackTrace();
  88. }
  89. }
  90. }
  91. else {
  92. //动了图片
  93. if (id.equals("")) {
  94. Tools.messageWindows("请输入编码");
  95. } else if (name.equals("")) {
  96. Tools.messageWindows("请输入商品名字");
  97. } else if (price.equals("")) {
  98. Tools.messageWindows("请输入价格");
  99. } else if (path1==null) {
  100. Tools.messageWindows("请添加图片");
  101. } else {
  102. try {
  103. Connection conn = JdbcUtils.getConnection();
  104. PreparedStatement pst = conn.prepareStatement("update jdbc.s_stock set s_name = ?,jdbc.s_stock.s_price = ?,jdbc.s_stock.s_photona = ?,jdbc.s_stock.s_path=? where s_id = ?");
  105. in = new FileInputStream(path);
  106. pst.setString(5, id);
  107. pst.setString(1, name);
  108. pst.setString(2, price);
  109. pst.setString(3, filename);
  110. pst.setBinaryStream(4, in, in.available());
  111. int a = pst.executeUpdate();//执行更新语句
  112. in.close();
  113. if (a == 0 || a == -1) {
  114. Tools.messageWindows("更改失败");
  115. } else {
  116. Tools.messageWindows("更新成功");
  117. dispose();
  118. }
  119. } catch (SQLException | IOException ex) {
  120. ex.printStackTrace();
  121. }
  122. }
  123. }
  124. }
  125. private void initComponents() {
  126. // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
  127. // Generated using JFormDesigner Evaluation license - 刘
  128. ResourceBundle bundle = ResourceBundle.getBundle("Flower.Flower");
  129. label1 = new JLabel();
  130. textField1 = new JTextField();
  131. label2 = new JLabel();
  132. textField2 = new JTextField();
  133. label3 = new JLabel();
  134. textField3 = new JTextField();
  135. label4 = new JLabel();
  136. label5 = new JLabel();
  137. button1 = new JButton();
  138. label6 = new JLabel();
  139. //======== this ========
  140. setTitle(bundle.getString("addStockView.this.title"));
  141. setIconImage(new ImageIcon(getClass().getResource("/image/login.png")).getImage());
  142. var contentPane = getContentPane();
  143. contentPane.setLayout(null);
  144. //---- label1 ----
  145. label1.setText(bundle.getString("addStockView.label1.text"));
  146. contentPane.add(label1);
  147. label1.setBounds(15, 30, 70, label1.getPreferredSize().height);
  148. contentPane.add(textField1);
  149. textField1.setEditable(false);
  150. textField1.setText(id);
  151. textField1.setBounds(100, 25, 90, 30);
  152. //---- label2 ----
  153. label2.setText(bundle.getString("addStockView.label2.text"));
  154. contentPane.add(label2);
  155. label2.setBounds(15, 80, 70, label2.getPreferredSize().height);
  156. contentPane.add(textField2);
  157. textField2.setText(name);
  158. textField2.setBounds(100, 80, 90, 30);
  159. //---- label3 ----
  160. label3.setText(bundle.getString("addStockView.label3.text"));
  161. contentPane.add(label3);
  162. label3.setBounds(new Rectangle(new Point(15, 130), label3.getPreferredSize()));
  163. contentPane.add(textField3);
  164. textField3.setText(price);
  165. textField3.setBounds(100, 130, 90, 30);
  166. //---- label4 ----
  167. label4.setText(bundle.getString("addStockView.label4.text"));
  168. contentPane.add(label4);
  169. label4.setBounds(new Rectangle(new Point(15, 180), label4.getPreferredSize()));
  170. //---- label5 ----
  171. //label5.setIcon(new ImageIcon(getClass().getResource("/image/\u52a0\u53f7 (2).png")));
  172. label5.setIcon(icon);
  173. label5.setBorder(LineBorder.createBlackLineBorder());
  174. label5.addMouseListener(new MouseAdapter() {
  175. @Override
  176. public void mouseClicked(MouseEvent e) {
  177. label5MouseClicked(e);
  178. }
  179. });
  180. contentPane.add(label5);
  181. label5.setBounds(20, 230, 135, 140);
  182. //---- button1 ----
  183. button1.setText("更改商品");
  184. button1.addActionListener(e -> button1(e));
  185. contentPane.add(button1);
  186. button1.setBounds(140, 415, 145, button1.getPreferredSize().height);
  187. //---- label6 ----
  188. label6.setIcon(new ImageIcon(getClass().getResource("/image/\u5220\u9664 (1).png")));
  189. label6.addMouseListener(new MouseAdapter() {
  190. @Override
  191. public void mouseClicked(MouseEvent e) {
  192. label6MouseClicked(e);
  193. }
  194. });
  195. contentPane.add(label6);
  196. label6.setBounds(75, 365, 30, 30);
  197. contentPane.setPreferredSize(new Dimension(343, 416));
  198. setSize(429, 520);
  199. setLocationRelativeTo(getOwner());
  200. // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
  201. }
  202. // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
  203. // Generated using JFormDesigner Evaluation license - 刘
  204. private JLabel label1;
  205. private JTextField textField1;
  206. private JLabel label2;
  207. private JTextField textField2;
  208. private JLabel label3;
  209. private JTextField textField3;
  210. private JLabel label4;
  211. private JLabel label5;
  212. private JButton button1;
  213. private JLabel label6;
  214. // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
  215. public static String getSelectPath(){
  216. JFileChooser jfs = new JFileChooser(FileSystemView.getFileSystemView());//打开目录不一样
  217. jfs.setDialogTitle("选择图片");
  218. jfs.setAcceptAllFileFilterUsed(false);//过滤器
  219. FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG","png");
  220. FileNameExtensionFilter filter1 = new FileNameExtensionFilter("JPG","jpg");
  221. //过滤png和jpg的图片
  222. jfs.addChoosableFileFilter(filter);
  223. jfs.addChoosableFileFilter(filter1);
  224. int returnValue = jfs.showOpenDialog(null);//打开文件选择器
  225. String path = null;
  226. if(returnValue == JFileChooser.APPROVE_OPTION){
  227. //获取到了
  228. path = jfs.getSelectedFile().getPath();
  229. }
  230. return path;
  231. }
  232. }

(六)修改密码界面

  1. import com.tools.Tools;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8. import java.util.*;
  9. import javax.swing.*;
  10. /**
  11. * @author LENOVO
  12. */
  13. public class ChangePassword1 extends JFrame {
  14. public ChangePassword1(String Account) {
  15. this.Account = Account;
  16. initComponents();
  17. }
  18. String Account;
  19. private void initComponents() {
  20. // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:off
  21. // Generated using JFormDesigner Evaluation license - 刘
  22. ResourceBundle bundle = ResourceBundle.getBundle("Flower.Flower");
  23. label1 = new JLabel();
  24. passwordField1 = new JPasswordField();
  25. label2 = new JLabel();
  26. passwordField2 = new JPasswordField();
  27. label3 = new JLabel();
  28. passwordField3 = new JPasswordField();
  29. button1 = new JButton();
  30. //======== this ========
  31. setTitle(bundle.getString("ChangePassword.this.title"));
  32. setIconImage(new ImageIcon(getClass().getResource("/image/\u4fee\u6539\u8d26\u53f7\u5bc6\u7801.png")).getImage());
  33. var contentPane = getContentPane();
  34. contentPane.setLayout(null);
  35. //---- label1 ----
  36. label1.setText(bundle.getString("ChangePassword.label1.text"));
  37. contentPane.add(label1);
  38. label1.setBounds(new Rectangle(new Point(5, 15), label1.getPreferredSize()));
  39. contentPane.add(passwordField1);
  40. passwordField1.setBounds(0, 40, 450, passwordField1.getPreferredSize().height);
  41. //---- label2 ----
  42. label2.setText(bundle.getString("ChangePassword.label2.text"));
  43. contentPane.add(label2);
  44. label2.setBounds(new Rectangle(new Point(5, 100), label2.getPreferredSize()));
  45. contentPane.add(passwordField2);
  46. passwordField2.setBounds(0, 130, 455, passwordField2.getPreferredSize().height);
  47. //---- label3 ----
  48. label3.setText(bundle.getString("ChangePassword.label3.text"));
  49. contentPane.add(label3);
  50. label3.setBounds(new Rectangle(new Point(5, 180), label3.getPreferredSize()));
  51. contentPane.add(passwordField3);
  52. passwordField3.setBounds(0, 210, 450, passwordField3.getPreferredSize().height);
  53. //---- button1 ----
  54. button1.setText(bundle.getString("ChangePassword.button1.text"));
  55. contentPane.add(button1);
  56. button1.setBounds(5, 295, 445, button1.getPreferredSize().height);
  57. button1.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
  58. //查看旧密码和老密码是否一致
  59. //更改密码
  60. String olps = Tools.getPassword(passwordField1);
  61. String nlps = Tools.getPassword(passwordField2);
  62. String onlps = Tools.getPassword(passwordField3);
  63. if(olps.equals("")){
  64. Tools.messageWindows("旧密码为空");
  65. }else if(nlps.equals("")){
  66. Tools.messageWindows("新密码 不能为空");
  67. } else if(onlps.equals("")){
  68. Tools.messageWindows("确认密码不能为空");
  69. } else if(!nlps.equals(onlps)){
  70. Tools.messageWindows("新密码和确认密码不一致");
  71. }else{
  72. //更改密码
  73. String []data = {
  74. nlps,
  75. Account,
  76. olps
  77. };
  78. try{
  79. Connection conn = JdbcUtils.getConnection();
  80. PreparedStatement pst = conn.prepareStatement("update s_admin set s_password = ? where s_account = ? and s_password = ?");
  81. pst.setString(1,nlps);
  82. pst.setString(2,Account);
  83. pst.setString(3,olps);
  84. int a = pst.executeUpdate();
  85. if(a==0||a==-1){
  86. Tools.messageWindows("账号不存在或者密码错误");
  87. }else{
  88. Tools.messageWindows("密码更改成功");
  89. //更改密码成功,返回登录界面
  90. dispose();
  91. new test2().setVisible(true);
  92. }
  93. }catch (SQLException ex){
  94. ex.printStackTrace();
  95. }
  96. }
  97. }});
  98. {
  99. // compute preferred size
  100. Dimension preferredSize = new Dimension();
  101. for(int i = 0; i < contentPane.getComponentCount(); i++) {
  102. Rectangle bounds = contentPane.getComponent(i).getBounds();
  103. preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
  104. preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
  105. }
  106. Insets insets = contentPane.getInsets();
  107. preferredSize.width += insets.right;
  108. preferredSize.height += insets.bottom;
  109. contentPane.setMinimumSize(preferredSize);
  110. contentPane.setPreferredSize(preferredSize);
  111. }
  112. pack();
  113. setLocationRelativeTo(getOwner());
  114. // JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on
  115. }
  116. // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:off
  117. // Generated using JFormDesigner Evaluation license - 刘
  118. private JLabel label1;
  119. private JPasswordField passwordField1;
  120. private JLabel label2;
  121. private JPasswordField passwordField2;
  122. private JLabel label3;
  123. private JPasswordField passwordField3;
  124. private JButton button1;
  125. // JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
  126. }

(七)表格形式查找商品 

      在button20(查找商品)中添加监听事件,在商品编码处为空时,显示所有的商品信息,在编码处有编码时,显示特定商品的信息,在textfild3中输入进货数量,点击进货后,商品数量增加,实现进货功能,两个功能都需要连接到数据库,显示并修改数据库中的数据,此处用到了table表格来显示,更加清晰。代码如下所示

  1. //定义一个表格
  2. Object []columns = {"商品编码","商品名字","商品价格","商品数量"};
  3. Table t1Table = new Table(columns);
  4. JTable table = t1Table.getTables();//获取表格
  5. JScrollPane JS = t1Table.getJScrollPane();//获取滚动条
  6. DefaultTableModel model = t1Table.getModel();//表格控制权限
  7. // JS.setPreferredSize(new Dimension(700,615));
  8. panel1.add(JS);
  9. JS.setBounds(5, 65, 700, 615);
  10. //---- button20 ----
  11. button20.setText(bundle.getString("Manager.button20.text"));
  12. button20.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {
  13. //查找商品
  14. if(textField2.getText().equals("")){
  15. //查找全部
  16. try{
  17. Connection conn = JdbcUtils.getConnection();
  18. Statement st = conn.createStatement();
  19. String sql = "select * from s_stock";
  20. ResultSet rs = st.executeQuery(sql);
  21. Tools.addDataTable(rs,model,4);
  22. }catch (SQLException ex){
  23. ex.printStackTrace();
  24. }
  25. }else{
  26. //查找单个
  27. String id = textField2.getText();
  28. try{
  29. Connection conn = JdbcUtils.getConnection();
  30. PreparedStatement pst = conn.prepareStatement("select * from jdbc.s_stock where s_id = ?");
  31. pst.setString(1,id);
  32. ResultSet rs = pst.executeQuery();
  33. Tools.addDataTable(rs,model,4);
  34. }catch (SQLException ex){
  35. ex.printStackTrace();
  36. }
  37. }
  38. }});
  39. panel2.add(button20);
  40. button20.setBounds(325, 5, 85, 35);
  41. //---- button21 ----
  42. button21.setText(bundle.getString("Manager.button21.text"));
  43. button21.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent e) {

以上只是部分代码,需要源码的可以私信联系我,喜欢或者有帮助的话可以点赞关注支持一下!!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/798547
推荐阅读
相关标签
  

闽ICP备14008679号