private JButton addButton;public InterfaceCallExample() { //... addButton = new JButton("新增数据"); addButton.addActionListener(this); panel.add(addButton); //...}@Overridepublic void actionPerformed(ActionEvent e) { if (e.getSource() == callButton) { //... } else if (e.getSource() == addButton) { String id = JOptionPane.showInputDialog(this, "请输入id:"); String titleName = JOptionPane.showInputDialog(this, "请输入材料类型:"); String status = JOptionPane.showInputDialog(this, "请输入状态:"); String createTime = JOptionPane.showInputDialog(this, "请输入创建时间:"); String updateTime = JOptionPane.showInputDialog(this, "请输入更改时间:"); ArrayList<String> strings = new ArrayList<>(); strings.add(id); strings.add(titleName); strings.add(status); strings.add(createTime); strings.add(updateTime); String[] data = strings.toString().replace("[","").replace("]","").split(","); tableModel.addRow(data); }} 把新增的输入框可以一次性展示出来
时间: 2024-03-23 08:38:26 浏览: 49
Button-Swing.zip_java swing button
您可以将输入框放在一个对话框中,以便一次性展示出来。可以使用以下代码:
```java
else if (e.getSource() == addButton) {
JPanel panel = new JPanel(new GridLayout(0, 1));
JTextField idField = new JTextField(10);
JTextField titleNameField = new JTextField(10);
JTextField statusField = new JTextField(10);
JTextField createTimeField = new JTextField(10);
JTextField updateTimeField = new JTextField(10);
panel.add(new JLabel("id:"));
panel.add(idField);
panel.add(new JLabel("材料类型:"));
panel.add(titleNameField);
panel.add(new JLabel("状态:"));
panel.add(statusField);
panel.add(new JLabel("创建时间:"));
panel.add(createTimeField);
panel.add(new JLabel("更改时间:"));
panel.add(updateTimeField);
int result = JOptionPane.showConfirmDialog(null, panel, "新增数据", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String id = idField.getText();
String titleName = titleNameField.getText();
String status = statusField.getText();
String createTime = createTimeField.getText();
String updateTime = updateTimeField.getText();
ArrayList<String> strings = new ArrayList<>();
strings.add(id);
strings.add(titleName);
strings.add(status);
strings.add(createTime);
strings.add(updateTime);
String[] data = strings.toString().replace("[","").replace("]","").split(",");
tableModel.addRow(data);
}
}
```
这段代码将会弹出一个带有多个输入框的对话框,用户可以在其中输入数据,单击“确定”按钮后,将会向表格中添加一行数据。
阅读全文