Welcome to Dream.In.Code
Become a Java Expert!

Join 149,534 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




SO SO FRUSTRATED w/ my simple GUI application!

 
Reply to this topicStart new topic

SO SO FRUSTRATED w/ my simple GUI application!, Please advise on code.

jreibe3000
3 Jul, 2007 - 10:31 AM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
Hello All,

I am working on some code that is supposed to produce a very small GUI that is a menu for a Restaurant using JLabels, JTextFields, and a JButton.
Here is how it's supposed to work:
1.User enters how many of each type of sandwich they want(there are only 2 kinds)in two seperate text fields.
2.User then clicks "Calculate Bill" button to find their total, which is found by adding the costs for the sandwiches, plus a 70cent toppings charge for each sandwich, plus 15% gratuity added in.

I am so confused and frustrated because I thought I understood how GUI's worked, but now I am kind of lost. I would love any help with my code, as I know it is pretty rough.

Thank you so much,
Jake

CODE

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

public class Restaurant extends JFrame
{
   private Container contents;
   private JLabel name, courseNum, welcome, prompt, chickP, fishP;
   private JLabel chargeSand, chargeService, totalBill;
   private JTextField numChick, numFish;
   private JButton compute;

   public Restaurant()
   {
      super("Barker's Menu");
      contents=getContentPane();
      contents.setLayout(newFlowLayout());
      
      myName=new JLabel("Programmer: Jake Reibert");
      courseNum=new JLabel("CSI 161/875");

      welcome=new JLabel("Welcome To Barker's Sandwich Shop");
      prompt=new JLabel("Enter # of Sandwiches for each; 0 if none");
      chickP=new JLabel("Chicken Sandwiches @ $4.59 each");
      chickP.setForeground(Color.BLUE);
      numChick=new JTextField(3);

      fishP=new JLabel("Salmon Sandwiches @ $4.99 each");
      fishP.setForeground(Color.BLUE);
      numFish=new JTextField(3);

      chargeSand=new JLabel("Charge for Sandwiches = $");
      chargeService= new JLabel("Charge for Service = $");
      totalBill= new JLabel("Total Bill = $");

      costSand=new JLabel("???");
      costService=new JLabel("???");
      costTotal=new JLabel("???");
      
      compute=new JButton("Calculate Bill");

      contents.add(myName);
      contents.add(courseNum);
      contents.add(welcome);  
      contents.add(prompt);
      contents.add(chickP);
      contents.add(numChick);
      contents.add(fishP);
      contents.add(numFish);
      contents.add(chargeSand);
      contents.add(chargeService);
      contents.add(totalBill);
      contents.add(costSand);
      contents.add(costService);
      contents.add(costTotal);
      contents.add(compute);

      ButtonHandler bh= new ButtonHandler();
      compute.addActionListener(bh);

      setSize(400,400);
      setVisible(true);
   }

   private class ButtonHandler implements ActionListener
   {
      try
      {
         double one= Double.parseDouble(numChick.getText());
         double two= Double.parseDouble(numFish.getText());

         double orderAmount= (one*4.59)+(two*4.99);
         double toppingsPrice= (one+two)*.70;
         double serviceAmount= (orderAmount+toppingsPrice)*.15;
         double totalAmount=(orderAmount+toppingsPrice+serviceAmount);
      }
  
      catch(NumberFormatException e)
      {
         numChick.setText("Enter a Number");
         numFish.setText("");
         costTotal.setText("???");
      }
   }


   public static void main(String{}args)
   {
      Restaurant rest= new Restaurant();
      rest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}



User is offlineProfile CardPM
+Quote Post

Zaal the Invincible
RE: SO SO FRUSTRATED W/ My Simple GUI Application!
3 Jul, 2007 - 10:56 AM
Post #2

New D.I.C Head
*

Joined: 1 Jul, 2007
Posts: 1


My Contributions
hi

I'm not sure what your specific problem is but I'll take a stab in the dark...your program compiles but your GUI doesn't show.

If so, you need to add the Frame.show() function because nothing will appear unless you do that. Insert this function as the last line of main()

If you have some other problem or if this doesn't solve your problem, I'm willing to give it another look.

Good luck,
Zaal
User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: SO SO FRUSTRATED W/ My Simple GUI Application!
3 Jul, 2007 - 11:01 AM
Post #3

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
QUOTE(Zaal the Invincible @ 3 Jul, 2007 - 11:56 AM) *

hi

I'm not sure what your specific problem is but I'll take a stab in the dark...your program compiles but your GUI doesn't show.

If so, you need to add the Frame.show() function because nothing will appear unless you do that. Insert this function as the last line of main()

If you have some other problem or if this doesn't solve your problem, I'm willing to give it another look.

Good luck,
Zaal


The assignment says that we are not supposed to use that function,
but instead i have //setVisible(true);
which is supposed to make the program visible.

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: SO SO FRUSTRATED W/ My Simple GUI Application!
3 Jul, 2007 - 11:31 AM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
setVisible(true) is a much better option, and glad to hear it is being encouraged.

I have to head to my crappy job... should have called in sick, lol. If your problem is not solved in the next 8hrs i'll take a look at it in detail then.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: SO SO FRUSTRATED W/ My Simple GUI Application!
3 Jul, 2007 - 01:21 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Wow, where to start because you have quite a few things wrong here. Lets start with the simple.

1. In your main at the bottom you have curly braces rather than square brackets after "String" remember it is a string array.

2. Second you have a bunch of variables defined and not used and using variables that are not defined. My guess is that you went with one set of names and changed them mid way through development. One such example is "myName" and "name". Another is "costTotal" and "totalBill".

3. Another is that you aren't setting your Jlabels in your "try" part of your try catch so you are not going to see output.

4. You needed a space between the words "new" and "FlowLayout"

Now once you fix these you have to put in some code for your actionListener. Remember the actionListener is going to need to listen for a specific event from the button, in this case an "actionPerformed". This is defined as an anonymous function or in this case an inline class.

Lastly to print out the results to your Jlabels you are going to have to wrap your primitive doubles into a Double class and using toString() to print it to the Jlabels.

Below is a fully functioning and tested solution correcting all the problems above.

CODE


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

public class Restaurant extends JFrame
{
   private Container contents;
   private JLabel name, courseNum, welcome, prompt, chickP, fishP;
   private JLabel chargeSand, chargeService, totalBill;
   private JTextField numChick, numFish;
   private JButton compute;

   public Restaurant()
   {
      super("Barker's Menu");
      contents = getContentPane();
      contents.setLayout(new FlowLayout());

      name=new JLabel("Programmer: Jake Reibert");
      courseNum=new JLabel("CSI 161/875");

      welcome=new JLabel("Welcome To Barker's Sandwich Shop");
      prompt=new JLabel("Enter # of Sandwiches for each; 0 if none");
      chickP=new JLabel("Chicken Sandwiches @ $4.59 each");
      chickP.setForeground(Color.BLUE);
      numChick=new JTextField(3);

      fishP=new JLabel("Salmon Sandwiches @ $4.99 each");
      fishP.setForeground(Color.BLUE);
      numFish=new JTextField(3);

      chargeSand = new JLabel("Charge for Sandwiches = $");
      chargeService = new JLabel("Charge for Service = $");
      totalBill= new JLabel("Total Bill = $");

      chargeSand = new JLabel("???");
      chargeService = new JLabel("???");
      totalBill = new JLabel("???");

      compute = new JButton("Calculate Bill");

      contents.add(name);
      contents.add(courseNum);
      contents.add(welcome);
      contents.add(prompt);
      contents.add(chickP);
      contents.add(numChick);
      contents.add(fishP);
      contents.add(numFish);
      contents.add(chargeSand);
      contents.add(chargeService);
      contents.add(totalBill);
      contents.add(chargeSand);
      contents.add(chargeService);
      contents.add(totalBill);
      contents.add(compute);

      ButtonHandler bh = new ButtonHandler();
      compute.addActionListener(bh);

      setSize(400,400);
      setVisible(true);
   }

   private class ButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e) {
          try
          {
             double one = Double.parseDouble(numChick.getText());
             double two = Double.parseDouble(numFish.getText());

             // Calculations for determining total price of bill
             double orderAmount = (one*4.59)+(two*4.99);
             double toppingsPrice = (one+two)*.70;
             double serviceAmount = (orderAmount+toppingsPrice)*.15;
             double totalAmount = (orderAmount+toppingsPrice+serviceAmount);

             // Now to display the charges
             chargeSand.setText(new Double(orderAmount).toString());
             chargeService.setText(new Double(serviceAmount).toString());
             totalBill.setText(new Double(totalAmount).toString());
          }

          catch(NumberFormatException ex)
          {
             numChick.setText("Enter a Number");
             numFish.setText("");
             totalBill.setText("???");
          }
        }
   }


   public static void main(String[] args)
   {
      Restaurant rest= new Restaurant();
      rest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}


Of course this project still needs further work for error handling and such.

This post has been edited by Martyr2: 3 Jul, 2007 - 02:11 PM
User is offlineProfile CardPM
+Quote Post

jreibe3000
RE: SO SO FRUSTRATED W/ My Simple GUI Application!
3 Jul, 2007 - 07:43 PM
Post #6

D.I.C Head
**

Joined: 8 Jun, 2007
Posts: 65


My Contributions
Thank you for pointing those mistakes out to me, I can see what I did wrong pretty clearly with most parts of it. Although, I am confused about the output the program is generating, and how to change the output to proper format. I know the decimal Format class can be used to round the numbers, but why are three values being given when you click the "Calculate Bill" button, instead of just one final value?

Many thanks,
Jake
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: SO SO FRUSTRATED W/ My Simple GUI Application!
3 Jul, 2007 - 08:12 PM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You tell me, you are the one that added three JLabels with ??? in them and titled them chargeSand, chargeService and totalBill. I just put the sandwich charge, service charge and the bill total in them for you. The last one is the total if you just want that.

Hey I only modified what you gave me.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month