所以我的 Java 程序有问题。我目前希望它有 3 个主菜选项汉堡包、比萨饼和沙拉以及附加选项。现在,该程序从选择汉堡开始,并且可用的附加组件也计算价格。When the main item is selected the add-on options should change with the new item however they don't. 我已经盯着这个看了一段时间,所以可能是我错过了一些非常简单的东西,比如清理区域然后显示新的附加选项。在任何情况下,任何帮助都将不胜感激,这是当前代码。
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
public class LunchOrder extends JFrame {
private JRadioButton hamburgerJButton, pizzaJButton, saladJButton;
private JCheckBox lettuceButton, mayonnaiseButton, mustardButton, pepperoniButton,
sausageButton, mushroomsButton, croutonsButton, baconBitsButton, breadSticksButton;
private JLabel subTotal, tax, totalDue;
private JTextField subTotalText, taxText, totalDueText;
private JButton placeOrder, clearOrder, exitButton;
// no-argument constructor
public LunchOrder()
{
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null);
hamburgerJButton = new JRadioButton("Hamburger - $6.95" );
hamburgerJButton.setSelected(true);
pizzaJButton = new JRadioButton("Pizza - $5.95");
pizzaJButton.setSelected(false);
saladJButton = new JRadioButton("Salad - $4.95");
saladJButton.setSelected(false);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(hamburgerJButton);
bgroup.add(pizzaJButton);
bgroup.add(saladJButton);
JPanel mainCourse = new JPanel();
mainCourse.setLayout( new GridLayout( 3, 1 ));
mainCourse.setBounds( 10, 10, 150,135 );
mainCourse.add(hamburgerJButton);
mainCourse.add(pizzaJButton);
mainCourse.add(saladJButton);
mainCourse.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Main course" ) );
//contentPane.add( mainCourseJPanel, BorderLayout.NORTH );
contentPane.add( mainCourse );
//Add action listener to created button
//JCheckBox
lettuceButton = new JCheckBox("Lettuce, tomato, and onions");
lettuceButton.setSelected(true);
mayonnaiseButton= new JCheckBox("Mayonnaise");
mayonnaiseButton.setSelected(false);
mustardButton = new JCheckBox("Mustard");
mustardButton.setSelected(true);
pepperoniButton = new JCheckBox("Pepperoni");
sausageButton= new JCheckBox("Sausage");
mushroomsButton = new JCheckBox("Mushrooms");
croutonsButton = new JCheckBox("Croutons");
baconBitsButton= new JCheckBox("Bacon bits");
breadSticksButton = new JCheckBox("Bread sticks");
//JPanel addons
JPanel addOns = new JPanel();
GridLayout addOnGlay = new GridLayout(3,3);
addOns.setLayout(addOnGlay);
addOns.setBounds( 250, 10, 250, 135 );
addOns.add(lettuceButton);
addOns.add(pepperoniButton);
addOns.add(croutonsButton);
addOns.add(mayonnaiseButton);
addOns.add(sausageButton);
addOns.add(baconBitsButton);
addOns.add(mustardButton);
addOns.add(mushroomsButton);
addOns.add(breadSticksButton);
pepperoniButton.setVisible(false);
sausageButton.setVisible(false);
mushroomsButton.setVisible(false);
croutonsButton.setVisible(false);
baconBitsButton.setVisible(false);
breadSticksButton.setVisible(false);
addOns.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Add ons($.25/each)" ) );
contentPane.add( addOns );
// subtotal JLabel
subTotal = new JLabel();
subTotal.setBounds(10, 110, 100, 200);
contentPane.add(subTotal);
subTotal.setText( "Subtotal: " );
subTotal.setHorizontalAlignment(JLabel.RIGHT);
// subtotal JTextField
subTotalText = new JTextField();
subTotalText.setBounds(115, 200, 80, 22);
subTotalText.setHorizontalAlignment(JTextField.LEFT);
contentPane.add(subTotalText);
// Tax JLabel
tax = new JLabel();
tax.setBounds(10, 135, 100, 200);
contentPane.add(tax);
tax.setText("Tax(7.85%) ");
tax.setHorizontalAlignment(JLabel.RIGHT);
// Tax JTextField
taxText = new JTextField();
taxText.setBounds(115, 225, 80, 22);
contentPane.add(taxText);
// total due JLabel
totalDue = new JLabel();
totalDue.setBounds(10, 160, 100, 200);
contentPane.add(totalDue);
totalDue.setText("Total due: " );
totalDue.setHorizontalAlignment(JLabel.RIGHT);
// total due JTextField
totalDueText = new JTextField();
totalDueText.setBounds(115, 250, 80, 22);
contentPane.add(totalDueText);
// order total JPanel
JPanel orderTotal = new JPanel();
GridLayout orderGLay = new GridLayout(3,1);
orderTotal.setLayout(orderGLay);
orderTotal.setBounds(10, 170, 200, 125 );
orderTotal.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Order total" ) );
contentPane.add( orderTotal );
// place order JButton
placeOrder = new JButton();
placeOrder.setBounds( 252, 175, 100, 24 );
placeOrder.setText( "Place order" );
contentPane.add( placeOrder );
placeOrder.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEvent event) {
placeOrderActionPerformed(event);
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearOrderJButton
clearOrder = new JButton();
clearOrder.setBounds(252,210, 100, 24 );
clearOrder.setText( "Clear order" );
contentPane.add( clearOrder );
clearOrder.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEvent event) {
clearOrderActionPerformed(event);
}
} // end anonymous inner class
); // end call to addActionListener
// set up exitJButton
exitButton = new JButton();
exitButton.setBounds( 425, 260, 70, 24 );
exitButton.setText( "Exit" );
contentPane.add( exitButton );
// set properties of application's window
setTitle( "Lunch order" ); // set window title
setResizable(true); // prevent user from resizing window
setSize( 525, 350 ); // set window size
setVisible( true ); // display window
setLocationRelativeTo(null);
}
// calculate subtotal plus tax
private void placeOrderActionPerformed(ActionEvent event) {
DecimalFormat dollars = new DecimalFormat("$0.00");
// declare double variables
double hamburgerPrice = 6.95;
double pizzaPrice = 5.95;
double saladPrice = 4.95;
double addons = 0;
double subTotPrice;
double taxPercent;
double totalDuePrice;
if ( hamburgerJButton.isSelected())
{
if( lettuceButton.isSelected()){
addons += 0.25;
}
if( mayonnaiseButton.isSelected()){
addons += 0.25;
}
if( mustardButton.isSelected()){
addons += 0.25;
}
else{
addons -= 0.25;
}
subTotPrice = hamburgerPrice + addons;
taxPercent = subTotPrice * 0.0785;
totalDuePrice = subTotPrice + taxPercent;
//display subtotal, tax and total due
subTotalText.setText( dollars.format( subTotPrice ));
taxText.setText( dollars.format( taxPercent));
totalDueText.setText( dollars.format( totalDuePrice ));
}
if ( pizzaJButton.isSelected())
{
//lettuceButton.setVisible(false);
//mayonnaiseButton.setVisible(false);
//mustardButton.setVisible(false);
croutonsButton.setVisible(false);
baconBitsButton.setVisible(false);
breadSticksButton.setVisible(false);
pepperoniButton.setVisible(true);
sausageButton.setVisible(true);
mushroomsButton.setVisible(true);
//calculation for pizza selection
if( pepperoniButton.isSelected())
addons += 0.25;
if( sausageButton.isSelected())
addons += 0.25;
if( mushroomsButton.isSelected())
addons += 0.25;
else
addons -= 0.25;
subTotPrice = (pizzaPrice + addons);
taxPercent = subTotPrice * 0.0785;
totalDuePrice = subTotPrice + taxPercent;
//display subtotal, tax and total due
subTotalText.setText( dollars.format( subTotPrice ));
taxText.setText( dollars.format( taxPercent));
totalDueText.setText( dollars.format( totalDuePrice ));
}
if ( saladJButton.isSelected())
{
croutonsButton.setVisible(true);
baconBitsButton.setVisible(true);
breadSticksButton.setVisible(true);
if( croutonsButton.isSelected())
addons += 0.25;
if( baconBitsButton.isSelected())
addons += 0.25;
if( breadSticksButton.isSelected())
addons += 0.25;
else
addons -= 0.25;
//calculation for salad selection
subTotPrice = (saladPrice + addons);
taxPercent = subTotPrice * 0.0785;
totalDuePrice = subTotPrice + taxPercent;
//display subtotal, tax and total due
subTotalText.setText( dollars.format( subTotPrice ));
taxText.setText( dollars.format( taxPercent));
totalDueText.setText( dollars.format( totalDuePrice ));
}
} // end method calculateJButtonActionPerformed
private void clearOrderActionPerformed(ActionEvent event) {
//reset hamburger and addons to default state
hamburgerJButton.setSelected(true);
lettuceButton.setSelected(true);
mayonnaiseButton.setSelected(false);
mustardButton.setSelected(true);
subTotalText.setText("");
taxText.setText("");
totalDueText.setText("");
} // end method calculateJButtonActionPerformed
public static void main( String args[] )
{
LunchOrder application = new LunchOrder();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}