JTextField
在我的代码中,一旦有人单击任一组合框中的选项,我就会尝试在总价格计算中发布。我不想要名称,即“意大利辣香肠”,而是实际的总计算。现在我必须单击文本框并按 Enter 更新数据。
如何JTextField
使用计算更新JComboBox
?
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);
}
}
}