用Java写一个弹出窗口的汽车管理系统,要求用文件输入输出,汽车管理系统包含了添加、删除、清空和查看所有车辆的功能
时间: 2023-06-25 14:02:13 浏览: 89
基于java的车辆管理系统含文档
好的,下面是一个简单的Java代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class CarManagementSystem extends JFrame implements ActionListener{
private JLabel lblMake, lblModel, lblYear, lblPrice;
private JTextField txtMake, txtModel, txtYear, txtPrice;
private JButton btnAdd, btnDelete, btnClear, btnViewAll;
private File file = new File("cars.txt"); // 文件名
private String[] headers = {"Make", "Model", "Year", "Price"};
public CarManagementSystem(){
setTitle("Car Management System");
// 标签和文本框
lblMake = new JLabel("Make:");
txtMake = new JTextField(20);
lblModel = new JLabel("Model:");
txtModel = new JTextField(20);
lblYear = new JLabel("Year:");
txtYear = new JTextField(20);
lblPrice = new JLabel("Price:");
txtPrice = new JTextField(20);
// 按钮
btnAdd = new JButton("Add");
btnAdd.addActionListener(this);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(this);
btnClear = new JButton("Clear");
btnClear.addActionListener(this);
btnViewAll = new JButton("View All");
btnViewAll.addActionListener(this);
// 布局
JPanel panel1 = new JPanel(new GridLayout(4, 2));
panel1.add(lblMake);
panel1.add(txtMake);
panel1.add(lblModel);
panel1.add(txtModel);
panel1.add(lblYear);
panel1.add(txtYear);
panel1.add(lblPrice);
panel1.add(txtPrice);
JPanel panel2 = new JPanel(new FlowLayout());
panel2.add(btnAdd);
panel2.add(btnDelete);
panel2.add(btnClear);
panel2.add(btnViewAll);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel1, BorderLayout.CENTER);
contentPane.add(panel2, BorderLayout.SOUTH);
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnAdd){
String make = txtMake.getText();
String model = txtModel.getText();
String year = txtYear.getText();
String price = txtPrice.getText();
try {
// 写入文件
FileWriter writer = new FileWriter(file, true);
writer.write(make + "," + model + "," + year + "," + price + "\n");
writer.close();
JOptionPane.showMessageDialog(this, "Car added successfully!");
} catch (IOException ex) {
ex.printStackTrace();
}
}else if(e.getSource() == btnDelete){
String make = JOptionPane.showInputDialog(this, "Enter make of car to delete:");
try {
// 读取文件
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder sb = new StringBuilder();
while((line = reader.readLine()) != null){
if(line.startsWith(make)){
continue; // 删除此行
}
sb.append(line);
sb.append(System.lineSeparator());
}
reader.close();
// 写入文件
FileWriter writer = new FileWriter(file);
writer.write(sb.toString());
writer.close();
JOptionPane.showMessageDialog(this, "Car deleted successfully!");
} catch (IOException ex) {
ex.printStackTrace();
}
}else if(e.getSource() == btnClear){
try {
// 清空文件
FileWriter writer = new FileWriter(file);
writer.write("");
writer.close();
JOptionPane.showMessageDialog(this, "All cars deleted successfully!");
} catch (IOException ex) {
ex.printStackTrace();
}
}else if(e.getSource() == btnViewAll){
try {
// 读取文件
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder sb = new StringBuilder();
while((line = reader.readLine()) != null){
sb.append(line);
sb.append(System.lineSeparator());
}
reader.close();
// 显示数据
String data = sb.toString();
if(data.length() > 0){
String[] rows = data.split(System.lineSeparator());
JTable table = new JTable(rows.length, headers.length);
for(int i=0; i<headers.length; i++){
table.getColumnModel().getColumn(i).setHeaderValue(headers[i]);
}
for(int i=0; i<rows.length; i++){
String[] values = rows[i].split(",");
for(int j=0; j<values.length; j++){
table.setValueAt(values[j], i, j);
}
}
JScrollPane scrollPane = new JScrollPane(table);
JOptionPane.showMessageDialog(this, scrollPane);
}else{
JOptionPane.showMessageDialog(this, "No cars found!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new CarManagementSystem();
}
}
```
上述代码实现了一个简单的汽车管理系统,可以添加、删除、清空和查看所有车辆。使用文件输入输出进行数据持久化。其中,添加和删除操作会将数据写入文件;查看所有车辆会从文件中读取数据并展示到一个表格中。
阅读全文