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.*;
/**
*
* @author Alexander
*/
public class Expenses extends JPanel
{
private JPanel panel;
private JLabel numberOfDays;
private JLabel amountAirfare;
private JLabel amontRentalFee;
private JLabel numberMiles;
private JLabel parkingFee;
private JLabel taxiFee;
private JLabel seminarFee;
private JLabel lodgingFee;
private JLabel totalExpenses;
private JLabel totalAllowed;
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);
seminarFee = new JLabel("Conference or seminar registration fees: ");
smeinarFeeField = new JTextField(10);
lodgingFee = new JLabel("Lodging charges, per night");
lodgingFeeField = new JTextField(10);
totalAllowed = 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(seminarFee);
panel.add(smeinarFeeField);
panel.add(lodgingFee);
panel.add(lodgingFeeField);
panel.add(calculateButton);
panel.add(totalExpenses);
panel.add(totalAllowed);
panel.add(excessMustPaid);
panel.add(amountSaved);
}
public static void main(String[] args)
{
new Expenses();
}
private class CalcButtonListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String stringDay,stringAirfare,stringCarRental,stringMilesDriven,stringParkingFee,stringTaxiCharges,stringSeminarFee,stringLodgingFee;
double totalAllowed,numberDays,AirFare,CarRental,MilesDriven,ParkingFee,TaxiCharges,seminarFee,lodgingFee;
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;
}
}
}
Loading
VulpesPosted Mar 29, 2012, 3:16 PM
If you're wanting me to look at the other project, it's not all there - there's no main() method for starters.
VulpesPosted Mar 29, 2012, 7:02 PM
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/GridLayout.html
If you only want two columns, you therefore have to make sure that the panel is just big enough to fit two across the page.
For your second project, these settings seemed to work OK:
Prime bPosted Mar 29, 2012, 3:33 PM
Prime bPosted Mar 29, 2012, 3:30 PM
Like this
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Alexander
*/
public class Revenue {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TheaterForm();
}
}
Prime bPosted Mar 29, 2012, 3:14 PM
Prime bPosted Mar 29, 2012, 3:12 PM
Prime bPosted Mar 29, 2012, 3:07 PM
This is my other homework , that is actually wokring!!!!!!!!!! I have been comparing them for last hour now and I just don't see any differences but yet it keeps throwing me an error.
So here this the homework I did and it works perfect, and thank you for that Grid Layout package!
VulpesPosted Mar 29, 2012, 2:42 PM
By default, the JPanel class uses a FlowLayout which explains the current untidy mess :)
A GridLayout looks suitable here with 2 columns : one for the label and one for the text field.
I couldn't get the application to compile as it stood as there are 3 controls which haven't yet been initialized and are therefore still null. I've commented those out for now which leaves 18 controls - 9 rows of 2 each within the grid:
Prime bPosted Mar 29, 2012, 2:02 PM
So here is the updated version, but I still get the same problem