-1

我正在做一个家庭作业,它几乎要求用户在两个房间之间进行选择,并计算在那个房间里呆几个星期需要多少钱。

所以我试图让我的程序从要求在房间之间进行选择开始,然后,在做出选择之后,让用户输入一定的周数,然后将房间的成本乘以周数.

我想我已经完成了我的代码的第一部分,但我完全不知道如何创建第二部分。

这是我到目前为止所拥有的:

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();
}}
4

2 回答 2

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);
    }
}
于 2013-08-02T01:32:49.293 回答
0

首先,您应该限制转储给用户的帧/窗口的数量。请参阅多个 JFrame 的使用:好的做法还是坏的做法?了解更多详情。

相反,我会单独创建每个“屏幕”,JPanel并根据需要使用类似 a 的东西CardLayout在这些视图之间切换。

于 2013-08-02T01:25:50.840 回答