Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Swing编程中创建第二个选项窗口_Java_Swing_User Interface - Fatal编程技术网

Java 在Swing编程中创建第二个选项窗口

Java 在Swing编程中创建第二个选项窗口,java,swing,user-interface,Java,Swing,User Interface,我正在做一个家庭作业,它几乎要求用户在两个房间中进行选择,并计算在该房间中待上几周的费用 因此,我试图让我的程序从要求在房间之间进行选择开始,然后,在做出选择之后,让用户输入一定的周数,并将房间的成本乘以周数 我想我已经完成了代码的第一部分,但是我完全不知道如何创建第二部分 以下是我到目前为止的情况: public class hotel extends JFrame { JCheckBox b1, b2, b3; JPanel p1; JLabel l; JTextField t; hote

我正在做一个家庭作业,它几乎要求用户在两个房间中进行选择,并计算在该房间中待上几周的费用

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

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

以下是我到目前为止的情况:

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

首先,您应该限制转储到用户上的帧/窗口的数量。有关更多详细信息,请参阅


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

在阅读了您的问题后,我想到了这一点。为了满足你的需要,我尽量简单。大多数台词上都有评论

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

你应该使用GUI吗?没有它会容易得多。首先,你需要将面板添加到框架中。然后需要将一些实例变量设置为每天的美元金额。然后你需要有一个完整的'另一个小组旁边的一个你必须收集天数等。(你可以在同一个面板中使用字段或微调器。)你是否可以使用“计算”按钮,这样就不必检测用户何时更改天数?@LeeMeador我想是的,比如说,我选择了1号房间,我想要它一周,我会点击calculate按钮并得到最终值,这意味着您需要一个文本字段,他们可以输入天数(或其他允许输入数字的gui组件)和一个按钮。在按钮上放置一个侦听器,如果选中任何复选框,该按钮将重新计算。复选框上不需要侦听器。“计算”按钮上的侦听器将知道三个复选框中每个复选框的每日成本。@AbdullahTamimi您可能应该使用
ActionListener
而不是
ItemListener