用于复选框gui的java with if语句
大家好,请您协助实施以下内容: -风格比萨饼gui的拖放菜单 肉:火腿、鸡肉和意大利香肠 海鲜:虾、贻贝和扇贝 蔬菜:从附加配料列表中选择 -带有“帮助”和“退出”的“帮助”菜单栏 -请求/添加新的浇头按钮,带有弹出框,要求用户输入浇头,然后在下面的两个按钮中单击一个按钮,当单击时,它将浇头发送到电子邮件地址,或者如果单击添加浇头,则另一个弹出框要求管理员输入授权代码,设置为123456,然后单击另一个按钮,显示添加浇头这会将顶部添加到顶部菜单 -二维图形 以下是我目前的代码:用于复选框gui的java with if语句,java,user-interface,if-statement,Java,User Interface,If Statement,大家好,请您协助实施以下内容: -风格比萨饼gui的拖放菜单 肉:火腿、鸡肉和意大利香肠 海鲜:虾、贻贝和扇贝 蔬菜:从附加配料列表中选择 -带有“帮助”和“退出”的“帮助”菜单栏 -请求/添加新的浇头按钮,带有弹出框,要求用户输入浇头,然后在下面的两个按钮中单击一个按钮,当单击时,它将浇头发送到电子邮件地址,或者如果单击添加浇头,则另一个弹出框要求管理员输入授权代码,设置为123456,然后单击另一个按钮,显示添加浇头这会将顶部添加到顶部菜单 -二维图形 以下是我目前的代码: //Packag
//Packages
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class CaseyPizza extends JFrame
{
private static final long serialVersionUID = 1L;
private JFrame frame;
private JPanel pizzaPanel, centerPanel, pricePanel, checkBoxPanel, radioButtonPanel;
private final int FRAME_WIDTH = 600;
private final int FRAME_HEIGHT = 400;
private ButtonGroup group;
private JRadioButton smallButton, mediumButton, largeButton;
private JCheckBox olivCheckBox, mushCheckBox, pineapCheckBox, capsiCheckBox;
private JTextField priceTextField;
private double price = 0.0;
private double topPrice = 0.0;
private double showPrice = 0.0;
private ActionListener listener = new PriceListener();
public CaseyPizza() {
pizzaPanel = new JPanel();
pizzaPanel.setLayout(new BorderLayout(10, 10));
createRadioButtonPanel();
createCheckBoxPanel();
createPricePanel();
createCenterPanel();
pizzaPanel.add(centerPanel, BorderLayout.CENTER);
pizzaPanel.add(pricePanel, BorderLayout.SOUTH);
frame = new JFrame("Casey Pizza Ordering System");
frame.add(pizzaPanel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(100, 100);
frame.setVisible(true);
}
private void createRadioButtonPanel() {
radioButtonPanel = new JPanel();
radioButtonPanel.setLayout(new GridLayout(3, 1));
radioButtonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));
group = new ButtonGroup();
smallButton = new JRadioButton(" Small ");
group.add(smallButton);
smallButton.addActionListener(listener);
radioButtonPanel.add(smallButton);
mediumButton = new JRadioButton(" Medium ");
group.add(mediumButton);
mediumButton.addActionListener(listener);
radioButtonPanel.add(mediumButton);
largeButton = new JRadioButton(" Large ");
group.add(largeButton);
largeButton.addActionListener(listener);
radioButtonPanel.add(largeButton);
}
// add the check boxes to this frame
// add action listener for the check boxes
private void createCheckBoxPanel() {
checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new GridLayout(4, 1));
olivCheckBox = new JCheckBox(" Olives ");
olivCheckBox.addActionListener(listener);
checkBoxPanel.add(olivCheckBox);
mushCheckBox = new JCheckBox(" Mushrooms ");
mushCheckBox.addActionListener(listener);
checkBoxPanel.add(mushCheckBox);
pineapCheckBox = new JCheckBox(" Pineapple ");
pineapCheckBox.addActionListener(listener);
checkBoxPanel.add(pineapCheckBox);
capsiCheckBox = new JCheckBox(" Capsicum ");
capsiCheckBox.addActionListener(listener);
checkBoxPanel.add(capsiCheckBox);
}
//Price Panel
private void createPricePanel() {
pricePanel = new JPanel();
pricePanel.add(new JLabel("Your Price:"));
priceTextField = new JTextField(7);
priceTextField.setFont(new Font("Serif", Font.BOLD, 16));
priceTextField.setEditable(false);
priceTextField.setForeground(Color.blue);
priceTextField.setBackground(pricePanel.getBackground());
priceTextField.setDisabledTextColor(Color.blue);
priceTextField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
pricePanel.add(priceTextField);
priceTextField.setText(" n/a Price");
}
private void createCenterPanel() {
centerPanel = new JPanel();
centerPanel.add(radioButtonPanel);
centerPanel.add(checkBoxPanel);
}
//Method for price of pizza build
private class PriceListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
topPrice = 0;
if (smallButton.isSelected()) {
price = 3.00;
} else if (mediumButton.isSelected()) {
price = 6.00;
} else if (largeButton.isSelected()) {
price = 8.00;
}
if(olivCheckBox.isSelected())
{
topPrice += 0.50;
}
if(mushCheckBox.isSelected())
{
topPrice += 0.50;
}
if(pineapCheckBox.isSelected())
{
topPrice += 0.50;
}
if(capsiCheckBox.isSelected())
{
topPrice += 0.50;
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
showPrice = price + topPrice;
priceTextField.setText(" $" + showPrice);
System.out.println("Price dispayed");
}
});
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
CaseyPizza pPF = new CaseyPizza();
System.out.println("Run");
}
});
}
}
应该是
if (olivCheckBox.isSelected() && mushCheckBox.isSelected() && pineapCheckBox.isSelected() && capsiCheckBox.isSelected()){
对于所有其他if语句,也会进行类似的更改
或者类似的
if((olivCheckBox.isSelected())&&&(mushCheckBox.isSelected())&(pineapCheckBox.isSelected())&&&(capsiCheckBox.isSelected())
{
对于所有if/else if语句
前三个条件句的语法不正确:
if ((olivCheckBox.isSelected()) && (mushCheckBox.isSelected()) && (pineapCheckBox.isSelected()) && (capsiCheckBox.isSelected())){
topPrice = 2.00;
} else if ((olivCheckBox.isSelected()) && (mushCheckBox.isSelected()) && (pineapCheckBox.isSelected())) {
topPrice = 1.50;
} else if ((olivCheckBox.isSelected()) && (mushCheckBox.isSelected())) {
topPrice = 1.00;
}
你缺少if/else if的括号。也许我没有抓住重点,但是 如果每个顶部都是
0.50
,为什么不使用一系列If
语句来检查选中了哪些复选框,并将0.50
添加到topPrice
,例如
topPrice = 0;
if(olivCheckBox.isSelected())
{
topPrice += 0.50;
}
if(mushCheckBox.isSelected())
{
topPrice += 0.50;
}
if(pineapCheckBox.isSelected())
{
topPrice += 0.50;
}
if(capsiCheckBox.isSelected())
{
topPrice += 0.50;
}
你甚至可以把它们放在一个数组中,然后简单地在上面循环
JCheckBox toppings[] = new JCheckBox[] {
olivCheckBox,
mushCheckBox,
pineapCheckBox,
capsiCheckBox
};
topPrice = 0;
for (JCheckBox topping : toppings) {
if (topping.isSelected()) {
topPrice += 0.50;
}
}
这样,如果你想添加更多的配料,你就不需要重新编写大量代码了……感谢你的知识,这是有意义的,但仍然不起作用。它计算橄榄和蘑菇复选框,但不计算菠萝和辣椒复选框,或者当所有4个复选框都选中时检查你的if-else语句顺序,尝试反转ordEr到底什么不起作用?你能再澄清一下你的问题吗?如果所有的成分都是0.50,为什么不从0开始,简单地使用一个If语句,为每个选定的项目加上0.50,而不是使用If-else-If…?我有4个复选框oliv、mush、pineap和Capis。每个都设置为0.50美元,所以我需要找出如何使每个co的If值为0.50美元要计算的组合和所选复选框集。当前仅在复选框中勾选时计算oliv和mush是的,我的知识可以进行两次计算,但不是四次,如果您可以分享您关于如何实现您所说的内容的知识great@CaseyT可以做,检查答案…你的第一系列if语句nts工作但是我现在遇到的问题是pineap和Capis不计算和添加。50Aweasome感谢现在运行良好。如果我需要更多的知识来实现其他功能,你能帮助我吗?我该怎么做?最好的办法是尝试一些东西并问更多的问题;)到目前为止,我用完整的代码重新发布了我的问题非常困难的任务所有这些gui如此多的编码,在没有知识的情况下让它们一起工作更加困难。我甚至找不到资源或帮助将它们集成在一起