我为每个汽车购买选项设置了值,但是当我点击按钮时,我一直得到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 :(得分:1)
你得0,因为没有计算价格"点击按钮"。
您正在尝试在创建按钮时计算价格。问题是用户还没有做任何事情。价格只能根据事件计算。
因此,您需要将所有价格计算代码移至ActionListener
答案 1 :(得分:1)
您只根据空控件进行了一次计算。这是一个修复:
//All the Buttons needed
private JComboBox<String> 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");
recalculate();
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){
recalculate();
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;
}
private void recalculate() {
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;
}
}