我正在完成一项家庭作业,它几乎要求用户在两个房间之间进行选择,并计算在该房间停留一定数周所需的费用。
所以我试图通过要求在房间之间进行选择来开始我的程序,然后,在做出选择之后,让用户输入一定数周的周期,并将房间的成本乘以周数。
我想我的代码的第一部分已完成,但我完全迷失了如何创建第二部分。
这是我到目前为止所做的:
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 :(得分: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);
}
}
答案 1 :(得分:0)
首先,您应该限制转储到用户的帧/窗口数。有关更多详细信息,请参阅The Use of Multiple JFrames: Good or Bad Practice?。
相反,我会在单独的JPanel
中创建每个“屏幕”,并使用类似CardLayout
的内容根据需要在这些视图之间切换。