在阅读了您的问题后,这就是我想出的。我尽可能简单地满足您的需求。大多数行都有注释。
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);
}
}