使用 Swing 组件库用Java写一个gui能够实现对停车位的信息管理(车位号、车主姓名、性别、年龄、 车牌号),进行信息录入部分的代码,要代码不用解释过程
时间: 2023-08-31 12:24:34 浏览: 117
### 回答1:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ParkingInfoManagement extends JFrame implements ActionListener {
private JLabel lblParkingNumber;
private JLabel lblOwnerName;
private JLabel lblGender;
private JLabel lblAge;
private JLabel lblLicensePlateNumber;
private JTextField txtParkingNumber;
private JTextField txtOwnerName;
private JTextField txtGender;
private JTextField txtAge;
private JTextField txtLicensePlateNumber;
private JButton btnSave;
public ParkingInfoManagement() {
setTitle("停车位信息管理");
setLayout(new FlowLayout());
lblParkingNumber = new JLabel("车位号:");
add(lblParkingNumber);
txtParkingNumber = new JTextField(20);
add(txtParkingNumber);
lblOwnerName = new JLabel("车主姓名:");
add(lblOwnerName);
txtOwnerName = new JTextField(20);
add(txtOwnerName);
lblGender = new JLabel("性别:");
add(lblGender);
txtGender = new JTextField(20);
add(txtGender);
lblAge = new JLabel("年龄:");
add(lblAge);
txtAge = new JTextField(20);
add(txtAge);
lblLicensePlateNumber = new JLabel("车牌号:");
add(lblLicensePlateNumber);
txtLicensePlateNumber = new JTextField(20);
add(txtLicensePlateNumber);
btnSave = new JButton("保存");
btnSave.addActionListener(this);
add(btnSave);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSave) {
String parkingNumber = txtParkingNumber.getText();
String ownerName = txtOwnerName.getText();
String gender = txtGender.getText();
String age = txtAge.getText();
String licensePlateNumber = txtLicensePlateNumber.getText();
// 在这里可以把输入的信息保存到数据库或者文件中
System.out.println("车位号: " + parkingNumber);
System.out.println("车主姓名: " + ownerName);
### 回答2:
以下是使用Swing组件库用Java编写的一个停车位信息管理GUI的代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ParkingLotManagementFrame extends JFrame {
private JTextField txtCarNumber;
private JTextField txtOwnerName;
private JComboBox<String> cmbGender;
private JSpinner spinAge;
private JButton btnSubmit;
public ParkingLotManagementFrame() {
setTitle("停车位信息管理");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(5, 2, 10, 10));
JLabel lblCarNumber = new JLabel("车牌号:");
txtCarNumber = new JTextField();
JLabel lblOwnerName = new JLabel("车主姓名:");
txtOwnerName = new JTextField();
JLabel lblGender = new JLabel("性别:");
String[] genders = {"男", "女"};
cmbGender = new JComboBox<>(genders);
JLabel lblAge = new JLabel("年龄:");
spinAge = new JSpinner();
JLabel lblEmptySpace = new JLabel(); // 占位用
btnSubmit = new JButton("提交");
btnSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submitForm();
}
});
mainPanel.add(lblCarNumber);
mainPanel.add(txtCarNumber);
mainPanel.add(lblOwnerName);
mainPanel.add(txtOwnerName);
mainPanel.add(lblGender);
mainPanel.add(cmbGender);
mainPanel.add(lblAge);
mainPanel.add(spinAge);
mainPanel.add(lblEmptySpace);
mainPanel.add(btnSubmit);
add(mainPanel);
setVisible(true);
}
private void submitForm() {
String carNumber = txtCarNumber.getText();
String ownerName = txtOwnerName.getText();
String gender = (String) cmbGender.getSelectedItem();
int age = (int) spinAge.getValue();
// 在此处实现将表单数据保存到数据库或其他数据结构的代码
JOptionPane.showMessageDialog(this, "信息保存成功!");
}
public static void main(String[] args) {
new ParkingLotManagementFrame();
}
}
```
这段代码创建了一个名为ParkingLotManagementFrame的Swing窗体,包含了一些输入字段,如txtCarNumber(车牌号)、txtOwnerName(车主姓名)、cmbGender(性别的下拉框)和spinAge(年龄的微调器),以及一个提交按钮btnSubmit。当用户点击提交按钮时,会调用submitForm()方法来处理输入的数据。
submitForm()方法会获取用户输入的车牌号、车主姓名、性别和年龄,并可以在此处添加相关代码,将这些信息保存到数据库或其他数据结构中。
以上就是停车位信息管理GUI的信息录入部分的代码实现。
### 回答3:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ParkingManagement extends JFrame {
private JTextField tfSlotNumber;
private JTextField tfOwnerName;
private JTextField tfGender;
private JTextField tfAge;
private JTextField tfLicensePlate;
private JButton btnSubmit;
public ParkingManagement() {
setTitle("停车位信息管理");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6, 2));
JLabel lblSlotNumber = new JLabel("车位号:");
tfSlotNumber = new JTextField();
JLabel lblOwnerName = new JLabel("车主姓名:");
tfOwnerName = new JTextField();
JLabel lblGender = new JLabel("性别:");
tfGender = new JTextField();
JLabel lblAge = new JLabel("年龄:");
tfAge = new JTextField();
JLabel lblLicensePlate = new JLabel("车牌号:");
tfLicensePlate = new JTextField();
panel.add(lblSlotNumber);
panel.add(tfSlotNumber);
panel.add(lblOwnerName);
panel.add(tfOwnerName);
panel.add(lblGender);
panel.add(tfGender);
panel.add(lblAge);
panel.add(tfAge);
panel.add(lblLicensePlate);
panel.add(tfLicensePlate);
btnSubmit = new JButton("提交");
btnSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
submit();
}
});
panel.add(btnSubmit);
add(panel);
}
private void submit() {
String slotNumber = tfSlotNumber.getText();
String ownerName = tfOwnerName.getText();
String gender = tfGender.getText();
int age = Integer.parseInt(tfAge.getText());
String licensePlate = tfLicensePlate.getText();
// 在这里进行信息录入的相应操作,如将信息存入数据库或文件中
tfSlotNumber.setText("");
tfOwnerName.setText("");
tfGender.setText("");
tfAge.setText("");
tfLicensePlate.setText("");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ParkingManagement().setVisible(true);
}
});
}
}
阅读全文