gpt4 book ai didi

java - 使用 JComboBox 的计算更新 JTextField

转载 作者:行者123 更新时间:2023-12-02 07:07:01 25 4
gpt4 key购买 nike

在我的代码中,一旦有人单击任一组合框中的选项,我就会尝试在 JTextField 中发布totalPrice 计算。我不想要名称,即“意大利辣香肠”,而是实际的总计算。现在我必须单击文本框并按 Enter 来更新数据。

如何使用 JComboBox 的计算更新 JTextField

import java.awt.* ;

import javax.swing.* ;
import java.awt.event.* ;

class JPizza extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;

public static void main(String[] args)
{
JPizza myFrame = new JPizza();
myFrame.setVisible(true);
}

private static final int WIDTH = 250;
private static final int HEIGHT = 300;
private static final int X_ORIGIN = 200;
private static final int Y_ORIGIN = 100;

private static final double smallPizzaPrice = 7.00;
private static final double mediumPizzaPrice = 9.00;
private static final double largePizzaPrice = 11.00;
private static final double extraLargePizzaPrice = 14.00;
private static final int smallPizzaSize = 0;
private static final int mediumPizzaSize = 1;
private static final int largePizzaSize = 2;
private static final int extraLargeSize = 3;

private static final double cheese = 0.0, pepperoni = 1.00, peppers = 1.00, sausage = 1.00, olives = 1.00;
private static final int cheesePlace = 0, pepperoniPlace = 1, peppersPlace = 2, sausagePlace = 3, olivesPlace = 4;

int states = 0 ;
double totalPrice = 0.0;
double pizzaSizePrice = smallPizzaPrice ;
double toppingPrice = 0.0;

//Toppings & Panel
JComboBox<String> pizzaTopping;
JPanel comboboxPanel2;

//Pizza Size & Panel
JComboBox<String> pizzaSize;
JPanel comboboxPanel;

//Price Display Field and Panel
JTextField calculate;
JPanel messagePanel;

public JPizza()
{
super("Tower of Pizza");

JLabel title = new JLabel("Tower of Pizza");
add(title);

pizzaSize = new JComboBox<String>();
pizzaSize.addItem( "Small" );
pizzaSize.addItem( "Medium" );
pizzaSize.addItem( "Large" );
pizzaSize.addItem( "Extra Large" );
pizzaSize.setSelectedIndex(0);
pizzaSize.addActionListener(this);

pizzaTopping = new JComboBox<String>();
pizzaTopping.addItem( "Cheese" );
pizzaTopping.addItem( "Pepperoni" );
pizzaTopping.addItem( "Peppers" );
pizzaTopping.addItem( "Sausage" );
pizzaTopping.addItem( "Olives" );
pizzaTopping.setSelectedIndex(0);
pizzaTopping.addActionListener(this);

calculate = new JTextField(10);
calculate.addActionListener(this);

Container contentPane = getContentPane();
GridLayout contentpaneLayout = new GridLayout(5,0,10,10);
contentPane.setLayout(contentpaneLayout);

comboboxPanel = new JPanel();
GridLayout comboboxPanelLayout = new GridLayout(2,0);
comboboxPanel.setLayout(comboboxPanelLayout);
comboboxPanel.add(pizzaSize);
contentPane.add(comboboxPanel);

comboboxPanel2 = new JPanel();
GridLayout comboboxPanelLayout1 = new GridLayout(2,0);
comboboxPanel2.setLayout(comboboxPanelLayout1);
comboboxPanel2.add(pizzaTopping);
contentPane.add(comboboxPanel2);

messagePanel = new JPanel();
GridLayout messagePanelLayout = new GridLayout(2,0);
messagePanel.setLayout(messagePanelLayout);
messagePanel.add(calculate);
contentPane.add(messagePanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT);
setResizable(false);
}


public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();

int pizzaSiz = (int)pizzaSize.getSelectedIndex();

if (source == calculate){

if (pizzaSiz == smallPizzaSize){
pizzaSizePrice = smallPizzaPrice;
}
else if (pizzaSiz == mediumPizzaSize){
pizzaSizePrice = mediumPizzaPrice ;

}
else if (pizzaSiz == largePizzaSize){
pizzaSizePrice = largePizzaPrice;
}
else if (pizzaSiz == extraLargeSize){
pizzaSizePrice = extraLargePizzaPrice;
}

int pizzaTop = (int)pizzaTopping.getSelectedIndex();

if(pizzaTop == cheesePlace ){
toppingPrice = cheese;
}
else if(pizzaTop == pepperoniPlace){
toppingPrice = pepperoni;
}
else if(pizzaTop == peppersPlace){
toppingPrice = peppers;
}
else if(pizzaTop == sausagePlace){
toppingPrice = sausage;
}
else if(pizzaTop == olivesPlace){
toppingPrice = olives;
}

totalPrice = pizzaSizePrice;
totalPrice +=toppingPrice;
calculate.setText("Total Price: $" + totalPrice);
}

}
}

最佳答案

您通过限制 actionPerformed 使用 if 测试执行的操作来故意限制您的代码:

if (source == calculate){

去掉它,该方法将在每次调用 actionPerformed 时进行计算。在尝试计算之前,您最好确保两个组合框都有有效的选择。您可以通过获取组合框的选定索引并测试它们是否 >= 0(不是 -1)来测试它们。

关于java - 使用 JComboBox 的计算更新 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15983231/

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