• 【JAVA】-- 简易超市管理系统(一)(实现思路+每步代码)


    简易超市管理系统用于管理超市商品的增删改查等操作,并且使用流式文件保存数据。

    主要学习其实现思想,掌握后可自行实现其他管理系统如:学生管理系统等。

    大致效果:

     

    一、构建窗口

    构建一个503×427的窗口,不可大小化,在屏幕正中央出现。

    创建主类MainFrame

    1. import javax.swing.*;
    2. import javax.swing.border.TitledBorder;
    3. import java.awt.*;
    4. import java.net.URL;
    5. public class MainFrame extends JFrame {
    6. public static void main(String[] args) {
    7. new MainFrame().setVisible(true);
    8. }
    9. public MainFrame() throws HeadlessException{//在不支持键盘、显示器或鼠标的环境中调用与键盘、显示器或鼠标有关的代码时,被抛出的异常。
    10. super();
    11. this.setTitle("简易超时管理系统");
    12. this.setSize(503,427);
    13. this.setResizable(false);//不可改变窗口大小
    14. this.setLocationRelativeTo(null);//设置窗口为屏幕正中央
    15. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//正常关闭
    16. }
    17. }

    二、构建面板组件

    构建一个JPanel面板组件,用于放置背景图片。

    创建BGPanel类

    使用网格包布局

    1. import javax.swing.*;
    2. import java.awt.*;
    3. public class BGPanel extends JPanel {
    4. private ImageIcon icon;//背景图片
    5. public BGPanel(){//主面板
    6. super();
    7. this.setSize(300,200);//界面大小
    8. this.setLayout(new GridBagLayout());//比较灵活的布局
    9. }
    10. public ImageIcon getIcon(){
    11. return icon;//将外部加载的图片返回
    12. }
    13. public void setIcon(ImageIcon icon){
    14. this.icon = icon;//接收外部传入的图片,并设置
    15. }
    16. protected void paintComponent(Graphics g){
    17. super.paintComponent(g);
    18. if(icon != null)
    19. g.drawImage(icon.getImage(),0,0,this);//接收到图片后,通过画笔绘制,在0,0点绘制,返回this当前界面
    20. }
    21. }

    三、设置背景图片

    主类MainFrame增加/修改

        private BGPanel bgPanel = null;
    this.setContentPane(getBGPanel());//添加组件
    

     

    1. private JPanel getBGPanel(){
    2. if(bgPanel == null){
    3. bgPanel = new BGPanel();//实例化背景类对象
    4. URL url = this.getClass().getResource("BGPanel.jpg");//拿到本文件下的图片资源
    5. ImageIcon icon = new ImageIcon(url);
    6. bgPanel.setIcon(icon);
    7. }
    8. return bgPanel;
    9. }

     四、构建标题部分

    先设置一个标题,后面的增删改查功能按钮设置在标题下面,此时只有一个实例对象,所以占整个窗口。

    主类MainFrame增加/修改

    网格包布局管理需要使用GridBagConstraints对象布局,

    gridx表示组件左上角所在的行列,容器中最左边列的 gridx 为 0,最上边行的 gridy 为 0。

    gridwidth和gridheight用来指定组件显示区域所占的列数和行数,以网格单元而不是像素为单位,默认值为 1。

    insets指定组件显示区域的外部填充,即组件与其显示区域边缘之间的空间,默认组件没有外部填充。

    先建立一个标签,然后设置好字体大小,将标签添加到布局中。

    1. GridBagConstraints gridBagConstraints= new GridBagConstraints();
    2. gridBagConstraints.gridx = 0;
    3. gridBagConstraints.gridy = 0;
    4. gridBagConstraints.gridwidth = 4;
    5. gridBagConstraints.insets = new Insets(15,0,0,0);
    6. JLabel label = new JLabel("简易超市管理系统");
    7. label.setFont(new Font("华文行楷",Font.BOLD,30));
    8. bgPanel.add(label,gridBagConstraints);

     五、增加“增、删、改、查”按钮

    创建按钮方法,设计出按钮样式,将按钮先组织到一个组件中,最后添加到网格包布局中。

    主类MainFrame增加/修改

    实例化按钮对象

    创建增删改查按钮方法,并设置按钮样式

    1. private JButton zengButton = null;
    2. private JButton shanButton = null;
    3. private JButton gaiButton = null;
    4. private JButton chaButton = null;
    1. private JButton getZengButton(){
    2. if(zengButton == null){
    3. zengButton = new JButton("增加内容");//实例化
    4. zengButton.setFont(new Font("华文行楷",Font.BOLD,20));
    5. zengButton.setOpaque(false);//设置组件透明
    6. zengButton.setBorderPainted(false);//设置按钮边界效果
    7. zengButton.setContentAreaFilled(false);//设置填充效果
    8. zengButton.setFocusPainted(false);//组件是否聚焦
    9. zengButton.addActionListener(new ActionListener() {
    10. @Override
    11. public void actionPerformed(ActionEvent e) {
    12. }
    13. });
    14. }
    15. return zengButton;
    16. }
    17. private JButton getShanButton(){
    18. if(shanButton == null){
    19. shanButton = new JButton("删除内容");//实例化
    20. shanButton.setFont(new Font("华文行楷",Font.BOLD,20));
    21. shanButton.setOpaque(false);//设置组件透明
    22. shanButton.setBorderPainted(false);//设置按钮边界效果
    23. shanButton.setContentAreaFilled(false);//设置填充效果
    24. shanButton.setFocusPainted(false);//组件是否聚焦
    25. shanButton.addActionListener(new ActionListener() {
    26. @Override
    27. public void actionPerformed(ActionEvent e) {
    28. }
    29. });
    30. }
    31. return shanButton;
    32. }
    33. private JButton getGaiButton(){
    34. if(gaiButton == null){
    35. gaiButton = new JButton("修改内容");//实例化
    36. gaiButton.setFont(new Font("华文行楷",Font.BOLD,20));
    37. gaiButton.setOpaque(false);//设置组件透明
    38. gaiButton.setBorderPainted(false);//设置按钮边界效果
    39. gaiButton.setContentAreaFilled(false);//设置填充效果
    40. gaiButton.setFocusPainted(false);//组件是否聚焦
    41. gaiButton.addActionListener(new ActionListener() {
    42. @Override
    43. public void actionPerformed(ActionEvent e) {
    44. }
    45. });
    46. }
    47. return gaiButton;
    48. }
    49. private JButton getChaButton(){
    50. if(chaButton == null){
    51. chaButton = new JButton("查找内容");//实例化
    52. chaButton.setFont(new Font("华文行楷",Font.BOLD,20));
    53. chaButton.setOpaque(false);//设置组件透明
    54. chaButton.setBorderPainted(false);//设置按钮边界效果
    55. chaButton.setContentAreaFilled(false);//设置填充效果
    56. chaButton.setFocusPainted(false);//组件是否聚焦
    57. chaButton.addActionListener(new ActionListener() {
    58. @Override
    59. public void actionPerformed(ActionEvent e) {
    60. }
    61. });
    62. }
    63. return chaButton;
    64. }

    getBGPanel()方法增加

    创建一个面板组件,将按钮添加进去,然后将此按钮组件添加到布局中。

    1. GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
    2. gridBagConstraints2.gridx = 0;
    3. gridBagConstraints2.gridy = 1;
    4. gridBagConstraints2.gridwidth = 4;
    5. gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;//横向充满显示区域
    6. gridBagConstraints2.insets = new Insets(0,0,0,0);
    7. JPanel panel = new JPanel();
    8. panel.setOpaque(false);//设置组件透明,可见背景图片
    9. panel.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
    10. //将功能按钮添加到面板上
    11. panel.add(getZengButton());
    12. panel.add(getShanButton());
    13. panel.add(getGaiButton());
    14. panel.add(getChaButton());
    15. bgPanel.add(panel,gridBagConstraints2);

  • 相关阅读:
    lodash学习
    提升绘图效率不再难,看看这8款AI流程图软件,一键快速生成流程图!
    Excel文档名称批量翻译的高效方法
    golang语言_2
    [C++11] --- 移动语义和完美转发
    27 - Excel 的基本公式和重要函数
    常见web安全及防护原理
    jvm02
    本周内容整理
    QT内嵌浏览器与JS通讯
  • 原文地址:https://blog.csdn.net/Tir_zhang/article/details/125407009