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

标签 java swing jframe jbutton actionlistener

我为每个汽车购买选项设置了值,但当我单击按钮时,我一直得到 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/

相关文章:

java - JFrame 布局问题,面板应该在顶部但在中间

java - Gradle:应用程序看不到从子模块的第二级导入的代码

java - Java6 中 java.nio.file.Path 的替代方案,以实现操作系统文件系统灵活性

java - 根据合并标准有效地合并列表

java - 无法识别 CardLayout 中当前可见的卡片

java - 无法将字符串附加到 jtextarea

java - Android:显示 24 小时后收到消息后的天数

java - 多行 JLabels - Java

java - JFrame更新问题

java - JFileChooser 显示在全屏 JFrame 之外