Prime b

Prime b

  • NA
  • 810
  • 346k

No one looks at java section so I post my JAVA GUI PROBLEM HERE!

Mar 29 2012 2:29 PM
This is my GUI, it looks like we have to learn how to hard code the gui............................................
So i did hard code it, but now the problem is labels, text fields are kind of in random location.
If you copy and paste the code it will work, my problem is how to i make it look better?



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.*;





import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;




/**
 *
 * @author Alexander
 */
public class Expenses extends JPanel
{
    String stringDay,stringAirfare,stringCarRental,stringMilesDriven,stringParkingFee,stringTaxiCharges,stringSeminarFee,stringLodgingFee;
    double totalAllowed,numberDays,AirFare,CarRental,MilesDriven,ParkingFee,TaxiCharges,seminarFee,lodgingFee;
    private JPanel panel;
    private JLabel numberOfDays;
    private JLabel amountAirfare;
    private JLabel amontRentalFee;
    private JLabel numberMiles;
    private JLabel parkingFee;
    private JLabel taxiFee;
    private JLabel seminarFeeLabel;
    private JLabel lodgingFeeLabel;
    private JLabel totalExpenses;
    private JLabel totalAllowedLabel;
    private JLabel excessMustPaid;
    private JLabel amountSaved;
   
    private JTextField daysField;
    private JTextField amountAirfareField;
    private JTextField rentalFeeField;
    private JTextField milesDrivenField;
    private JTextField parkingFeeField;
    private JTextField taxiFeeField;
    private JTextField smeinarFeeField;
    private JTextField lodgingFeeField;
   
    private JButton calculateButton;
   
    private final int WIDTH = 500;
    private final int HEIGHT = 500;
   
    public Expenses()
    {
        JFrame frame = new JFrame("Travel Expenses");
        frame.setSize(WIDTH,HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        frame.add(panel);
        frame.setVisible(true);
       
    }
    private void buildPanel()
    {
       
     numberOfDays = new JLabel("Number of days on the trip: ");
     daysField = new JTextField(10);
     
     amountAirfare = new JLabel("Amount of airfare: ");
     amountAirfareField = new JTextField(10);
     
     amontRentalFee = new JLabel("Amount of car rental fee: ");
     rentalFeeField = new JTextField(10);
     
     numberMiles = new JLabel("Number of miles driven: ");
     milesDrivenField = new JTextField(10);
     
     parkingFee = new JLabel("Amount of parking fees: ");
     parkingFeeField = new JTextField(10);
     
     taxiFee = new JLabel("Taxi charges: ");
     taxiFeeField = new JTextField(10);
     
     seminarFeeLabel = new JLabel("Conference or seminar registration fees: ");
     smeinarFeeField = new JTextField(10);
     
     lodgingFeeLabel = new JLabel("Lodging charges, per night");
     lodgingFeeField = new JTextField(10);
     
     totalAllowedLabel = new JLabel("Total allowed to spend is "+ totalAllowed);
     calculateButton = new JButton("Calculate");
     
     panel = new JPanel();
     
     panel.add(numberOfDays);
     panel.add(daysField);
     panel.add(amountAirfare);
     panel.add(amountAirfareField);
     panel.add(amontRentalFee);
     panel.add(rentalFeeField);
     panel.add(numberMiles);
     panel.add(milesDrivenField);
     panel.add(parkingFee);
     panel.add(parkingFeeField);
     panel.add(taxiFee);
     panel.add(taxiFeeField);
     panel.add(seminarFeeLabel);
     panel.add(smeinarFeeField);
     panel.add(lodgingFeeLabel);
     panel.add(lodgingFeeField);
     panel.add(calculateButton);
     panel.add(totalExpenses);
     panel.add(totalAllowedLabel);
     panel.add(excessMustPaid);
     panel.add(amountSaved);
    }
   
    public static void main(String[] args)
    {
        new Expenses();
    }
     public void actionPerformed(ActionEvent e)
     {


         final double dayMeal = 37.00;
         final double parkingFee = 10.00;
         final double taxiFee = 20.00;
         final double lodgingCharge = 95.00;
         final double rentCar = 0.27;
         double totalMilesDriven;
         
         stringDay = daysField.getText();
         numberDays = Double.parseDouble(stringDay);
         
         stringAirfare = amountAirfareField.getText();
         AirFare = Double.parseDouble(stringAirfare);
         
         stringCarRental = rentalFeeField.getText();
         CarRental = Double.parseDouble(stringCarRental);
         
         stringMilesDriven = milesDrivenField.getText();
         MilesDriven = Double.parseDouble(stringMilesDriven);
         
         stringParkingFee = parkingFeeField.getText();
         ParkingFee = Double.parseDouble(stringParkingFee);
         
         stringTaxiCharges = taxiFeeField.getText();
         TaxiCharges = Double.parseDouble(stringTaxiCharges);
         
         stringSeminarFee = smeinarFeeField.getText();
         seminarFee = Double.parseDouble(stringSeminarFee);
         
         stringLodgingFee = lodgingFeeField.getText();
         lodgingFee = Double.parseDouble(stringLodgingFee);
         
         totalMilesDriven = MilesDriven * rentCar;
         totalAllowed = numberDays * dayMeal * parkingFee * taxiFee * lodgingCharge * rentCar * totalMilesDriven;
         
     }

Answers (2)