📄 roompanel.java
字号:
package ui;
import RoomPackage.Room;
import RoomPackage.RoomManager;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import AccountsPack.User;
/*
* Created on 2005-4-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author zinniazxp
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class RoomPanel extends JPanel {
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JComboBox jComboBox = null;
private JLabel jLabel2 = null;
private JTextField jTextField = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JComboBox jComboBox1 = null;
private JComboBox jComboBox2 = null;
private JLabel jLabel5 = null;
private JPanel jPanel = null;
private JPanel jPanel1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JTextArea jTextArea = null;
private Handler _handler = new Handler();
private Room _currentRoom = null;
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
String[] items = Room.ROOMTYPE;
jComboBox = new JComboBox(items);
jComboBox.setPreferredSize(new java.awt.Dimension(80,20));
}
return jComboBox;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setPreferredSize(new java.awt.Dimension(80,20));
}
return jTextField;
}
/**
* This method initializes jComboBox1
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox1() {
if (jComboBox1 == null) {
String[] items = {"Yes", "No"};
jComboBox1 = new JComboBox(items);
jComboBox1.setPreferredSize(new java.awt.Dimension(80,20));
}
return jComboBox1;
}
/**
* This method initializes jComboBox2
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox2() {
if (jComboBox2 == null) {
String[] items = {"Yes", "No"};
jComboBox2 = new JComboBox(items);
jComboBox2.setPreferredSize(new java.awt.Dimension(80,20));
}
return jComboBox2;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setPreferredSize(new java.awt.Dimension(600,100));
jPanel.add(getJPanel1(), null);
jPanel.add(getSubmitButton(), null);
jPanel.add(getCancelButton(), null);
}
return jPanel;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setPreferredSize(new java.awt.Dimension(590,40));
}
return jPanel1;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getSubmitButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("Submit");
jButton.addActionListener(_handler);
jButton.setActionCommand("SUBMIT");
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
public JButton getCancelButton() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("Cancel");
jButton1.addActionListener(_handler);
jButton1.setActionCommand("CANCEL");
}
return jButton1;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setPreferredSize(new java.awt.Dimension(500,60));
jTextArea.setLineWrap(true);
jTextArea.setRows(3);
}
return jTextArea;
}
/**
* This is the default constructor
*/
public RoomPanel() {
super();
jLabel5 = new JLabel();
jLabel4 = new JLabel();
jLabel3 = new JLabel();
jLabel2 = new JLabel();
jLabel1 = new JLabel();
jLabel = new JLabel();
setLayout(new FlowLayout());
jLabel.setText(" Add a room...");
jLabel.setPreferredSize(new java.awt.Dimension(600,60));
jLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 18));
jLabel1.setText("Type");
jLabel1.setPreferredSize(new java.awt.Dimension(50,20));
jLabel2.setText("Prize");
jLabel2.setPreferredSize(new java.awt.Dimension(50,20));
jLabel3.setText("Breakfast");
jLabel3.setPreferredSize(new java.awt.Dimension(70,20));
jLabel4.setText("Telephone");
jLabel4.setPreferredSize(new java.awt.Dimension(70,20));
jLabel5.setText("Description");
jLabel5.setPreferredSize(new java.awt.Dimension(450,20));
add(jLabel, null);
add(jLabel1, null);
add(getJComboBox(), null);
add(jLabel2, null);
add(getJTextField(), null);
add(jLabel3, null);
add(getJComboBox1(), null);
add(jLabel4, null);
add(getJComboBox2(), null);
add(jLabel5, null);
add(new JScrollPane(getJTextArea()), null);
add(getJPanel(), null);
}
public void showRoomInfo(Room room) {
jComboBox.setSelectedIndex(room.get_roomType());
jTextField.setText(new Float(room.get_roomPrice()).toString());
jTextArea.setText(room.get_roomDescription());
jComboBox2.setSelectedIndex(room.get_roomHasPhone() ? 0 : 1);
jComboBox1.setSelectedIndex(room.get_roomOfferBreakfast() ? 0 : 1);
}
public void setValues(Room room){
if(room == null){
// add room info
/*
todo set value of default info
*/
showRoomInfo(new Room());
getSubmitButton().setText(" Add ");
}
else{
//modify room info
/*
todo set value of room
*/
_currentRoom = room;
showRoomInfo(_currentRoom);
jLabel.setText(" Modify a room...");
getSubmitButton().setText("Modify");
}
}
public void OnSubmit(){
Room newRoom = (_currentRoom!=null)?_currentRoom:(new Room());
newRoom.set_roomType(jComboBox.getSelectedIndex());
newRoom.set_roomPrice(Float.parseFloat(jTextField.getText()));
newRoom.set_roomDescription(jTextArea.getText());
newRoom.set_roomHasPhone(jComboBox2.getSelectedIndex() == 0 ? true : false);
newRoom.set_roomOfferBreakfast(jComboBox1.getSelectedIndex() == 0 ? true : false);
RoomManager mgr = RoomManager.get_INSTANCE();
if (jButton.getText().equals(" Add ")||jButton.getText().equals("Submit")) {
if (mgr.AddRoom(newRoom)){
JOptionPane.showMessageDialog(this, "Add room succeeded!", "Message", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Add room failed!", "Error", JOptionPane.ERROR_MESSAGE);
}
}else if (jButton.getText().equals("Modify")) {
if (mgr.ModifyRoom(newRoom)){
JOptionPane.showMessageDialog(this, "Modify room succeeded!", "Message", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Modify room failed!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent ev) {
if("SUBMIT" == ev.getActionCommand()){
OnSubmit();
_currentRoom = null;
System.out.println("roompanel submit button clicked");
}
else if("CANCEL" == ev.getActionCommand()){
System.out.println("roompanel cancel button cliked");
setValues(_currentRoom);
}
else if("EXIT" == ev.getActionCommand()){
JFrame parent = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, jButton1);
parent.dispose();
}
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Room");
frame.setResizable(false);
frame.setSize(640, 320);
frame.setContentPane(new RoomPanel());
//frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocation(100,100);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -