GUI-demo(不含DB)

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class BookManagementSystem extends JFrame {
    private DefaultTableModel tableModel;
    private JTable bookTable;
    private int selectedRowIndex = -1;

    public BookManagementSystem() {
        setTitle("图书管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // 添加查询面板
        JPanel searchPanel = new JPanel(new FlowLayout());
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String bookName = searchField.getText();
                if (!bookName.isEmpty()) {
                    searchBook(bookName);
                }
            }
        });
        searchPanel.add(new JLabel("图书名称:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        panel.add(searchPanel, BorderLayout.NORTH);

        // 创建表格模型和表格组件
        tableModel = new DefaultTableModel();
        tableModel.addColumn("图书编号");
        tableModel.addColumn("图书名称");
        tableModel.addColumn("作者");
        tableModel.addColumn("出版社");
        bookTable = new JTable(tableModel);

        // 添加双击事件监听器
        bookTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                int row = bookTable.rowAtPoint(evt.getPoint());
                int col = bookTable.columnAtPoint(evt.getPoint());
                if (row >= 0 && col >= 0 && evt.getClickCount() == 2) {
                    showBookDetails(row);
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(bookTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        getContentPane().add(panel);

        // 添加按钮
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton addButton = new JButton("添加图书");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addOrUpdateBook(false);
            }
        });

        JButton updateButton = new JButton("修改图书");
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedRowIndex = bookTable.getSelectedRow();
                if (selectedRowIndex != -1) {
                    addOrUpdateBook(true);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择要修改的图书。", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        JButton deleteButton = new JButton("删除图书");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = bookTable.getSelectedRow();
                if (row != -1) {
                    tableModel.removeRow(row);
                }
            }
        });

        buttonPanel.add(addButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(deleteButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addOrUpdateBook(boolean isUpdate) {
        JTextField nameField = new JTextField(20);
        JTextField authorField = new JTextField(20);
        JTextField publisherField = new JTextField(20);

        JPanel myPanel = new JPanel();
        myPanel.setLayout(new GridLayout(3, 2));
        myPanel.add(new JLabel("图书名称:"));
        myPanel.add(nameField);
        myPanel.add(new JLabel("作者:"));
        myPanel.add(authorField);
        myPanel.add(new JLabel("出版社:"));
        myPanel.add(publisherField);

        if (isUpdate && selectedRowIndex != -1) {
            nameField.setText((String) tableModel.getValueAt(selectedRowIndex, 1));
            authorField.setText((String) tableModel.getValueAt(selectedRowIndex, 2));
            publisherField.setText((String) tableModel.getValueAt(selectedRowIndex, 3));
        }

        int result = JOptionPane.showConfirmDialog(null, myPanel,
                isUpdate ? "修改图书信息" : "添加图书信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String bookName = nameField.getText();
            String author = authorField.getText();
            String publisher = publisherField.getText();

            if (bookName.isEmpty() || author.isEmpty() || publisher.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入完整的图书信息。", "提示", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (isUpdate && selectedRowIndex != -1) {
                tableModel.setValueAt(bookName, selectedRowIndex, 1);
                tableModel.setValueAt(author, selectedRowIndex, 2);
                tableModel.setValueAt(publisher, selectedRowIndex, 3);
                selectedRowIndex = -1;
            } else {
                Vector<String> row = new Vector<>();
                row.add(String.valueOf(tableModel.getRowCount() + 1));
                row.add(bookName);
                row.add(author);
                row.add(publisher);
                tableModel.addRow(row);
            }
        }
    }

    private void searchBook(String bookName) {
        Vector<String> booksFound = new Vector<>();
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            String name = (String) tableModel.getValueAt(i, 1);
            if (name.equals(bookName)) {
                showBookDetails(i);
                return;
            }
        }
        JOptionPane.showMessageDialog(this, "未找到该图书。", "提示", JOptionPane.INFORMATION_MESSAGE);
    }

    private void showBookDetails(int rowIndex) {
        String bookID = (String) tableModel.getValueAt(rowIndex, 0);
        String bookName = (String) tableModel.getValueAt(rowIndex, 1);
        String author = (String) tableModel.getValueAt(rowIndex, 2);
        String publisher = (String) tableModel.getValueAt(rowIndex, 3);

        StringBuilder details = new StringBuilder();
        details.append("图书编号: ").append(bookID).append("\n");
        details.append("图书名称: ").append(bookName).append("\n");
        details.append("作者: ").append(author).append("\n");
        details.append("出版社: ").append(publisher).append("\n");

        JOptionPane.showMessageDialog(this, details.toString(), "图书详细信息", JOptionPane.INFORMATION_MESSAGE);
    }

    private void editBook(int row) {
        selectedRowIndex = row;
        addOrUpdateBook(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BookManagementSystem().setVisible(true);
            }
        });
    }
}

666

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class InventoryManagementSystem extends JFrame {
    private DefaultTableModel tableModel;
    private JTable inventoryTable;
    private int selectedRowIndex = -1;

    public InventoryManagementSystem() {
        setTitle("出入库管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // 添加查询面板
        JPanel searchPanel = new JPanel(new FlowLayout());
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String itemName = searchField.getText();
                if (!itemName.isEmpty()) {
                    searchItem(itemName);
                }
            }
        });
        searchPanel.add(new JLabel("物品名称:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        panel.add(searchPanel, BorderLayout.NORTH);

        // 创建表格模型和表格组件
        tableModel = new DefaultTableModel();
        tableModel.addColumn("编号");
        tableModel.addColumn("名称");
        tableModel.addColumn("数量");
        inventoryTable = new JTable(tableModel);

        JScrollPane scrollPane = new JScrollPane(inventoryTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        getContentPane().add(panel);

        // 添加按钮
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton addButton = new JButton("添加物品");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addOrUpdateItem(false);
            }
        });

        JButton updateButton = new JButton("修改物品");
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedRowIndex = inventoryTable.getSelectedRow();
                if (selectedRowIndex != -1) {
                    addOrUpdateItem(true);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择要修改的物品。", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        JButton deleteButton = new JButton("删除物品");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = inventoryTable.getSelectedRow();
                if (row != -1) {
                    tableModel.removeRow(row);
                }
            }
        });

        buttonPanel.add(addButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(deleteButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addOrUpdateItem(boolean isUpdate) {
        JTextField idField = new JTextField(10);
        JTextField nameField = new JTextField(10);
        JTextField quantityField = new JTextField(10);

        JPanel myPanel = new JPanel();
        myPanel.setLayout(new GridLayout(3, 2));
        myPanel.add(new JLabel("编号:"));
        myPanel.add(idField);
        myPanel.add(new JLabel("名称:"));
        myPanel.add(nameField);
        myPanel.add(new JLabel("数量:"));
        myPanel.add(quantityField);

        if (isUpdate) {
            idField.setText((String) tableModel.getValueAt(selectedRowIndex, 0));
            nameField.setText((String) tableModel.getValueAt(selectedRowIndex, 1));
            quantityField.setText((String) tableModel.getValueAt(selectedRowIndex, 2));
        }

        int result = JOptionPane.showConfirmDialog(null, myPanel,
                isUpdate ? "修改物品信息" : "添加物品信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String itemID = idField.getText();
            String itemName = nameField.getText();
            String itemQuantity = quantityField.getText();

            if (itemID.isEmpty() || itemName.isEmpty() || itemQuantity.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请填写完整的物品信息。", "提示", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (isUpdate) {
                tableModel.setValueAt(itemID, selectedRowIndex, 0);
                tableModel.setValueAt(itemName, selectedRowIndex, 1);
                tableModel.setValueAt(itemQuantity, selectedRowIndex, 2);
                selectedRowIndex = -1;
            } else {
                Vector<String> row = new Vector<>();
                row.add(itemID);
                row.add(itemName);
                row.add(itemQuantity);
                tableModel.addRow(row);
            }
        }
    }

    private void searchItem(String itemName) {
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            String name = (String) tableModel.getValueAt(i, 1);
            if (name.equals(itemName)) {
                String itemID = (String) tableModel.getValueAt(i, 0);
                String itemQuantity = (String) tableModel.getValueAt(i, 2);
                JOptionPane.showMessageDialog(this, "编号: " + itemID + "\n名称: " + itemName + "\n数量: " + itemQuantity, "物品信息", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }
        JOptionPane.showMessageDialog(this, "未找到该物品。", "提示", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new InventoryManagementSystem().setVisible(true);
            }
        });
    }
}

666

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class StudentManagementSystem extends JFrame {
    private DefaultTableModel tableModel;
    private JTable studentTable;
    private int selectedRowIndex = -1;

    public StudentManagementSystem() {
        setTitle("学生管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        // 添加查询面板
        JPanel searchPanel = new JPanel(new FlowLayout());
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentName = searchField.getText();
                if (!studentName.isEmpty()) {
                    searchStudent(studentName);
                }
            }
        });
        searchPanel.add(new JLabel("学生姓名:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        panel.add(searchPanel, BorderLayout.NORTH);

        // 创建表格模型和表格组件
        tableModel = new DefaultTableModel();
        tableModel.addColumn("学号");
        tableModel.addColumn("姓名");
        tableModel.addColumn("年龄");
        tableModel.addColumn("性别");
        tableModel.addColumn("成绩");
        studentTable = new JTable(tableModel);

        // 添加双击事件监听器
        studentTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                int row = studentTable.rowAtPoint(evt.getPoint());
                int col = studentTable.columnAtPoint(evt.getPoint());
                if (row >= 0 && col >= 0 && evt.getClickCount() == 2) {
                    showStudentDetails(row);
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(studentTable);
        panel.add(scrollPane, BorderLayout.CENTER);

        getContentPane().add(panel);

        // 添加按钮
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton addButton = new JButton("添加学生");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addOrUpdateStudent(false);
            }
        });

        JButton updateButton = new JButton("修改学生");
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                selectedRowIndex = studentTable.getSelectedRow();
                if (selectedRowIndex != -1) {
                    addOrUpdateStudent(true);
                } else {
                    JOptionPane.showMessageDialog(null, "请选择要修改的学生。", "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        JButton deleteButton = new JButton("删除学生");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = studentTable.getSelectedRow();
                if (row != -1) {
                    tableModel.removeRow(row);
                }
            }
        });

        buttonPanel.add(addButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(deleteButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addOrUpdateStudent(boolean isUpdate) {
        JTextField idField = new JTextField(10);
        JTextField nameField = new JTextField(10);
        JTextField ageField = new JTextField(10);
        JTextField genderField = new JTextField(10);
        JTextField scoreField = new JTextField(10);

        JPanel myPanel = new JPanel();
        myPanel.setLayout(new GridLayout(5, 2));
        myPanel.add(new JLabel("学号:"));
        myPanel.add(idField);
        myPanel.add(new JLabel("姓名:"));
        myPanel.add(nameField);
        myPanel.add(new JLabel("年龄:"));
        myPanel.add(ageField);
        myPanel.add(new JLabel("性别:"));
        myPanel.add(genderField);
        myPanel.add(new JLabel("成绩:"));
        myPanel.add(scoreField);

        if (isUpdate) {
            idField.setText((String) tableModel.getValueAt(selectedRowIndex, 0));
            nameField.setText((String) tableModel.getValueAt(selectedRowIndex, 1));
            ageField.setText((String) tableModel.getValueAt(selectedRowIndex, 2));
            genderField.setText((String) tableModel.getValueAt(selectedRowIndex, 3));
            scoreField.setText((String) tableModel.getValueAt(selectedRowIndex, 4));
        }

        int result = JOptionPane.showConfirmDialog(null, myPanel,
                isUpdate ? "修改学生信息" : "添加学生信息", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String studentID = idField.getText();
            String studentName = nameField.getText();
            String studentAge = ageField.getText();
            String studentGender = genderField.getText();
            String studentScore = scoreField.getText();

            if (studentID.isEmpty() || studentName.isEmpty() || studentAge.isEmpty() ||
                    studentGender.isEmpty() || studentScore.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请填写完整的学生信息。", "提示", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (isUpdate) {
                tableModel.setValueAt(studentID, selectedRowIndex, 0);
                tableModel.setValueAt(studentName, selectedRowIndex, 1);
                tableModel.setValueAt(studentAge, selectedRowIndex, 2);
                tableModel.setValueAt(studentGender, selectedRowIndex, 3);
                tableModel.setValueAt(studentScore, selectedRowIndex, 4);
                selectedRowIndex = -1;
            } else {
                Vector<String> row = new Vector<>();
                row.add(studentID);
                row.add(studentName);
                row.add(studentAge);
                row.add(studentGender);
                row.add(studentScore);
                tableModel.addRow(row);
            }
        }
    }

    private void searchStudent(String studentName) {
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            String name = (String) tableModel.getValueAt(i, 1);
            if (name.equals(studentName)) {
                showStudentDetails(i);
                return;
            }
        }
        JOptionPane.showMessageDialog(this, "未找到该学生。", "提示", JOptionPane.INFORMATION_MESSAGE);
    }

    private void showStudentDetails(int rowIndex) {
        String studentID = (String) tableModel.getValueAt(rowIndex, 0);
        String studentName = (String) tableModel.getValueAt(rowIndex, 1);
        String studentAge = (String) tableModel.getValueAt(rowIndex, 2);
        String studentGender = (String) tableModel.getValueAt(rowIndex, 3);
        String studentScore = (String) tableModel.getValueAt(rowIndex, 4);

        StringBuilder details = new StringBuilder();
        details.append("学号: ").append(studentID).append("\n");
        details.append("姓名: ").append(studentName).append("\n");
        details.append("年龄: ").append(studentAge).append("\n");
        details.append("性别: ").append(studentGender).append("\n");
        details.append("成绩: ").append(studentScore).append("\n");

        JOptionPane.showMessageDialog(this, details.toString(), "学生详细信息", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StudentManagementSystem().setVisible(true);
            }
        });
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值