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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GUI Application problem! HELP Please!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
    add(panel);

    // 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);
    panel.add(messageLabel);
    panel.add( days );
    panel.add( airfare);
    panel.add( fees );
    panel.add( miles );
    panel.add( parkfees );
    panel.add( taxi );
    panel.add( regfees );
    panel.add( lodg );
    }
    }


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default 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. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: GUI Application problem! HELP Please!

    Quote Originally Posted by gpelefty90 View Post
    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...
    		panel.add(txt);
    		panel.add(label);
    		/*
    		 * 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.
    Last edited by bgroenks96; October 4th, 2011 at 09:25 PM. Reason: Added highlights

Similar Threads

  1. Problem while calling mPlayer from Java application
    By bhavani418 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 28th, 2012, 09:49 PM
  2. Chat application Sign in Problem (maybe : Exception)
    By aymane-tech in forum Java Networking
    Replies: 1
    Last Post: March 23rd, 2011, 11:01 AM
  3. Chat application problem
    By fembiz in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2009, 04:21 AM
  4. Application Continued (problem)
    By Riston in forum AWT / Java Swing
    Replies: 6
    Last Post: November 26th, 2009, 02:33 PM
  5. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 03:15 AM

Tags for this Thread