我正在使用Java中的GUI。我正在尝试为想象中的餐厅创建一个程序,它包括2个课程。
布局:
如果用户要为订单选择商品,或者从订单中删除商品,则中间有2个按钮。
另一个班级显示小计,税,并为小费添加几个选项,最后将显示总数。
对于带菜单的类,我有一个初始化为0的总变量。
如果用户想要在他的订单中添加项目,则需要单击特定按钮。然后它会更新总价。
我正在创建此变量static
,以便显示总计的类可以与之交互。
我遇到的问题是,在显示总数的类中此变量似乎不会更新。它仍然显示总变量为0,即使我直接从其他类调用它。
如果有人有任何想法或任何可以帮助我的提示,请这样做,因为我严重坚持这一点。谢谢
P.S。这些类是另一个类的一部分,它将创建这些类的对象,因此这些类没有public static void main()方法
这是我的课程,显示餐厅菜单。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MenuFrame extends JPanel implements ActionListener {
final double BURGER_PRICE = 6.24;
final double FISH_PRICE = 13.78;
final double SOUP_PRICE = 4.49;
final double CHICKEN_PRICE = 11.36;
final double SALAD_PRICE = 5.60;
final double FRIES_PRICE = 3.24;
JPanel menuPanel = new JPanel(); //Panel to place the menu in, which will be on left side
JPanel buttonPanel = new JPanel(); //Panel to place buttons in, which will be in center
JPanel orderPanel = new JPanel(); //Panel to place order menu in, which will be on right side
JLabel menuLabel = new JLabel("Menu"); //Label for the menu
JLabel orderLabel = new JLabel("Your Order"); //Label for the user's order
JCheckBox burger = new JCheckBox("Burger: $6.24", false); //Burger check box
JCheckBox fish = new JCheckBox("Fish: $13.78", false); //Fish check box
JCheckBox soup = new JCheckBox("Soup: $4.49", false); //Soup check box
JCheckBox chicken = new JCheckBox("Chicken: $11.36", false); //Chicken check box
JCheckBox salad = new JCheckBox("Salad: $5.60", false); //Salad check box
JCheckBox fries = new JCheckBox("Fries: $3.24", false); //Fries check box
JButton rightButton = new JButton("->"); //Button to move items from left to right
JButton leftButton = new JButton("<-"); //Button to move items from right to left
static double totalPrice = 0; //Total price of order
Font titleFont = new Font("Times New Roman",Font.BOLD,20);
public MenuFrame() {
setLayout(new GridLayout(0,3)); //Setting this panel's layout to GridLayout
setSize(1055, 275); //Set size of Menu Frame
menuPanel.setLayout(new GridLayout(0,3)); //Setting menuPanel layout to GridLayout
buttonPanel.setLayout(new GridLayout(8,3)); //Setting buttonPanel layout to GridLayout
orderPanel.setLayout(new GridLayout(0,3)); //Setting orderPanel layout to GridLayout
menuLabel.setFont(titleFont); //Setting font's of menuLabel and orderLabel to titleFont
orderLabel.setFont(titleFont);
//Adding menu items to Menu //Adding menu items to menuPanel, along with adding new JLabel's to skip positions
menuPanel.add(new JLabel("")); //in GridLayout
menuPanel.add(menuLabel);
menuPanel.add(new JLabel(""));
menuPanel.add(burger);
menuPanel.add(fish);
menuPanel.add(soup);
menuPanel.add(chicken);
menuPanel.add(salad);
menuPanel.add(fries);
//Adding "Your Order" label to orderPanel. Items will be subsequently added or removed. Also adding new JLabel's to skip positions
orderPanel.add(new JLabel(""));
orderPanel.add(orderLabel);
orderPanel.add(new JLabel(""));
//Adding new JLabels, rightButton, and leftButton so that the buttons will be placed on top of each other
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(rightButton);
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(leftButton);
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
buttonPanel.add(new JLabel(""));
//Adding action listener to rightButton and leftButton
rightButton.addActionListener(this);
leftButton.addActionListener(this);
//Adding menuPanel, buttonPanel, and orderPanel to MenuFrame
add(menuPanel);
add(buttonPanel);
add(orderPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
//If right button was clicked
if(source == rightButton) {
//if burger was selected and is in the menu panel
if(burger.isSelected() && burger.getParent() == menuPanel) {
//Move burger from menu to order, deselect burger, and add 6.24 to total price
menuPanel.remove(burger);
orderPanel.add(burger);
burger.setSelected(false);
totalPrice += BURGER_PRICE;
}
//if fish was selected and is in the menu panel
if(fish.isSelected() && fish.getParent() == menuPanel) {
//Move fish from menu to order, deselect fish, and add 13.78 to total price
menuPanel.remove(fish);
orderPanel.add(fish);
fish.setSelected(false);
totalPrice += FISH_PRICE;
}
//if soup was selected and is in the menu panel
if(soup.isSelected() && soup.getParent() == menuPanel) {
//Move soup from menu to order, deselect soup, and add 4.49 to total price
menuPanel.remove(soup);
orderPanel.add(soup);
soup.setSelected(false);
totalPrice += SOUP_PRICE;
}
//if chicken was selected and is in the menu panel
if(chicken.isSelected() && chicken.getParent() == menuPanel) {
//Move chicken from menu to order, deselect chicken, and add 11.36 to total price
menuPanel.remove(chicken);
orderPanel.add(chicken);
chicken.setSelected(false);
totalPrice += CHICKEN_PRICE;
}
//if salad was selected and is in the menu panel
if(salad.isSelected() && salad.getParent() == menuPanel) {
//Move salad from menu to order, deselect salad, and add 5.60 to total price
menuPanel.remove(salad);
orderPanel.add(salad);
salad.setSelected(false);
totalPrice += SALAD_PRICE;
}
//if fries was selected and is in the menu panel
if(fries.isSelected() && fries.getParent() == menuPanel) {
//Move fries from menu to order, deselect fries, and add 3.24 to total price
menuPanel.remove(fries);
orderPanel.add(fries);
fries.setSelected(false);
totalPrice += FRIES_PRICE;
}
}
//if left button was clicked
if(source == leftButton) {
//if burger was selected and is in the order panel
if(burger.isSelected() && burger.getParent() == orderPanel) {
//Move burger from order to menu, deselect burger, and remove 6.24 from total price
orderPanel.remove(burger);
menuPanel.add(burger);
burger.setSelected(false);
totalPrice -= BURGER_PRICE;
}
//if fish was selected and is in the order panel
if(fish.isSelected() && fish.getParent() == orderPanel) {
//Move fish from order to menu, deselect fish, and remove 13.78 from total price
orderPanel.remove(fish);
menuPanel.add(fish);
fish.setSelected(false);
totalPrice -= FISH_PRICE;
}
//if soup was selected and is in the order panel
if(soup.isSelected() && soup.getParent() == orderPanel) {
//Move soup from order to menu, deselect soup, and remove 4.49 from total price
orderPanel.remove(soup);
menuPanel.add(soup);
soup.setSelected(false);
totalPrice -= SOUP_PRICE;
}
//if chicken was selected and is in the order panel
if(chicken.isSelected() && chicken.getParent() == orderPanel) {
//Move chicken from order to menu, deselect chicken, and remove 11.35 from total price
orderPanel.remove(chicken);
menuPanel.add(chicken);
chicken.setSelected(false);
totalPrice -= CHICKEN_PRICE;
}
//if salad was selected and is in the order panel
if(salad.isSelected() && salad.getParent() == orderPanel) {
//Move salad from order to menu, deselect salad, and remove 5.60 from total price
orderPanel.remove(salad);
menuPanel.add(salad);
salad.setSelected(false);
totalPrice -= SALAD_PRICE;
}
//if fries was selected and is in the order panel
if(fries.isSelected() && fries.getParent() == orderPanel) {
//Move fries from order to menu, deselect fries, and remove 3.24 from total price
orderPanel.remove(fries);
menuPanel.add(fries);
fries.setSelected(false);
totalPrice -= FRIES_PRICE;
}
}
//Make changes appear
repaint();
revalidate();
}
此类是显示总额和税金的类
package finalproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TaxAndGratuityFrame extends JPanel implements ActionListener {
final double TAX_RATE = 0.07; //Tax rate
JLabel subtotal = new JLabel("Subtotal"); //Sub-total label
JLabel tax = new JLabel("Tax"); //Tax label
JLabel gratuity = new JLabel("Gratuity"); //Gratuity label
JLabel total = new JLabel("Total"); //Total label
JLabel other = new JLabel("Other $"); //Other label for user entered gratuity
JTextField subtotalTextField = new JTextField(10); //Displays sub-total
JTextField taxTextField = new JTextField(10); //Displays 8% tax
JTextField otherGratuity = new JTextField(10); //Allows user to enter gratuity
JTextField totalTextField = new JTextField(10); //Displays total
JButton enterOtherGratuity = new JButton("Enter"); //Applies gratuity to total
JButton nextPage = new JButton("Next Page"); //Displays next page when entered
JCheckBox zeroPercent = new JCheckBox("0%", false); //0% gratuity
JCheckBox fifteenPercent = new JCheckBox("15%", false); //15% gratuity
JCheckBox twentyPercent = new JCheckBox("20%", false); //20% gratuity
ButtonGroup group = new ButtonGroup();
double subtotalVariable; //Variable used for the subtotal
double taxVariable; //Variable used for tax
double finalTotal; //Variable for the total bill
double gratuityVariable; //Variable for gratuity
Font otherFont = new Font("Times New Roman", Font.BOLD, 15);
public TaxAndGratuityFrame() {
setLayout(new GridLayout(0,7));
setSize(470,375);
//Adding check boxes to button group so that only one can be selected
group.add(zeroPercent);
group.add(fifteenPercent);
group.add(twentyPercent);
//Setting subtotal, tax, and total text fields to disabled so that user's cannot change value
subtotalTextField.setEditable(false);
taxTextField.setEditable(false);
totalTextField.setEditable(false);
//Initializing subtotal variable and tax variable
subtotalVariable = MenuFrame.totalPrice; //Using totalPrice variable from MenuFrame
taxVariable = subtotalVariable * TAX_RATE;
//Displaying subtotal and tax in their respective JLabels
subtotalTextField.setText("$" + subtotalVariable);
taxTextField.setText("$" + taxVariable);
//Adding action listener to 'other gratuity' button and 'next page' button and checkboxes
enterOtherGratuity.addActionListener(this);
nextPage.addActionListener(this);
zeroPercent.addActionListener(this);
fifteenPercent.addActionListener(this);
twentyPercent.addActionListener(this);
//Setting fonts for labels
subtotal.setFont(otherFont);
tax.setFont(otherFont);
gratuity.setFont(otherFont);
total.setFont(otherFont);
//Adding components along with new JLabels so that components are in the correct position
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(subtotal);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(subtotalTextField);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(tax);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(taxTextField);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(gratuity);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(zeroPercent);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(fifteenPercent);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(twentyPercent);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(other);
add(otherGratuity);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(enterOtherGratuity);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(total);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(totalTextField);
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
add(new JLabel(""));
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
//if user clicked enter button
if(source == enterOtherGratuity) {
//Uncheck the check boxes if any are selected
group.clearSelection();
//Store what user entered into 'gratuityVariable', and add this variable, subtotalvariable, and tax variable
//to get final
gratuityVariable = Double.parseDouble(otherGratuity.getText());
finalTotal = gratuityVariable + subtotalVariable + taxVariable;
totalTextField.setText("$" + finalTotal);
}
if(source == zeroPercent) {
//If there is anything typed in the other gratuity text field, clear it
otherGratuity.setText("");
//If not gratuity, just add subtotal and tax
finalTotal = subtotalVariable + taxVariable;
totalTextField.setText("$" + finalTotal);
}
if(source == fifteenPercent) {
//If there is anything typed in the other gratuity text field, clear it
otherGratuity.setText("");
//Calculate fifteen percent gratuity and add that to subtotal and tax to get final
gratuityVariable = ((subtotalVariable + taxVariable) * 0.15);
finalTotal = gratuityVariable + subtotalVariable + taxVariable;
totalTextField.setText("$" + finalTotal);
}
if(source == twentyPercent) {
//If there is anything typed in the other gratuity text field, clear it
otherGratuity.setText("");
//Calculate twenty percent gratuity and add that to subtotal and tax to get final
gratuityVariable = ((subtotalVariable + taxVariable) * 0.2);
finalTotal = gratuityVariable + subtotalVariable + taxVariable;
totalTextField.setText("$" + finalTotal);
}
//Makes changes appear
repaint();
revalidate();
}
}