?? ginshopmanagement.java
字號:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class GinshopManagement
{
public static void main(String [] args)
{
MainFrame frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.show();
}
}
class MainFrame extends JFrame
{
public MainFrame()
{
//set MainFrame size
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenLength = screenSize.height;
setSize(screenWidth / 2 + 30, screenLength / 2);
setLocation((screenWidth - getWidth()) / 2, (screenLength - getHeight()) / 2);
setTitle("歡迎使用:酒店管理系統");
loadData();
initMenu();
if(autoConnect == true)
connectDB();
initGUI();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
try
{
saveData();
if (stat != null)
stat.close();
if (conn != null)
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
System.exit(0);
}
}
});
}
private void initGUI()
{
JTabbedPane tabbedPane = new JTabbedPane();
CheckInPreorderPanelInstance = new CheckInPreorderPanel(this);
CheckOutPanelInstance = new CheckOutPanel(this);
tabbedPane.addTab("入住及預定", CheckInPreorderPanelInstance);
tabbedPane.addTab("結賬退房", CheckOutPanelInstance);
getContentPane().add(tabbedPane, BorderLayout.CENTER);
}
private void initMenu()
{
JMenu fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic('F');
fileMenu.add(new ConnectAction("連接數據庫...", true, KeyEvent.VK_L, InputEvent.CTRL_MASK));
fileMenu.add(new ConnectAction("配置數據庫...", false, KeyEvent.VK_C, InputEvent.CTRL_MASK));
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
public void connectDB()
{
try
{
if(stat != null)
stat.close();
if(conn != null)
conn.close();
conn = getConnection("酒店管理");
stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
}
catch(IOException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
Toolkit.getDefaultToolkit().beep();
if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, "無法連接到數據庫\n"+
"這可能是因為數據庫文件的路徑不正確\n"
+ "要配置路徑嗎?", "無法打開數據庫",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE))
{
formerFilePath = "";
reconnect = true;
showConnectDBDialog();
}
}
}
private void loadData()
{
Properties defaultSettings = new Properties();
defaultSettings.put("userName", "");
defaultSettings.put("password", "");
defaultSettings.put("formerFilePath", "");
defaultSettings.put("autoConnect", "false");
settings = new Properties(defaultSettings);
try
{
FileInputStream in = new FileInputStream("酒店管理.db");
settings.load(in);
}
catch(IOException e)
{
e.printStackTrace();
}
userName = settings.getProperty("userName");
password = Safety.coordinate(settings.getProperty("password"));
formerFilePath = settings.getProperty("formerFilePath");
autoConnect = ((settings.getProperty("autoConnect").equals("true")) ? true : false);
}
private void saveData()
{
try
{
FileOutputStream out = new FileOutputStream("酒店管理.db");
settings.put("userName", userName);
settings.put("password", Safety.chaos(password)); //加密之后再儲存
settings.put("formerFilePath", formerFilePath);
settings.put("autoConnect", String.valueOf(autoConnect));
settings.store(out, "GinshopManagement");
}
catch(IOException e)
{
e.printStackTrace();
}
}
private void showConnectDBDialog()
{
if(connectDBDialog == null)
connectDBDialog = new DataExchangeDialog(this, new ConnectDBPanel(this), "連接數據庫");
connectDBDialog.showDialog();
}
public void showMessage(String message)
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(MainFrame.this, message,
"錯誤", JOptionPane.INFORMATION_MESSAGE);
}
public void reConnect()
{
if(reconnect)
{
if(CheckInPreorderPanelInstance != null)
CheckInPreorderPanelInstance.clear();
connectDB();
if(CheckInPreorderPanelInstance != null)
CheckInPreorderPanelInstance.updatehouseTypeComboBox();
}
}
private Connection getConnection(String DBName)
throws SQLException, IOException
{
Properties props = new Properties();
props.setProperty("password", password);
props.setProperty("user", userName);
props.setProperty("url", "jdbc:odbc:" + DBName);
System.setProperty("jdbc.drivers", "sun.jdbc.odbc.JdbcOdbcDriver");
return
DriverManager.getConnection(props.getProperty("url"), props);
}
public void showWarningMessage(String message)
{
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(MainFrame.this, message,
"錯誤", JOptionPane.WARNING_MESSAGE);
}
private class ConnectAction extends AbstractAction
{
public ConnectAction(String name, boolean connectImmediately, int keyCode,
int modifiers)
{
super(name);
this.connectImmediately = connectImmediately;
putValue(Action.ACCELERATOR_KEY,
KeyStroke.getKeyStroke(keyCode,modifiers));
}
public void actionPerformed(ActionEvent event)
{
reconnect = connectImmediately;
showConnectDBDialog();
}
private boolean connectImmediately;
}
private DataExchangeDialog connectDBDialog = null;
private CheckOutPanel CheckOutPanelInstance;
private Properties settings;
private boolean reconnect;
public CheckInPreorderPanel CheckInPreorderPanelInstance;
public String formerFilePath = "";
public String userName = "";
public String password = "";
public boolean autoConnect = false;
public Connection conn = null;
public Statement stat = null;
public static final int NAME = 0;
public static final int ID = 1;
public static final int HOUSE_NUM = 2;
public static final int TYPE = 3;
public static final int TIME = 4;
public static final int MONEY = 5;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -