Skip to content

Instantly share code, notes, and snippets.

@kishida
Created September 28, 2023 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishida/165c590025b0c8a21ca10c861432c731 to your computer and use it in GitHub Desktop.
Save kishida/165c590025b0c8a21ca10c861432c731 to your computer and use it in GitHub Desktop.
ChatGPT generated Java form from form design image
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.table.DefaultTableModel;
public class SimpleForm {
public static void main(String[] args) {
JFrame frame = new JFrame("Sample Form");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
// ラベルとテキストフィールドの追加
frame.add(new JLabel("ID"));
JTextField idField = new JTextField(10);
frame.add(idField);
frame.add(new JLabel("名前"));
JTextField nameField = new JTextField(10);
frame.add(nameField);
frame.add(new JLabel("種別"));
JComboBox<String> comboBox = new JComboBox<>();
comboBox.addItem("男性");
comboBox.addItem("女性");
frame.add(comboBox);
frame.add(new JLabel("金額"));
JTextField amountField = new JTextField(10);
frame.add(amountField);
// テーブルの追加
String[] columnNames = {"ID", "名前", "種別", "金額"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(350, 150));
frame.add(scrollPane);
// 登録ボタンの追加
JButton registerButton = new JButton("登録");
frame.add(registerButton);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = idField.getText();
String name = nameField.getText();
String type = (String) comboBox.getSelectedItem();
String amount = amountField.getText();
tableModel.addRow(new Object[]{id, name, type, amount});
// フィールドをクリア
idField.setText("");
nameField.setText("");
amountField.setText("");
}
});
frame.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment