簡易酒店管理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();
}
}
}