826220679@qq.com
2025-10-31 9aca70f16836952e2e3462ecc69dabe679811eb7
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();
    }
}