Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## GUI Application problem! HELP Please!

Hi, I have alot of confusion with a program I am supposed to write. The GUI App is supposed to take information from the user, that information is: Number of days on trip, amount of airfare, amount of car rental fees, amount of miles driven if only driven in private car, amount of parking fees, amount of taxi charges, conference or seminar registration fees, and lodging charges.

Now the companies reimbursement policy is as follows:
37\$ per day for meals
up to 10\$ dollars a day for parking fees
up to 20 \$ a day for taxi charges
up to 95\$ dollars a day for lodging
and .27\$ per mile driven.

Now here is what I am supposed to calculate:

Total expenses by the business person
total allowable expenses for the trip(what the company will reimburse for trip depending on how many days it was)
the excess amount that must be paid by business person if there is any
the amount saved by business person if the expenses are under the total amount allowed

The first problems I am having is getting a label on the JTextField components so the user knows where to enter each specified information.

The Second problem I have is actually executing all of the calculations i need to do. My teacher has not gone over any of this in the form of performing the calculations, just the basics on how to set up a basic GUI APP.

Here is what i have so far:

package Travel_Expenses;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Expenses extends JFrame {
private JTextField days;
private JLabel messageLabel;
private JTextField airfare;
private JTextField fees;
private JTextField miles;
private JTextField parkfees;
private JTextField taxi;
private JTextField regfees;
private JTextField lodg;
private JPanel panel;

public static void main(String[] args) {
JFrame frame = new Expenses();
}
public Expenses(){

// call set methods to customize frame
setTitle( "Expenses");
setSize( 400, 200);
setLocation( 500, 300 );
setDefaultCloseOperation(EXIT_ON_CLOSE);

// call buildPanel() method and add panel to frame
buildPanel();

// display the frame
setVisible( true );

}
private void buildPanel() {

// Instantiate panel and GUI Components
panel = new JPanel();
messageLabel=new JLabel("How Many days was the trip");
days = new JTextField(10);
airfare = new JTextField(10);
fees= new JTextField(10);
miles = new JTextField(10);
parkfees = new JTextField(10);
taxi = new JTextField(10);
regfees = new JTextField(10);
lodg = new JTextField(10);
}
}

2. ## Re: GUI Application problem! HELP Please!

The first problems I am having is getting a label on the JTextField components so the user knows where to enter each specified information
You don't place labels on those components, however you can create a JLabel component and place it adjacent to a JTextField. See the following for JLabel
How to Use Labels (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
and the following for how to align:
A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

To do the calculations, one typically has a button that contains a Listener - when the button is clicked the listener is fired. See the following:
How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

Might seem like a lot, but break the problem down to one step at a time. And for future reference, please wrap your code in the code tags.

3. ## Re: GUI Application problem! HELP Please!

Thank you for your reply. I am still confused on how to align the JLabel to a JTextField. I was wondering if you could show me example. It seems i have to put all of these fields in a Layout to create and organize the GUI application.

Thanks

4. ## Re: GUI Application problem! HELP Please!

Originally Posted by gpelefty90
Thank you for your reply. I am still confused on how to align the JLabel to a JTextField. I was wondering if you could show me example. It seems i have to put all of these fields in a Layout to create and organize the GUI application.

Thanks
Try this example code. It does compile and performs correctly when run:

```[CODE]
import javax.swing.*;
import java.awt.*;

public class LabelDemo {

public static void main(String[] args) {
new LabelDemo().start();
}

public void start() {
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Here is one example using a JPanel to place the label directly to the right.
JPanel panel = new JPanel();
//Let's allign the components to the left side of the panel by telling its Layout Manager to do so.
FlowLayout layout = (FlowLayout) panel.getLayout();
layout.setAlignment(FlowLayout.LEFT);
panel.setLayout(layout);
JTextField txt = new JTextField(20);
JLabel label = new JLabel("Hey!  Look at me!");
//Just as a note, you can also try just pre-setting the text of a JTextField to whatever you wanted to label it.
//Then the user can just delete the pre-set text and enter the correct value.
txt.setText("Preset text!");
//Now it's time to add the stuff...
/*
* The FlowLayout alligns things from left to right (can be configured) and is the nicest to you usually about
* giving you the preferred size you want.  You cannot, however, choose a specific place for the component to go.
* They are simply alligned left to right from the top of the JPanel to the bottom.
*/
frame.setContentPane(panel);
frame.setSize(400,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
[/CODE]```

Keep in mind there are other ways to do this. Lets say you wanted the JTextField on the far left and the JLabel on the far right. Then you could make the JPanel have a BorderLayout and assign them positions at BorderLayout.EAST and BorderLayout.WEST.

Let me know if you have questions.