简易超市管理系统用于管理超市商品的增删改查等操作,并且使用流式文件保存数据。
主要学习其实现思想,掌握后可自行实现其他管理系统如:学生管理系统等。
大致效果:

构建一个503×427的窗口,不可大小化,在屏幕正中央出现。
- import javax.swing.*;
- import javax.swing.border.TitledBorder;
- import java.awt.*;
- import java.net.URL;
-
- public class MainFrame extends JFrame {
-
- public static void main(String[] args) {
- new MainFrame().setVisible(true);
- }
- public MainFrame() throws HeadlessException{//在不支持键盘、显示器或鼠标的环境中调用与键盘、显示器或鼠标有关的代码时,被抛出的异常。
- super();
- this.setTitle("简易超时管理系统");
- this.setSize(503,427);
- this.setResizable(false);//不可改变窗口大小
- this.setLocationRelativeTo(null);//设置窗口为屏幕正中央
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//正常关闭
- }
- }
构建一个JPanel面板组件,用于放置背景图片。
使用网格包布局
- import javax.swing.*;
- import java.awt.*;
-
- public class BGPanel extends JPanel {
- private ImageIcon icon;//背景图片
-
- public BGPanel(){//主面板
- super();
- this.setSize(300,200);//界面大小
- this.setLayout(new GridBagLayout());//比较灵活的布局
-
- }
- public ImageIcon getIcon(){
- return icon;//将外部加载的图片返回
- }
- public void setIcon(ImageIcon icon){
- this.icon = icon;//接收外部传入的图片,并设置
- }
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- if(icon != null)
- g.drawImage(icon.getImage(),0,0,this);//接收到图片后,通过画笔绘制,在0,0点绘制,返回this当前界面
- }
- }
private BGPanel bgPanel = null;
this.setContentPane(getBGPanel());//添加组件
- private JPanel getBGPanel(){
- if(bgPanel == null){
- bgPanel = new BGPanel();//实例化背景类对象
- URL url = this.getClass().getResource("BGPanel.jpg");//拿到本文件下的图片资源
- ImageIcon icon = new ImageIcon(url);
- bgPanel.setIcon(icon);
-
- }
- return bgPanel;
- }

先设置一个标题,后面的增删改查功能按钮设置在标题下面,此时只有一个实例对象,所以占整个窗口。
网格包布局管理需要使用GridBagConstraints对象布局,
gridx表示组件左上角所在的行列,容器中最左边列的 gridx 为 0,最上边行的 gridy 为 0。
gridwidth和gridheight用来指定组件显示区域所占的列数和行数,以网格单元而不是像素为单位,默认值为 1。
insets指定组件显示区域的外部填充,即组件与其显示区域边缘之间的空间,默认组件没有外部填充。
先建立一个标签,然后设置好字体大小,将标签添加到布局中。
- GridBagConstraints gridBagConstraints= new GridBagConstraints();
- gridBagConstraints.gridx = 0;
- gridBagConstraints.gridy = 0;
- gridBagConstraints.gridwidth = 4;
- gridBagConstraints.insets = new Insets(15,0,0,0);
- JLabel label = new JLabel("简易超市管理系统");
- label.setFont(new Font("华文行楷",Font.BOLD,30));
- bgPanel.add(label,gridBagConstraints);

创建按钮方法,设计出按钮样式,将按钮先组织到一个组件中,最后添加到网格包布局中。
实例化按钮对象
创建增删改查按钮方法,并设置按钮样式
- private JButton zengButton = null;
- private JButton shanButton = null;
- private JButton gaiButton = null;
- private JButton chaButton = null;
- private JButton getZengButton(){
- if(zengButton == null){
- zengButton = new JButton("增加内容");//实例化
- zengButton.setFont(new Font("华文行楷",Font.BOLD,20));
- zengButton.setOpaque(false);//设置组件透明
- zengButton.setBorderPainted(false);//设置按钮边界效果
- zengButton.setContentAreaFilled(false);//设置填充效果
- zengButton.setFocusPainted(false);//组件是否聚焦
- zengButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- }
- });
- }
- return zengButton;
- }
- private JButton getShanButton(){
- if(shanButton == null){
- shanButton = new JButton("删除内容");//实例化
- shanButton.setFont(new Font("华文行楷",Font.BOLD,20));
- shanButton.setOpaque(false);//设置组件透明
- shanButton.setBorderPainted(false);//设置按钮边界效果
- shanButton.setContentAreaFilled(false);//设置填充效果
- shanButton.setFocusPainted(false);//组件是否聚焦
- shanButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- }
- });
- }
- return shanButton;
- }
- private JButton getGaiButton(){
- if(gaiButton == null){
- gaiButton = new JButton("修改内容");//实例化
- gaiButton.setFont(new Font("华文行楷",Font.BOLD,20));
- gaiButton.setOpaque(false);//设置组件透明
- gaiButton.setBorderPainted(false);//设置按钮边界效果
- gaiButton.setContentAreaFilled(false);//设置填充效果
- gaiButton.setFocusPainted(false);//组件是否聚焦
- gaiButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- }
- });
- }
- return gaiButton;
- }
- private JButton getChaButton(){
- if(chaButton == null){
- chaButton = new JButton("查找内容");//实例化
- chaButton.setFont(new Font("华文行楷",Font.BOLD,20));
- chaButton.setOpaque(false);//设置组件透明
- chaButton.setBorderPainted(false);//设置按钮边界效果
- chaButton.setContentAreaFilled(false);//设置填充效果
- chaButton.setFocusPainted(false);//组件是否聚焦
- chaButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
-
- }
- });
- }
- return chaButton;
- }
getBGPanel()方法增加
创建一个面板组件,将按钮添加进去,然后将此按钮组件添加到布局中。
- GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
- gridBagConstraints2.gridx = 0;
- gridBagConstraints2.gridy = 1;
- gridBagConstraints2.gridwidth = 4;
- gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;//横向充满显示区域
- gridBagConstraints2.insets = new Insets(0,0,0,0);
- JPanel panel = new JPanel();
- panel.setOpaque(false);//设置组件透明,可见背景图片
- panel.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
- //将功能按钮添加到面板上
- panel.add(getZengButton());
- panel.add(getShanButton());
- panel.add(getGaiButton());
- panel.add(getChaButton());
- bgPanel.add(panel,gridBagConstraints2);
