| | |
| | | |
| | | import targets.LocationTag; |
| | | import javax.swing.*; |
| | | import javax.swing.table.DefaultTableCellRenderer; |
| | | import javax.swing.table.DefaultTableModel; |
| | | import javax.swing.table.JTableHeader; |
| | | import java.awt.*; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | import java.sql.SQLException; |
| | | import java.util.List; |
| | | import java.util.ResourceBundle; |
| | | import publicsWay.ButtonUtils; // Added import |
| | | |
| | | public class TagManagementPanel extends JPanel { |
| | | private static final long serialVersionUID = 1L; |
| | |
| | | public TagManagementPanel(ResourceBundle messages) { |
| | | this.messages = messages; |
| | | setLayout(new BorderLayout()); |
| | | setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); |
| | | setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| | | |
| | | // 创建搜索面板 |
| | | JPanel searchPanel = new JPanel(new BorderLayout(5, 5)); |
| | | searchPanel.setBorder(BorderFactory.createTitledBorder(messages.getString("SEARCH"))); |
| | | // 创建顶部面板 |
| | | JPanel topPanel = new JPanel(new BorderLayout(5, 5)); |
| | | topPanel.setBorder(BorderFactory.createTitledBorder(messages.getString("SEARCH"))); |
| | | |
| | | // 创建搜索文本框和按钮 |
| | | searchField = new JTextField(10); // 缩短搜索文本框长度 |
| | | JButton searchButton = new JButton(messages.getString("SEARCH")); |
| | | JButton refreshButton = new JButton(messages.getString("REFRESH")); // 添加刷新按钮 |
| | | refreshButton.addActionListener(e -> loadTagData()); // 绑定刷新动作 |
| | | // 左侧搜索组件面板 |
| | | JPanel searchInputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0)); |
| | | JLabel searchLabel = new JLabel(messages.getString("DEVICE_NUMBER") + ":"); |
| | | searchField = new JTextField(20); |
| | | |
| | | // 使用ButtonUtils创建按钮 |
| | | JButton searchButton = ButtonUtils.createBlueButton(messages.getString("SEARCH")); |
| | | JButton resetButton = ButtonUtils.createBlueButton(messages.getString("RESET")); |
| | | |
| | | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5)); |
| | | buttonPanel.add(searchButton); |
| | | buttonPanel.add(refreshButton); |
| | | searchInputPanel.add(searchLabel); |
| | | searchInputPanel.add(searchField); |
| | | searchInputPanel.add(searchButton); |
| | | searchInputPanel.add(resetButton); |
| | | |
| | | searchPanel.add(searchField, BorderLayout.CENTER); |
| | | searchPanel.add(buttonPanel, BorderLayout.EAST); |
| | | |
| | | // 创建操作按钮面板 |
| | | JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5)); |
| | | JButton addButton = new JButton(messages.getString("ADD")); |
| | | JButton deleteButton = new JButton(messages.getString("DELETE")); |
| | | // 右侧操作按钮面板 |
| | | JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0)); |
| | | JButton addButton = ButtonUtils.createBlueButton(messages.getString("ADD")); |
| | | JButton deleteButton = ButtonUtils.createBlueButton(messages.getString("DELETE")); |
| | | JButton refreshButton = ButtonUtils.createBlueButton(messages.getString("REFRESH")); |
| | | |
| | | actionPanel.add(addButton); |
| | | actionPanel.add(deleteButton); |
| | | actionPanel.add(refreshButton); |
| | | |
| | | // 将左侧搜索和右侧操作添加到顶部面板 |
| | | topPanel.add(searchInputPanel, BorderLayout.CENTER); |
| | | topPanel.add(actionPanel, BorderLayout.EAST); |
| | | |
| | | // 创建表格 - 按照表5要求修改列名 |
| | | String[] columnNames = { |
| | |
| | | tagTable.setAutoCreateRowSorter(true); |
| | | tagTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| | | |
| | | // 设置表格样式 - 去掉竖线,只显示横线,栅栏效果 |
| | | tagTable.setShowVerticalLines(false); // 不显示竖线 |
| | | tagTable.setShowHorizontalLines(true); // 显示横线 |
| | | tagTable.setGridColor(new Color(220, 220, 220)); // 设置网格线颜色为浅灰色 |
| | | tagTable.setIntercellSpacing(new Dimension(0, 1)); // 设置单元格间距,增加行间距 |
| | | tagTable.setRowHeight(25); // 设置行高 |
| | | |
| | | // 设置列宽 |
| | | tagTable.getColumnModel().getColumn(0).setPreferredWidth(30); |
| | | tagTable.getColumnModel().getColumn(1).setPreferredWidth(100); |
| | |
| | | tagTable.getColumnModel().getColumn(9).setPreferredWidth(120); // 上线时间 |
| | | tagTable.getColumnModel().getColumn(10).setPreferredWidth(100); |
| | | |
| | | // 设置表头样式 |
| | | JTableHeader header = tagTable.getTableHeader(); |
| | | header.setBackground(new Color(70, 130, 180)); // 钢蓝色表头 |
| | | header.setForeground(Color.WHITE); |
| | | header.setFont(header.getFont().deriveFont(Font.BOLD)); |
| | | header.setPreferredSize(new Dimension(header.getWidth(), 30)); // 增加表头高度 |
| | | |
| | | // 创建表头渲染器 |
| | | DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer() { |
| | | /** |
| | | * |
| | | */ |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | { |
| | | setHorizontalAlignment(SwingConstants.CENTER); |
| | | setBackground(new Color(70, 130, 180)); // 钢蓝色表头 |
| | | setForeground(Color.WHITE); |
| | | setFont(getFont().deriveFont(Font.BOLD)); |
| | | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); // 增加表头内边距 |
| | | } |
| | | }; |
| | | |
| | | // 应用表头渲染器 |
| | | for (int i = 0; i < tagTable.getColumnCount(); i++) { |
| | | tagTable.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer); |
| | | } |
| | | |
| | | // 创建单元格渲染器,实现栅栏效果 |
| | | DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer() { |
| | | /** |
| | | * |
| | | */ |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @Override |
| | | public Component getTableCellRendererComponent(JTable table, Object value, |
| | | boolean isSelected, boolean hasFocus, |
| | | int row, int column) { |
| | | Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
| | | |
| | | // 设置交替行颜色 |
| | | if (!isSelected) { |
| | | if (row % 2 == 0) { |
| | | c.setBackground(new Color(240, 240, 200)); // 浅灰色 |
| | | } else { |
| | | c.setBackground(Color.WHITE); // 白色 |
| | | } |
| | | } else { |
| | | c.setBackground(new Color(200, 220, 240)); // 选中行的蓝色 |
| | | } |
| | | |
| | | // 设置单元格对齐方式 |
| | | if (column == 0) { // 索引列居中 |
| | | setHorizontalAlignment(SwingConstants.CENTER); |
| | | } else if (column == 5) { // 电池电量列居中 |
| | | setHorizontalAlignment(SwingConstants.CENTER); |
| | | } else { |
| | | setHorizontalAlignment(SwingConstants.CENTER); |
| | | } |
| | | |
| | | // 设置单元格边框 |
| | | setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5)); // 左右内边距 |
| | | |
| | | return c; |
| | | } |
| | | }; |
| | | |
| | | // 应用单元格渲染器到所有列 |
| | | for (int i = 0; i < tagTable.getColumnCount(); i++) { |
| | | tagTable.getColumnModel().getColumn(i).setCellRenderer(cellRenderer); |
| | | } |
| | | |
| | | JScrollPane scrollPane = new JScrollPane(tagTable); |
| | | scrollPane.setPreferredSize(new Dimension(1200, 400)); |
| | | scrollPane.setBorder(BorderFactory.createEmptyBorder()); // 移除滚动面板边框 |
| | | |
| | | // 添加组件 |
| | | add(searchPanel, BorderLayout.NORTH); |
| | | add(topPanel, BorderLayout.NORTH); |
| | | add(scrollPane, BorderLayout.CENTER); |
| | | add(actionPanel, BorderLayout.SOUTH); |
| | | |
| | | // 加载数据 |
| | | loadTagData(); |
| | | |
| | | // 添加事件监听器 |
| | | searchButton.addActionListener(new SearchAction()); |
| | | resetButton.addActionListener(e -> resetSearch()); |
| | | refreshButton.addActionListener(e -> loadTagData()); |
| | | |
| | | addButton.addActionListener(e -> { |
| | | AddTagDialog dialog = new AddTagDialog((Frame) SwingUtilities.getWindowAncestor(TagManagementPanel.this), messages); |
| | | dialog.setVisible(true); |
| | |
| | | } |
| | | } |
| | | |
| | | // 重置搜索 |
| | | private void resetSearch() { |
| | | searchField.setText(""); |
| | | updateTable(allTags); |
| | | } |
| | | |
| | | private class SearchAction implements ActionListener { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |