用java,gui实现这样一个代码,建立一个表格,点击表格中的一行,能够展示该行的详细信。该代码也可以用下拉菜单栏实现筛选功能(用注释解释)
时间: 2023-06-26 13:10:00 浏览: 154
以下是用Java Swing实现该功能的代码,已经添加了注释以方便理解:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TableDemo extends JFrame {
private JTable table;
private DefaultTableModel tableModel;
private JLabel nameLabel;
private JLabel ageLabel;
private JLabel genderLabel;
private JLabel addressLabel;
private JLabel phoneLabel;
private JTextField nameTextField;
private JTextField ageTextField;
private JComboBox<String> genderComboBox;
private JTextField addressTextField;
private JTextField phoneTextField;
public TableDemo() {
super("Table Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 创建表格模型
tableModel = new DefaultTableModel(new String[]{"Name", "Age", "Gender", "Address", "Phone"}, 0);
// 添加示例数据
tableModel.addRow(new Object[]{"John Doe", 30, "Male", "123 Main St, Anytown USA", "555-1234"});
tableModel.addRow(new Object[]{"Jane Smith", 25, "Female", "456 Oak St, Anytown USA", "555-5678"});
// 创建表格并设置模型
table = new JTable(tableModel);
// 添加表格行点击事件监听器
table.getSelectionModel().addListSelectionListener(event -> {
int row = table.getSelectedRow();
if (row >= 0) {
nameTextField.setText((String) tableModel.getValueAt(row, 0));
ageTextField.setText(String.valueOf(tableModel.getValueAt(row, 1)));
genderComboBox.setSelectedItem(tableModel.getValueAt(row, 2));
addressTextField.setText((String) tableModel.getValueAt(row, 3));
phoneTextField.setText((String) tableModel.getValueAt(row, 4));
}
});
// 创建筛选面板
JPanel filterPanel = new JPanel(new FlowLayout());
// 添加筛选条件下拉菜单
String[] genders = {"All", "Male", "Female"};
JComboBox<String> filterComboBox = new JComboBox<>(genders);
filterPanel.add(new JLabel("Gender:"));
filterPanel.add(filterComboBox);
// 添加筛选按钮
JButton filterButton = new JButton("Filter");
filterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedGender = (String) filterComboBox.getSelectedItem();
for (int i = tableModel.getRowCount() - 1; i >= 0; i--) {
String gender = (String) tableModel.getValueAt(i, 2);
if (!selectedGender.equals("All") && !selectedGender.equals(gender)) {
tableModel.removeRow(i);
} else if (selectedGender.equals("All") && tableModel.getRowCount() < 2) {
// 如果是“全部”选项,并且表格中只有一行,则不删除
break;
}
}
}
});
filterPanel.add(filterButton);
// 添加表格和筛选面板到主面板
add(new JScrollPane(table), BorderLayout.CENTER);
add(filterPanel, BorderLayout.NORTH);
// 创建详细信息面板
JPanel detailPanel = new JPanel(new GridLayout(5, 2));
nameLabel = new JLabel("Name:");
ageLabel = new JLabel("Age:");
genderLabel = new JLabel("Gender:");
addressLabel = new JLabel("Address:");
phoneLabel = new JLabel("Phone:");
nameTextField = new JTextField();
ageTextField = new JTextField();
genderComboBox = new JComboBox<>(genders);
addressTextField = new JTextField();
phoneTextField = new JTextField();
detailPanel.add(nameLabel);
detailPanel.add(nameTextField);
detailPanel.add(ageLabel);
detailPanel.add(ageTextField);
detailPanel.add(genderLabel);
detailPanel.add(genderComboBox);
detailPanel.add(addressLabel);
detailPanel.add(addressTextField);
detailPanel.add(phoneLabel);
detailPanel.add(phoneTextField);
// 添加详细信息面板到主面板
add(detailPanel, BorderLayout.SOUTH);
// 显示窗口
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new TableDemo();
}
}
```
运行上述代码后,将会看到一个带有表格和筛选面板的窗口。可以使用下拉菜单来筛选特定性别的行,也可以点击表格中的行来查看详细信息。
阅读全文