简易酒店管理GUI程序
继上一篇文章写得一个酒店管理控制台程序,我又重新做了一个GUI的程序,相比于枯燥的控制打印,我更喜欢这种效果的,也拿出来分享一下。
下面是效果图:
下面是代码:
package day0720200513;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class HotelFrame {
static String[][] rooms = new String[9][9];
static JFrame jf = null;
static JPanel jp = null;
static JButton[][] jbtns = new JButton[9][9];
public static void main(String[] args) {
jf = new JFrame("酒店管理系统");
// 设置窗体大小
jf.setSize(700, 600);
// 设置窗体位置居中显示
jf.setLocationRelativeTo(null);
// 禁止鼠标拖动修改窗体大小
jf.setResizable(false);
// 设置默认关闭选项:关闭窗体时退出程序进程
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp = new JPanel();
// 设置布局
jp.setLayout(null);
// 创建标题
JLabel title = new JLabel("欢迎使用酒店管理系统");
// 调整组件的位置大小
title.setBounds(0, 0, 700, 50);
// 设置字体
title.setFont(new Font("楷体", Font.BOLD, 30));
// 设置标题颜色
title.setForeground(new Color(125, 59, 99));
// 设置标题水平居中
title.setHorizontalAlignment(JLabel.CENTER);
// 添加到panel中
jp.add(title);
reader();
initFrame();
jf.add(jp);
jf.setVisible(true);
}
/**
* 初始化窗体,并操作房间的方法
*/
private static void initFrame() {
int y = 60;
int x = 10;
int w = 65;
int h = 45;
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
String name = "<html>" + ((i + 1) * 1000 + j + 1) + "<br>"
+ ("empty".equals(rooms[i][j]) ? "empty" : "****") + "</html>";
JButton jbtn = new JButton(name);
// 设置按钮位置、大小
jbtn.setBounds(x, y, w, h);
// 设置黑体 加粗 16号字体
jbtn.setFont(new Font("黑体", Font.BOLD, 16));
// 未入住的房间
if ("empty".equals(rooms[i][j])) {
// 设置绿色背景
jbtn.setBackground(new Color(153, 230, 108));
// 设置灰色字体
jbtn.setForeground(Color.GRAY);
} else { // 已入住的房间
// 设置红色背景
jbtn.setBackground(new Color(119, 7, 11));
// 设置白色字体
jbtn.setForeground(Color.WHITE);
}
// 按钮添加点击事件监听
jbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 遍历按钮数组
for (int i = 0; i < jbtns.length; i++) {
for (int j = 0; j < jbtns[i].length; j++) {
// 找到被点击的按钮
if (e.getSource().equals(jbtns[i][j])) {
// 当前按钮对应的房间数据是empty说明没有入住,进行入住操作
if ("empty".equals(rooms[i][j])) {
String name = JOptionPane.showInputDialog(null, "请输入姓名:", "预订房间",
JOptionPane.QUESTION_MESSAGE);
if (name == null) {
return;
}
if ("".equals(name)) {
JOptionPane.showMessageDialog(null, "姓名不能是空的");
return;
}
rooms[i][j] = name;
jbtns[i][j].setBackground(new Color(119, 7, 11));
jbtns[i][j].setForeground(Color.WHITE);
String message = name + "成功预订" + ((i + 1) * 1000 + j + 1) + "号房间";
System.out.println(message);
JOptionPane.showMessageDialog(null, message);
} else { // 否则进行退房操作
int choose = JOptionPane.showConfirmDialog(null, "确定退订房间吗?", "退订房间",
JOptionPane.YES_NO_OPTION);
if (choose == JOptionPane.YES_OPTION) {
rooms[i][j] = "empty";
jbtns[i][j].setBackground(new Color(153, 230, 108));
jbtns[i][j].setForeground(Color.GRAY);
String message = ((i + 1) * 1000 + j + 1) + "号房间退订成功";
System.out.println(message);
JOptionPane.showMessageDialog(null, message);
}
}
// 根据当前房间数据更新按钮文字
jbtns[i][j].setText("<html>" + ((i + 1) * 1000 + j + 1) + "<br>"
+ ("empty".equals(rooms[i][j]) ? "empty" : "****") + "</html>");
// 保存数据
saver();
}
}
}
}
});
// 不绘制焦点框框
jbtn.setFocusPainted(false);
jbtns[i][j] = jbtn;
jp.add(jbtn);
x += w + 10;
}
y += h + 10;
x = 10;
}
}
/**
* 初始化房间数据的方法
*/
private static void reader() {
File file = new File("./data/data.csv");
try {
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
if (!(file.isFile() && file.exists())) {
file.createNewFile();
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
rooms[i][j] = "empty";
}
}
} else {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), "UTF-8"));
String linestr = null;
int i = 0;
while ((linestr = bufferedReader.readLine()) != null) {
String[] room = linestr.split(",");
rooms[i] = room;
i++;
}
bufferedReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 保存数据到文件的方法
*/
private static void saver() {
File file = new File("./data/data.csv");
try {
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
if (!(file.isFile() && file.exists())) {
file.createNewFile();
}
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
for (int i = 0; i < rooms.length; i++) {
StringBuffer linestr = new StringBuffer();
for (int j = 0; j < rooms[i].length; j++) {
linestr.append(rooms[i][j]);
if (j != rooms[i].length - 1) {
linestr.append(",");
}
}
bufferedWriter.write(linestr.toString());
bufferedWriter.newLine();
}
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}