gpt4 book ai didi

java - 买车程序-需要用JButton计算最终价格

转载 作者:行者123 更新时间:2023-11-30 03:33:10 24 4
gpt4 key购买 nike

我为每个汽车购买选项设置了值,但当我单击按钮时,我一直得到 0。在声明后,我无法让 fP 更新。这是我的代码:

public class CarOptions extends JFrame {

//All the Buttons needed
private JComboBox colorOptions;
private JRadioButton leatherInt;
private JRadioButton touchScreen;
private JRadioButton premiumSound;
private JRadioButton bodyKit;

//Car prices
int cP = 1000;
int eng1 = 6000;
int eng2 = 8000;
int eng3 = 12000;
int acc1 = 600;
int acc2 = 800;
int acc3 = 3000;
int acc4 = 1200;
int fP;

public CarOptions(){
createControlPanel();
}
public void createControlPanel(){
//Panel for all the options
JPanel colorOptions1 = createComboBox();
JPanel sportsOptions = createCheckBoxes();
JPanel accOptions = createRadioButtons();
JPanel finalPrice1 = createButton();

//Line up the panels
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(4,1));
controlPanel.add(colorOptions1);
controlPanel.add(sportsOptions);
controlPanel.add(accOptions);
controlPanel.add(finalPrice1);

add(controlPanel, BorderLayout.SOUTH);
}
public JPanel createComboBox(){

colorOptions = new JComboBox();
colorOptions.addItem("Black");
colorOptions.addItem("Yellow");
colorOptions.addItem("Blue");
colorOptions.addItem("Red");

JPanel panel = new JPanel();
panel.add(colorOptions);
return panel;
}
public JPanel createCheckBoxes(){
ButtonGroup group = new ButtonGroup();
AbstractButton hybrid = new JCheckBox("Hybrid");
AbstractButton base = new JCheckBox("Base Model");
AbstractButton sport = new JCheckBox("Sport");
group.add(hybrid);
group.add(base);
group.add(sport);

if (hybrid.isSelected()){
fP = fP + eng1;
}
else if (base.isSelected()){
fP = fP + eng2;
}
else if (sport.isSelected()){
fP = fP + eng3;
}

JPanel panel = new JPanel();
panel.add(hybrid);
panel.add(base);
panel.add(sport);
panel.setBorder(new TitledBorder(new EtchedBorder(), "Engine Package"));
return panel;
}
public JPanel createRadioButtons(){
leatherInt = new JRadioButton("Leather Interior");
touchScreen = new JRadioButton("Touchscreen Radio");
premiumSound = new JRadioButton("Premium Sound System");
bodyKit = new JRadioButton("Body Kit");

if (leatherInt.isSelected()){
fP = cP + acc1;
}
else if (touchScreen.isSelected()){
fP = cP + acc2;
}
else if (premiumSound.isSelected()){
fP = cP + acc3;
}
else if (bodyKit.isSelected()){
fP = cP + acc4;
}

JPanel panel = new JPanel();
panel.add(leatherInt);
panel.add(touchScreen);
panel.add(premiumSound);
panel.add(bodyKit);
panel.setBorder(new TitledBorder(new EtchedBorder(), "Accesories"));
return panel;
}
public JPanel createButton(){
JButton finalPrice = new JButton("Final Price");
finalPrice.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

JFrame fPFrame = new JFrame();
fPFrame.setSize(200,100);
fPFrame.setTitle("Final Price");
fPFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
fPFrame.setVisible(true);

JLabel fPLabel = new JLabel("Your final price is: $" + fP);
JPanel fPPanel = new JPanel();
fPPanel.add(fPLabel);
fPFrame.add(fPPanel);
}
});

JPanel panel = new JPanel();
panel.add(finalPrice);
return panel;
}
}

最佳答案

您得到 0,因为“单击按钮时”不计算价格。

您在创建按钮时尝试计算价格。问题是用户还没有做任何事情。价格只能根据事件来计算。

因此您所有的价格计算代码都需要移至 ActionListener

关于java - 买车程序-需要用JButton计算最终价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28544668/

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