单击提交后填充jframe

时间:2013-06-12 02:31:54

标签: java swing button jpanel submit

我正在写一个家庭作业的程序,除了一个项目(除了屏幕的实际“视图”显示不佳)之外,它已经完成大部分工作。我正在使用文本字段和组合框基于用户输入进行一些计算。我已将任何文本转换为整数。问题是,当我点击提交按钮时,它应该进行计算,然后显示结果。程序首次打开时,结果显示0.任何帮助将不胜感激。它确实编译没有错误,并将运行。这是我的代码:

import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics.*;
    import java.awt.Font.*;
    import javax.swing.*;
    import javax.swing.text.*;

    public class tripManager extends JFrame implements ActionListener
    {
        //declare variables
            Color teal = new Color(0,128,128);
            Color wheat = new Color(245,222,179);
            Font myFont = new Font("Arial", Font.BOLD,14);
            Font titleFont = new Font("Arial",Font.BOLD, 20);
            private int oGallons;
            private double oPerMileGas;
            private int oRefuel;
            private int oOilChange;
            private double oTotalTripCost;
            private int oVehicle;
            private int oCMileage;
            private int oOMileage;
            private double oGasType;
            private double oGasCost;
            private int oMiles;
            private double oOilCost;



        //construct components
        JPanel titleJPanel = new JPanel();
            JLabel companyJLabel = new JLabel("TRAVELOR'S GASOLINE CALCULATOR");
            JLabel companyAddressJLabel = new JLabel("11333 Main Street, Zephyrhills, FL 33542");
            JLabel descriptionJLabel = new JLabel("Welcome to the Travelor's Gasoline Calculator.  This application");
            JLabel descriptionJLabel2 = new JLabel("will calculate the total cost of your trip, breaking it down by");
            JLabel descriptionJLabel3 = new JLabel("cost per mile and maintenance costs.");

        JPanel inputJPanel = new JPanel();
            JLabel milesTravelJLabel = new JLabel("Enter total number of trip miles: ");
                TextField tripMiles = new TextField(8);
            JLabel currentMileageJLabel = new JLabel("Enter current mileage on your vehicle: ");
                TextField cMileage = new TextField(8);
            JLabel oilChangeMileageJLabel = new JLabel("Enter mileage of last oil change: ");
                TextField oMileage = new TextField(8);

        JPanel buttonPanel = new JPanel();
            Button  clearButton = new Button("Clear");
            Button submitButton = new Button("Submit");
            Button quitButton = new Button("Exit");

        JPanel outputPanel = new JPanel();
            JLabel gallonsUsedLabel = new JLabel("Total Gallons of gas needed for trip: "+ oGallons+" Gallons");
            JLabel costPerMileLabel = new JLabel("Total cost of gas per mile: $"+oPerMileGas);
            JLabel refuelLabel = new JLabel("Total number of times to refuel: "+oRefuel);
            JLabel oilChangesNeedLabel = new JLabel("Total number of oil changes needed: "+oOilChange);
            JLabel totalCostLabel = new JLabel("Total travel cost is: $"+oTotalTripCost);

        //construct drop box
        JPanel dropJPanel = new JPanel();
            JComboBox vehicleCombo = new JComboBox();
            JComboBox beginLocation = new JComboBox();
            JComboBox endLocation = new JComboBox();
            JComboBox gasType = new JComboBox();



        public tripManager()
        {
            //set layouts for frame and panels
            this.setLayout(new BorderLayout());
                titleJPanel.setLayout(new GridLayout(5,1));
                inputJPanel.setLayout(new FlowLayout());
                outputPanel.setLayout(new GridLayout(1,5));
                dropJPanel.setLayout(new GridLayout(1,4));
                buttonPanel.setLayout(new GridLayout(1,3));

            //add components to titleJPanel
            titleJPanel.add(companyJLabel);
            titleJPanel.add(companyAddressJLabel);
            titleJPanel.add(descriptionJLabel);
            titleJPanel.add(descriptionJLabel2);
            titleJPanel.add(descriptionJLabel3);


            //add components to inputJPanel
            inputJPanel.add(milesTravelJLabel);
            inputJPanel.add(tripMiles);
            inputJPanel.add(currentMileageJLabel);
            inputJPanel.add(cMileage);
            inputJPanel.add(oilChangeMileageJLabel);
            inputJPanel.add(oMileage);

            //add components to output Panel
            outputPanel.add(gallonsUsedLabel);
            outputPanel.add(costPerMileLabel);
            outputPanel.add(refuelLabel);
            outputPanel.add(oilChangesNeedLabel);
            outputPanel.add(totalCostLabel);

            //add components to button Panel
            buttonPanel.add(clearButton);
            buttonPanel.add(submitButton);
            buttonPanel.add(quitButton);



            //populate JCombo Boxes
            vehicleCombo.addItem("compact");
            vehicleCombo.addItem("mid");
            vehicleCombo.addItem("luxury");
            vehicleCombo.addItem("SUV");
            vehicleCombo.addActionListener(this);
            vehicleCombo.setToolTipText("Click the drop-down arrow to display list of vehicles");
            beginLocation.addItem("New York City");
            beginLocation.addItem("Seatle");
            beginLocation.addItem("Los Angelos");
            beginLocation.addItem("Chicago");
            beginLocation.addItem("Atlanta");
            beginLocation.addItem("Tampa");
            beginLocation.setToolTipText("Click the drop-down arrow to display list of cities");
            beginLocation.addItem("New York City");
            endLocation.addItem("Seatle");
            endLocation.addItem("Los Angelos");
            endLocation.addItem("Chicago");
            endLocation.addItem("Atlanta");
            endLocation.addItem("Tampa");
            endLocation.setToolTipText("Click the drop-down arrow to display list of cities");
            gasType.addItem("leaded");
            gasType.addItem("unleaded");
            gasType.addItem("super unleaded");
            gasType.addItem("diesel");
            gasType.setToolTipText("Click the drop-down arrow to display list of gas types");

            //add components to dropPanel
            dropJPanel.add(vehicleCombo);
            dropJPanel.add(beginLocation);
            dropJPanel.add(endLocation);
            dropJPanel.add(gasType);

            //add panels to frame
            add(titleJPanel, BorderLayout.NORTH);
            add(inputJPanel, BorderLayout.CENTER);
            add(dropJPanel,BorderLayout.EAST);
            add(outputPanel, BorderLayout.SOUTH);
            add(buttonPanel, BorderLayout.WEST);

            //initiate buttons
            clearButton.addActionListener(this);
            submitButton.addActionListener(this);
            quitButton.addActionListener(this);

            //add action listener to combo boxes
            vehicleCombo.addActionListener(this);
            beginLocation.addActionListener(this);
            endLocation.addActionListener(this);
            gasType.addActionListener(this);


        }
        public static void main(String[] args)
        {
            tripManager f = new tripManager();
            f.setBounds(400,400,600,600);
            f.setTitle("TRAVELOR'S GASOLINE CALCULATOR");
            f.setVisible(true);
        }//end of main

        public void actionPerformed(ActionEvent e)
        {

        if(e.getSource()==clearButton)
        {
            tripMiles.setText("");
            cMileage.setText("");
            oMileage.setText("");
            tripMiles.requestFocus();
        }

        else if(e.getSource()==submitButton)
        {
            String arg = e.getActionCommand();

            if (arg=="submit")
            {
            //vehicle combo selections
            if (arg =="compact")
                oVehicle = 13;
            else if (arg=="mid")
                oVehicle = 18;
            else if (arg=="luxury")
                oVehicle = 15;
            else if (arg == "SUV")
                oVehicle = 23;
        //gas type combo selections
            if (arg =="leaded")
                oGasType = 2.50;
            else if (arg == "unleaded")
                oGasType = 2.90;
            else if (arg == "super unleaded")
                oGasType = 3.00;
            else if (arg == "diesel")
                oGasType = 4.00;
        //get number of miles
        oMiles = Integer.parseInt(tripMiles.getText());
        oGasCost = ((oMiles/oVehicle) * oGasType);

        //calcculate gallons used
        oGallons = oMiles/oVehicle;

        //calculate cost per mile
        oPerMileGas = oGasCost/oMiles;

        //calculate number times to refuel
        oRefuel = oGallons/oVehicle;

        //calculate oil changes needed
        if ((oCMileage - oOMileage)+oMiles >3000)
        {
            oOilChange = (oCMileage-oOMileage+oMiles)/3000;
        }
        else
        {
            oOilChange = 0;
        }
        oOilCost = oOilChange * 30;

        //calculate total trip cost
        oTotalTripCost = oOilCost * oGasCost;

        Component outputPanel = new JPanel();
        JLabel gallonsUsedLabel = new JLabel("Total Gallons of gas needed for trip: " +oGallons+" Gallons");
        JLabel costPerMileLabel = new JLabel("Total cost of gas per mile: $"+oPerMileGas);
        JLabel refuelLabel = new JLabel("Total number of times to refuel: "+oRefuel);
        JLabel oilChangesNeedLabel = new JLabel("Total number of oil changes needed: "+oOilChange);
        JLabel totalCostLabel = new JLabel("Total travel cost is: $"+oTotalTripCost);
    }

1 个答案:

答案 0 :(得分:2)

不要使用==来比较字符串,因为它会检查不是您想要的对象标识。请改用equals(...)equalsIgnoreCase(...)方法。此外,您无需检查任何JComboBox。 arg String与组合框无关,而是按下按钮的文本,虽然重要的是告诉你什么组合框项目没有被选中。检查每个组合框中的选定项目。如果您对这一点感到困惑,请查看组合框教程。