TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Prime b
NA
810
346.9k
GUI problem
Mar 28 2012 4:56 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.*;
/**
*
* @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;
}
}
}
Reply
Answers (
9
)
How can be perform validation with the help of Filter in java ?
Can we put a private constructor in my public class if yes then give the example ?