zsh_root
2025-12-10 8d662de2fd262b3a485f16e197cb4d0ca2a61cdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package home;
 
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
public class ParameterTablePanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private MainFrame mainFrame;
    private JTable parameterTable;
    private DefaultTableModel tableModel;
    private JButton readBtn, saveBtn, restartBtn, resetBtn;
    
    public ParameterTablePanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        initializeUI();
    }
    
    private void initializeUI() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder(getString("base.parameters")));
        
        // 创建表格列名
        String[] columnNames = {
            getString("parameter.name"),
            getString("parameter.value")
        };
        
        // 创建表格模型,默认20行数据
        tableModel = new DefaultTableModel(columnNames, 0) {
            private static final long serialVersionUID = 1L;
            
            @Override
            public boolean isCellEditable(int row, int column) {
                // 只有参数值列可编辑
                return column == 1;
            }
        };
        
        // 添加20行默认数据
        for (int i = 1; i <= 20; i++) {
            tableModel.addRow(new Object[]{
                "第" + i + "行字符串",
                ""
            });
        }
        
        parameterTable = new JTable(tableModel);
        parameterTable.setRowHeight(25);
        
        // 设置列宽
        TableColumn column;
        column = parameterTable.getColumnModel().getColumn(0);
        column.setPreferredWidth(90); // 第1列宽90像素
        
        column = parameterTable.getColumnModel().getColumn(1);
        column.setPreferredWidth(100); // 第2列宽100像素
        
        JScrollPane scrollPane = new JScrollPane(parameterTable);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        readBtn = createStyledButton(getString("read.parameters"));
        saveBtn = createStyledButton(getString("save.parameters"));
        restartBtn = createStyledButton(getString("restart.device"));
        resetBtn = createStyledButton(getString("reset.factory"));
        
        buttonPanel.add(readBtn);
        buttonPanel.add(saveBtn);
        buttonPanel.add(restartBtn);
        buttonPanel.add(resetBtn);
        
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
        
        // 添加事件监听
        setupEventListeners();
    }
    
    /**
     * 创建样式按钮(参考SendPanel中的按钮设计)
     * @param text 按钮文本
     * @return 配置好样式的JButton
     */
    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(new Color(0, 120, 215)); // 蓝色背景
        button.setForeground(Color.WHITE); // 白色文字
        button.setFocusPainted(false);
        button.setOpaque(true);
        button.setBorderPainted(false);
        button.setFont(button.getFont().deriveFont(Font.BOLD)); // 加粗字体
        
        // 设置固定高度
        Dimension preferredSize = button.getPreferredSize();
        Dimension newSize = new Dimension(preferredSize.width, 25);
        button.setPreferredSize(newSize);
        button.setMinimumSize(newSize);
        button.setMaximumSize(newSize);
        
        // 添加鼠标悬停效果
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                button.setBackground(Color.GRAY); // 鼠标悬停时变为灰色
                button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 鼠标变为手型
            }
            
            @Override
            public void mouseExited(MouseEvent e) {
                button.setBackground(new Color(0, 120, 215)); // 鼠标离开时恢复蓝色
                button.setCursor(Cursor.getDefaultCursor()); // 鼠标恢复默认形状
            }
        });
        
        return button;
    }
    
    private void setupEventListeners() {
        readBtn.addActionListener(e -> readParameters());
        saveBtn.addActionListener(e -> saveParameters());
        restartBtn.addActionListener(e -> restartDevice());
        resetBtn.addActionListener(e -> resetFactory());
    }
    
    private void readParameters() {
        // 读取参数逻辑
        JOptionPane.showMessageDialog(this, 
            getString("read.parameters") + " - " + getString("function.not.implemented"),
            getString("prompt"), 
            JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void saveParameters() {
        // 保存参数逻辑
        StringBuilder savedData = new StringBuilder();
        savedData.append("保存的参数:\n");
        
        for (int i = 0; i < tableModel.getRowCount(); i++) {
            String paramName = (String) tableModel.getValueAt(i, 0);
            String paramValue = (String) tableModel.getValueAt(i, 1);
            savedData.append(paramName).append(": ").append(paramValue).append("\n");
        }
        
        JOptionPane.showMessageDialog(this, 
            savedData.toString(),
            getString("save.parameters"), 
            JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void restartDevice() {
        // 重启设备逻辑
        int result = JOptionPane.showConfirmDialog(this,
            getString("confirm.restart.device"),
            getString("restart.device"),
            JOptionPane.YES_NO_OPTION);
            
        if (result == JOptionPane.YES_OPTION) {
            JOptionPane.showMessageDialog(this,
                getString("device.restarting"),
                getString("prompt"),
                JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    private void resetFactory() {
        // 恢复出厂设置逻辑
        int result = JOptionPane.showConfirmDialog(this,
            getString("confirm.reset.factory"),
            getString("reset.factory"),
            JOptionPane.YES_NO_OPTION,
            JOptionPane.WARNING_MESSAGE);
            
        if (result == JOptionPane.YES_OPTION) {
            // 重置表格数据
            for (int i = 0; i < tableModel.getRowCount(); i++) {
                tableModel.setValueAt("", i, 1); // 清空参数值
            }
            
            JOptionPane.showMessageDialog(this,
                getString("factory.reset.completed"),
                getString("prompt"),
                JOptionPane.INFORMATION_MESSAGE);
        }
    }
    
    public void updateLanguage() {
        // 更新边框标题
        setBorder(BorderFactory.createTitledBorder(getString("base.parameters")));
        
        // 更新按钮文本
        readBtn.setText(getString("read.parameters"));
        saveBtn.setText(getString("save.parameters"));
        restartBtn.setText(getString("restart.device"));
        resetBtn.setText(getString("reset.factory"));
        
        // 更新表格列名
        String[] columnNames = {
            getString("parameter.name"),
            getString("parameter.value")
        };
        tableModel.setColumnIdentifiers(columnNames);
        
        revalidate();
        repaint();
    }
    
    private String getString(String key) {
        return mainFrame.getString(key);
    }
    
    /**
     * 获取表格数据
     * @return 包含所有行数据的二维数组
     */
    public Object[][] getTableData() {
        int rowCount = tableModel.getRowCount();
        int colCount = tableModel.getColumnCount();
        Object[][] data = new Object[rowCount][colCount];
        
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < colCount; j++) {
                data[i][j] = tableModel.getValueAt(i, j);
            }
        }
        return data;
    }
    
    /**
     * 设置表格数据
     * @param data 要设置的数据
     */
    public void setTableData(Object[][] data) {
        tableModel.setRowCount(0); // 清空现有数据
        for (Object[] row : data) {
            tableModel.addRow(row);
        }
    }
    
    /**
     * 获取指定行的数据
     * @param row 行索引
     * @return 该行的数据数组
     */
    public Object[] getRowData(int row) {
        if (row >= 0 && row < tableModel.getRowCount()) {
            Object[] rowData = new Object[tableModel.getColumnCount()];
            for (int i = 0; i < tableModel.getColumnCount(); i++) {
                rowData[i] = tableModel.getValueAt(row, i);
            }
            return rowData;
        }
        return null;
    }
    
    /**
     * 设置指定行的数据
     * @param row 行索引
     * @param rowData 行数据数组
     */
    public void setRowData(int row, Object[] rowData) {
        if (row >= 0 && row < tableModel.getRowCount() && rowData != null) {
            for (int i = 0; i < Math.min(tableModel.getColumnCount(), rowData.length); i++) {
                tableModel.setValueAt(rowData[i], row, i);
            }
        }
    }
    
    /**
     * 获取表格行数
     * @return 行数
     */
    public int getRowCount() {
        return tableModel.getRowCount();
    }
    
    /**
     * 获取表格列数
     * @return 列数
     */
    public int getColumnCount() {
        return tableModel.getColumnCount();
    }
}