java创建swing表格,表的表头为"姓名","账号","密码","操作",其中”操作“下面有”同意“”取消“两个按钮
时间: 2023-02-06 13:12:34 浏览: 67
在 Java 中创建一个 Swing 表格,其中包含 "姓名"、"账号"、"密码"、"操作" 四个列,其中 "操作" 列包含 "同意" 和 "取消" 两个按钮,你可以这样做:
1. 首先,创建一个 JFrame 窗口,用于显示表格:
```
JFrame frame = new JFrame("表格示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
```
2. 然后,定义表格的列名,并使用 DefaultTableModel 创建表格模型:
```
String[] columnNames = {"姓名", "账号", "密码", "操作"};
Object[][] data = {};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
```
3. 接下来,创建一个 JTable 表格,并将表格模型作为参数传递给构造函数:
```
JTable table = new JTable(model);
```
4. 创建一个 JScrollPane 对象,并将表格作为参数传递给构造函数,这样就可以在表格中使用滚动条:
```
JScrollPane scrollPane = new JScrollPane(table);
```
5. 为 "操作" 列添加按钮。首先,创建一个自定义的表格渲染器,用于在单元格中显示按钮:
```
class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)