2013-08-01 51 views
-1

我正在做一個家庭作業,它幾乎要求用戶在兩個房間之間進行選擇,並計算在特定的幾周內停留在該房間的費用。在Swing編程中創建第二個選項窗口

所以我想通過要求在房間之間進行選擇來啓動我的程序,然後,在做出選擇之後,讓用戶輸入一定的週數,並乘以房間的成本按週數計算。

我想我已經完成了我的代碼的第一部分,但我完全失去了如何創建第二部分。

這是我到目前爲止有:

public class hotel extends JFrame { 
JCheckBox b1, b2, b3; 
JPanel p1; 
JLabel l; 
JTextField t; 

hotel() { 
    setTitle("Hotel Room Selection"); 
    setSize(300,400); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    l = new JLabel("Hotel."); 
    t = new JTextField("Select a room type"); 

    b1 = new JCheckBox("b1"); 
    b2 = new JCheckBox("b2"); 
    b3 = new JCheckBox("b3"); 
    setLayout(new FlowLayout()); 
    p1.add(b1); 
    p1.add(b2); 
    p1.add(b3); 
    p1.add(l); 
    p1.add(t); 

    b1.addItemListener(new cbListener()); 
    b2.addItemListener(new cbListener()); 
    b3.addItemListener(new cbListener()); 

    setVisible(true); 
} 

class cbListener implements ItemListener{ 
    public void itemStateChanged(ItemEvent b) 
    { 
     if(b.getSource() == b1) 
     { 
      if (b1.isSelected()) 
      { 
       int room1 = 600; 
      } 

     } 
     else if (b.getSource() == b2) 
     { 
      if (b2.isSelected()) 
      { 
       int room2=850; 
      } 

     } 
     if (b.getSource() == b3) 
     { 
      if (b3.isSelected()) 
      { 
       int boat=60; 
      } 

      } 
     } 

    } 

public static void main(String [] args) 
{ 
     Hotel a1 = new Hotel(); 
}} 
+0

您是否應該使用GUI?沒有它就會容易得多。 – tbodt

+0

首先,您需要將面板添加到框架中。然後你需要設置一些實例變量,以每天的美元金額。然後,你需要在你需要收集的天數等的旁邊有一個完整的'nother面板。 (您可以在同一個面板中使用字段或微調器。)您是否允許使用「計算」​​按鈕,以便您不必檢測用戶何時更改天數? –

+0

@tbodt是的我是 –

回答

1

看完你的問題後,這就是我想出的。我保持儘可能簡單,以滿足您的需求。大多數評論都有評論。

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JRadioButton; 

import java.awt.FlowLayout; 

import javax.swing.border.TitledBorder; 
import javax.swing.JSpinner; 
import javax.swing.JLabel; 
import javax.swing.UIManager; 

import java.awt.Color; 
import java.text.NumberFormat; 

import javax.swing.event.ChangeListener; 
import javax.swing.event.ChangeEvent; 

public class HotelFrame extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    private JLabel lblTotalCost; 
    private JRadioButton rdbtnRoomSuite; 
    private JRadioButton rdbtnRoomStandard; 

    private double totalCost = 0; 
    private double cost = 0; 
    private JSpinner spinner; 

    // Room Costs 
    private final double SUITE_COST = 300.00; 
    private final double STANDARD_COST = 100.00; 

    /** 
    * Create the frame. 
    */ 
    public HotelFrame() 
    { 
     // Frame Values 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 263, 147); 

     // Primary Panel 
     JPanel contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 

     // Panel for room type and a border 
     JPanel panelRoomType = new JPanel(); 
     panelRoomType.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Room Type", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); 
     contentPane.add(panelRoomType); 
     panelRoomType.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 

     // Standard room radio button 
     rdbtnRoomStandard = new JRadioButton("Standard"); 
     panelRoomType.add(rdbtnRoomStandard); 

     // Add Action Listener to standard radio button 
     rdbtnRoomStandard.addActionListener(new RadioButtonListener()); 

     // Suite room radio button 
     rdbtnRoomSuite = new JRadioButton("Suite"); 
     panelRoomType.add(rdbtnRoomSuite); 

     // Add Action Listener to Suite radio button 
     rdbtnRoomSuite.addActionListener(new RadioButtonListener()); 

     // Button group to link standard/suite radio buttons 
     ButtonGroup group = new ButtonGroup(); 
     group.add(rdbtnRoomStandard); 
     group.add(rdbtnRoomSuite); 

     // Panel for calculating cost 
     JPanel panelCostCalc = new JPanel(); 
     contentPane.add(panelCostCalc); 
     panelCostCalc.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); 

     // Label for weeks 
     JLabel lblNewLabel = new JLabel("Weeks"); 
     panelCostCalc.add(lblNewLabel); 

     // Spinner to select week 
     spinner = new JSpinner(); 
     panelCostCalc.add(spinner); 

     // Change Listener for updating the week 
     spinner.addChangeListener(new SpinnerListener()); 

     // Label displaying total costs - Field Variable 
     lblTotalCost = new JLabel("Total Cost = $0.00"); 
     panelCostCalc.add(lblTotalCost); 
    } 

    /** 
    * Update Total Cost and Label 
    */ 
    private void updateTotalCost() 
    { 
     // Week value was changed. Recalculate total 
     totalCost = cost * (Integer) spinner.getValue(); 

     // Formatter for currency 
     NumberFormat fmt = NumberFormat.getCurrencyInstance(); 

     // Update Total Cost label 
     lblTotalCost.setText("Total Cost = " + fmt.format(totalCost)); 
    } 

    /** 
    * Action Listener class for radio buttons 
    */ 
    class RadioButtonListener implements ActionListener 
    { 
     @Override 
     public void actionPerformed(ActionEvent a) 
     { 
      // Room was selected 
      if(a.getSource().equals(rdbtnRoomStandard)) 
      { 
       // Standard room selected 
       cost = STANDARD_COST; 

      } 
      else if(a.getSource().equals(rdbtnRoomSuite)) 
      { 
       // Suite room selected 
       cost = SUITE_COST; 
      } 

      // Ensure total cost is updated 
      updateTotalCost(); 
     } 
    } 

    /** 
    * Change Listener class for week spinner 
    */ 
    class SpinnerListener implements ChangeListener 
    { 
     @Override 
     public void stateChanged(ChangeEvent e) 
     { 
      updateTotalCost(); 
     } 
    } 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) 
    { 
     HotelFrame frame = new HotelFrame(); 
     frame.setVisible(true); 
    } 
} 
相關問題