JavaSwing汽车租赁系统二
本系统是将面向对象基础和高级开发融合起来,同时将GUI、数据库融入到面向对象软件设计和开发中,运用所学知识解决复杂工程问题。
实现功能截图
登录页面
主菜单页面:
车辆信息管理页面:
租车信息管理页面:
还车信息管理页面:
修车管理页面:
利润分析页面:
系统功能
本汽车租赁系统实现了以下功能:
1、提供图形界面输入用户名和密码登录,然后进入主界面,进行汽车租赁业务操作。
2、进入系统后,用户可以查看所有车辆、租车、归还、保修等操作。
使用技术
数据库:mysql
开发工具:Eclipse(Myeclispe、Idea也可以)
知识点:JavaSwing
本系统采用将数据与视图分离的思想:将项目包分为model、util和view,代码结构清晰
代码
登录:
package view;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import util.Database;
import javax.swing.JPanel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* 登录窗口
* @author wen
*
*/
public class LoginView extends JFrame{
//private JFrame frame;
/**
* 用户名
*/
private JTextField textField;
/**
* 密码
*/
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginView window = new LoginView();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginView() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//this = new JFrame();
this.setTitle("车辆管理系统 - 管理员登录");
this.setBounds(100, 100, 450, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 261);
this.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("登录名:");
lblNewLabel.setBounds(87, 73, 56, 16);
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14));
panel.add(lblNewLabel);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
JLabel lblNewLabel_1 = new JLabel("密码:");
lblNewLabel_1.setBounds(99, 122, 44, 30);
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 14));
panel.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(147, 67, 194, 30);
panel.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(147, 123, 194, 30);
panel.add(passwordField);
JButton btnNewButton = new JButton("登录");
btnNewButton.setBounds(180, 186, 93, 30);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = textField.getText().trim();
String password = new String(passwordField.getPassword()).trim();
if (userName == null || "".equals(userName)) {
JOptionPane.showMessageDialog(null, "登录名不能为空");
return;
}
if (password == null || "".equals(password)) {
JOptionPane.showMessageDialog(null, "密码不能为空");
return;
}
try {
if (Database.userLogin(userName, password)) {
dispose();
MainView mv = new MainView();
//mv.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "登录失败,请核对登录信息");
return;
}
} catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "登录失败");
return;
}
}
});
panel.add(btnNewButton);
}
}
主页面:
package view;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.SystemColor;
import javax.swing.JPanel;
public class MainView{
private JFrame frame;
private JButton btnNewButton;
private JButton button;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private final JPanel panel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainView window = new MainView();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainView() {
super();
initialize();
}
public MainView(boolean reload) {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("车辆管理系统");
frame.setBounds(100, 100, 729, 517);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
panel.setBounds(10, 0, 691, 468);
frame.getContentPane().add(panel);
btnNewButton = new JButton("车辆信息管理");
button = new JButton("租车信息管理");
button_1 = new JButton("还车信息管理");
button_2 = new JButton("修车信息管理");
button_3 = new JButton("利润分析");
panel.setLayout(null);
btnNewButton.setBounds(63, 97, 272, 58);
panel.add(btnNewButton);
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 24));
button.setBounds(387, 97, 272, 58);
panel.add(button);
button.setFont(new Font("宋体", Font.PLAIN, 24));
button_1.setBounds(63, 191, 272, 58);
panel.add(button_1);
button_1.setFont(new Font("宋体", Font.PLAIN, 24));
button_2.setBounds(387, 191, 272, 58);
panel.add(button_2);
button_2.setFont(new Font("宋体", Font.PLAIN, 24));
button_3.setBounds(63, 296, 596, 58);
panel.add(button_3);
button_3.setFont(new Font("宋体", Font.PLAIN, 24));
JLabel lblNewLabel = new JLabel("车 辆 管 理 系 统");
lblNewLabel.setBounds(63, 20, 596, 40);
panel.add(lblNewLabel);
lblNewLabel.setForeground(SystemColor.text);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 34));
BtnListener btn = new BtnListener();
button_3.addActionListener(btn);
button_2.addActionListener(btn);
button_1.addActionListener(btn);
button.addActionListener(btn);
btnNewButton.addActionListener(btn);
}
public class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnNewButton) {
// frame.dispose();
CarInfoView carInfoView = new CarInfoView();
} else if (e.getSource() == button) {
RentCarInfoView carInfoView = new RentCarInfoView();
// System.exit(0);
} else if (e.getSource() == button_1) {
RepayCarInfoView carInfoView = new RepayCarInfoView();
// System.exit(0);
} else if (e.getSource() == button_2) {
FixCarInfoView carInfoView = new FixCarInfoView();
// System.exit(0);
} else if (e.getSource() == button_3) {
// frame.dispose();
ProfitAnalysisView p = new ProfitAnalysisView();
}
}
}
}
车辆管理页面:
package view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import model.TCar;
import util.Database;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.JButton;
public class CarInfoView {
private JFrame frame;
private JTable table;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
JScrollPane scrollPane;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_4;
private TableModel model;
/**
* Launch the application.
*/
private Vector<String> columnName = new Vector();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CarInfoView window = new CarInfoView();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CarInfoView() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("车辆管理系统");
frame.setBounds(100, 100, 567, 504);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel_1 = new JPanel();
panel_1.setBounds(422, 10, 129, 455);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel label = new JLabel("车辆编号");
label.setBounds(38, 10, 54, 15);
panel_1.add(label);
textField = new JTextField();
textField.setBounds(27, 35, 77, 21);
panel_1.add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("车辆名称");
lblNewLabel.setBounds(38, 66, 54, 15);
panel_1.add(lblNewLabel);
textField_1 = new JTextField();
textField_1.setBounds(27, 91, 77, 21);
panel_1.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("车辆颜色");
lblNewLabel_1.setBounds(38, 122, 54, 15);
panel_1.add(lblNewLabel_1);
textField_2 = new JTextField();
textField_2.setBounds(27, 147, 77, 21);
panel_1.add(textField_2);
textField_2.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("车辆类型");
lblNewLabel_2.setBounds(38, 178, 54, 15);
panel_1.add(lblNewLabel_2);
textField_3 = new JTextField();
textField_3.setBounds(27, 203, 77, 21);
panel_1.add(textField_3);
textField_3.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("车辆计价");
lblNewLabel_3.setBounds(38, 234, 54, 15);
panel_1.add(lblNewLabel_3);
textField_4 = new JTextField();
textField_4.setBounds(27, 259, 77, 21);
panel_1.add(textField_4);
textField_4.setColumns(10);
// 操作按钮
button_1 = new JButton("添加");
button_1.setBounds(27, 307, 77, 23);
panel_1.add(button_1);
button_2 = new JButton("修改");
button_2.setBounds(27, 340, 77, 23);
panel_1.add(button_2);
button_3 = new JButton("删除");
button_3.setBounds(27, 373, 77, 23);
panel_1.add(button_3);
button_4 = new JButton("刷新");
button_4.setBounds(27, 406, 77, 23);
panel_1.add(button_4);
scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, 393, 455);
frame.getContentPane().add(scrollPane);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setToolTipText("车辆信息");
// 初始化表格数据
columnName.add("车辆编号");
columnName.add("车辆名称");
columnName.add("车辆颜色");
columnName.add("车辆类型");
columnName.add("车辆计价");
Vector rowData = new Vector();
List<TCar> list = Database.queryAllCarInfo();
if (list != null && list.size() > 0) {
for (int i=0;i<list.size();i++) {
TCar car = list.get(i);
Vector vector = new Vector();
vector.add(car.getId());
vector.add(car.getName());
vector.add(car.getColor());
vector.add(car.getType());
vector.add(car.getPrice());
rowData.add(vector);
}
}
model = new DefaultTableModel(rowData, columnName) {
// 设置表格中的数据不可以编辑
public boolean isCellEditable(int r,int c) {
return false;
}
};
table = new JTable(model);
scrollPane.setViewportView(table);
// 只可单选
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getTableHeader().setReorderingAllowed(false);
// 表格监听事件
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
if (!e.getValueIsAdjusting()) {
//支持拖动多选
// int[]rows = table.getSelectedRows();
// int[]cols = table.getSelectedColumns();//选中的列
//for (int i : rows) {
//System.out.println("#方法一:\t" + table.getValueAt(i, 0) + "\t" + table.getValueAt(i, 1));
//}
//支持单选
int row=table.getSelectedRow();//选中行
int col=table.getSelectedColumn();//选中列
System.out.println("方法一:"+table.getValueAt(row, 0)+"\t"+table.getValueAt(row,4));
// 綁定值
textField.setText(String.valueOf(table.getValueAt(row, 0)));
textField_1.setText(String.valueOf(table.getValueAt(row, 1)));
textField_2.setText(String.valueOf(table.getValueAt(row, 2)));
textField_3.setText(String.valueOf(table.getValueAt(row, 3)));
textField_4.setText(String.valueOf(table.getValueAt(row, 4)));
}
}
});
BtnListener btn = new BtnListener();
button_3.addActionListener(btn);
button_2.addActionListener(btn);
button_1.addActionListener(btn);
button_4.addActionListener(btn);
}
public class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_1) {
// 添加事件
String name = textField_1.getText();
String color = textField_2.getText().trim();
String type = textField_3.getText().trim();
String price = textField_4.getText().trim();
if (name == null || "".equals(name)) {
JOptionPane.showMessageDialog(null, "车辆名称不能为空");
return;
}
if (color == null || "".equals(color)) {
JOptionPane.showMessageDialog(null, "车辆颜色不能为空");
return;
}
if (type == null || "".equals(type)) {
JOptionPane.showMessageDialog(null, "车辆类型不能为空");
return;
}
if (price == null || "".equals(price)) {
JOptionPane.showMessageDialog(null, "车辆计价不能为空");
return;
}
float fPrice = Float.valueOf(price);
String sql = "insert into t_car(name,color,type,price) values('" + name + "','" + color + "','" + type + "'," + fPrice + " )";
Database.update(sql);
JOptionPane.showMessageDialog(null, "添加成功");
} else if (e.getSource() == button_2) {
// 修改事件
int row = table.getSelectedRow();
if (row <= 0) {
JOptionPane.showMessageDialog(null, "请选择一条记录再执行修改操作");
return;
}
int id = (int) table.getValueAt(row, 0);
String name = textField_1.getText().trim();
if (name == null || "".equals(name)) {
JOptionPane.showMessageDialog(null, "车辆名称不能为空");
return;
}
String color = textField_2.getText().trim();
String type = textField_3.getText().trim();
String price = textField_4.getText().trim();
float fPrice = 0;
if (price != null || price != "") {
fPrice = Float.valueOf(price);
}
String sql = "update t_car set name = '" + name + "',color = '" + color + "',type = '" + type + "',price = " + fPrice + " where id = " + id;
Database.update(sql);
JOptionPane.showMessageDialog(null, "修改成功");
} else if (e.getSource() == button_3) {
// 删除事件
int row = table.getSelectedRow();
if (row <= 0) {
JOptionPane.showMessageDialog(null, "请选择一条记录再执行删除操作");
return;
}
int id = (int) table.getValueAt(row, 0);
String sql = "delete from t_car where id = " + id;
Database.update(sql);
JOptionPane.showMessageDialog(null, "删除成功");
} else if (e.getSource() == button_4) {
// 刷新表格数据事件
Object carInfos[][] = {};
Vector rowData = new Vector();
List<TCar> list = Database.queryAllCarInfo();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
TCar car = list.get(i);
Vector vector = new Vector();
vector.add(car.getId());
vector.add(car.getName());
vector.add(car.getColor());
vector.add(car.getType());
vector.add(car.getPrice());
rowData.add(vector);
}
}
model = new DefaultTableModel(rowData, columnName) {
// 设置表格中的数据不可以编辑
public boolean isCellEditable(int r,int c) {
return false;
}
};
table = new JTable(model);
scrollPane.setViewportView(table);
// 表格监听事件
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
if (!e.getValueIsAdjusting()) {
//支持拖动多选
// int[]rows = table.getSelectedRows();
// int[]cols = table.getSelectedColumns();//选中的列
//for (int i : rows) {
//System.out.println("#方法一:\t" + table.getValueAt(i, 0) + "\t" + table.getValueAt(i, 1));
//}
//支持单选
int row=table.getSelectedRow();//选中行
int col=table.getSelectedColumn();//选中列
System.out.println("方法一:"+table.getValueAt(row, 0)+"\t"+table.getValueAt(row,4));
// 綁定值
textField.setText(String.valueOf(table.getValueAt(row, 0)));
textField_1.setText(String.valueOf(table.getValueAt(row, 1)));
textField_2.setText(String.valueOf(table.getValueAt(row, 2)));
textField_3.setText(String.valueOf(table.getValueAt(row, 3)));
textField_4.setText(String.valueOf(table.getValueAt(row, 4)));
}
}
});
}
}
}
}
利润分析页面:
package view;
import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import util.Database;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
/**
* 利润分析窗口
* @author wen
*
*/
public class ProfitAnalysisView {
private JFrame frame;
private JTable table;
private JLabel label_1;
private JLabel label_2;
private JLabel label_3;
private Vector<String> columnName = new Vector();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProfitAnalysisView window = new ProfitAnalysisView();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ProfitAnalysisView() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("利润分析");
frame.setBounds(100, 100, 567, 504);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBorder(UIManager.getBorder("ComboBox.border"));
scrollPane.setBounds(0, 0, 551, 304);
frame.getContentPane().add(scrollPane);
columnName.add("编号");
columnName.add("时间");
columnName.add("类型");
columnName.add("车辆ID");
columnName.add("费用");
JPanel panel = new JPanel();
panel.setBounds(0, 338, 551, 117);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("收入");
lblNewLabel.setHorizontalTextPosition(SwingConstants.CENTER);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel.setBounds(89, 24, 54, 31);
panel.add(lblNewLabel);
JLabel lblNewLabe2 = new JLabel("支出");
lblNewLabe2.setHorizontalTextPosition(SwingConstants.CENTER);
lblNewLabe2.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabe2.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabe2.setBounds(253, 24, 54, 31);
panel.add(lblNewLabe2);
JLabel lblNewLabe3 = new JLabel("利润");
lblNewLabe3.setHorizontalTextPosition(SwingConstants.CENTER);
lblNewLabe3.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabe3.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabe3.setBounds(412, 24, 54, 31);
panel.add(lblNewLabe3);
label_1 = new JLabel("0.0");
label_1.setForeground(new Color(0, 255, 255));
label_1.setFont(new Font("宋体", Font.PLAIN, 16));
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setHorizontalTextPosition(SwingConstants.CENTER);
label_1.setBounds(83, 65, 70, 31);
panel.add(label_1);
label_2 = new JLabel("0.0");
label_2.setForeground(Color.RED);
label_2.setHorizontalTextPosition(SwingConstants.CENTER);
label_2.setHorizontalAlignment(SwingConstants.CENTER);
label_2.setFont(new Font("宋体", Font.PLAIN, 16));
label_2.setBounds(247, 65, 70, 31);
panel.add(label_2);
label_3 = new JLabel("0.0");
label_3.setForeground(Color.GREEN);
label_3.setHorizontalTextPosition(SwingConstants.CENTER);
label_3.setHorizontalAlignment(SwingConstants.CENTER);
label_3.setFont(new Font("宋体", Font.PLAIN, 16));
label_3.setBounds(407, 65, 70, 31);
panel.add(label_3);
// 车辆使用信息汇总
Vector usage = new Vector();
usage = Database.queryAllCarUsage();
TableModel model = new DefaultTableModel(usage, columnName) {
// 设置表格中的数据不可以编辑
public boolean isCellEditable(int r,int c) {
return false;
}
};
table = new JTable(model);
DefaultTableCellRenderer d = new DefaultTableCellRenderer();
// 设置表格列名对齐方式为居左对齐
//table.getTableHeader().setDefaultRenderer(d);
scrollPane.setViewportView(table);
// 数据分析
float inCome = Database.queryIncome();
label_1.setText(String.valueOf(inCome));
float cost = Database.queryCost();
label_2.setText(String.valueOf(cost));
float profits = inCome - cost;
label_3.setText(String.valueOf(profits));
}
}
写在最后
如果运行代码中遇到问题,或者需要完整源码和报告,可以加博主V交流:Code2Life2