gpt4 book ai didi

java - 单选按钮组和额外选项

转载 作者:行者123 更新时间:2023-12-01 23:26:48 26 4
gpt4 key购买 nike

所以我的 Java 程序有问题。我目前希望它有 3 种主菜选项,汉堡包、披萨和沙拉以及附加选项。现在,该程序从选择的汉堡包开始,并且他们还会计算可用的附加组件的价格。选择主项目后,附加选项应随新项目而更改,但事实并非如此。我已经盯着这个问题有一段时间了,所以可能是我错过了一些非常简单的东西,比如清理该区域,然后显示新的附加选项。无论如何,我们将不胜感激,这是当前的代码。

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

}

最佳答案

您还没有添加任何可以通知任何类型状态更改的监听器,Java 无法神奇地知道您想要它做什么...我希望...

您可以在每个按钮上使用 ItemListener,并根据所选内容对您的 UI 进行更改,例如...

创建一个 ItemListener...

ItemListener il = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
System.out.println("Hamburger = " + hamburgerJButton.isSelected());
System.out.println("Pizza = " + pizzaJButton.isSelected());
System.out.println("Salad = " + saladJButton.isSelected());
}
}
};

初始化按钮后,将其注册到每个按钮...

hamburgerJButton.addItemListener(il);
pizzaJButton.addItemListener(il);
saladJButton.addItemListener(il);

看看How to use buttons了解更多详情

关于java - 单选按钮组和额外选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19871233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com