Hello to everyone. I'm Alphonse, but you can call me Phonse for short. We're taking Java for computer class this semester. Anyway, I'll just cut short on the story. Our teacher has been teaching us for a month and she gave us a project and it's to make a fastfood menu like the ones we see on the store's cashiers. She didn't teach us GUI. She just told us to self-study it and now I'm having some problems. BTW, I chose to make a menu of Burger King.
I've been going through the posts, researching on the problem for a few hours and it's either I couldn't find them or they're incomprehensible to me because I'm a total n00b. I know my problems are probably way too simple for you guys, but please bear with me. Also, please go easy on the advanced jargon. I'm using JCreator BTW.
Anyway, here are my problems:
1) I was trying to make a variable "global" or available for use throughout the code but it always becomes "stuck" inside one of my listener codes. Now my 'total' variable can't be updated. Please check the 1st and 2nd "Trial code for the calculations in the menu" which I commented in my code. We weren't taught yet the use of public and private.
2) Before adding the codes for the calculation, I tried making a sample code for updating the JTextArea. Apparently, it doesn't work. When I click the button I assigned for clearing or adding text to the JTextArea, it doesn't change. I didn't get any error when compiling and executing the code but the action I assigned to the button isn't working.
3) This seems really stupid, but... do I still have to add code to the 'public static void main' part because I only made it so the application can start.
4) Another n00b question. Why doesn't my tooltip work when I compile and run the code? When I first started with the GUI, the tooltip works. But after adding the rest of the code, it stopped working. I tried commenting the other parts of the code that have errors so that I can compile and run it, but the tooltips still don't work. I point my mouse over to the button but the tooltip doesn't appear anymore.
Meh... My post feels a lot less intellectual and n00bish than the other posts, but I hope you guys can still help me. I just wanted to finish this project now so I can attend to my other subjects come finals week. I hate cramming.
- Phonse
Anyway, here's my code:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Trial1 extends JPanel {
JButton wphrjrml;
JButton twolwphrml;
JButton wphrml;
JTextArea jtextarea1;
JButton num1;
JButton num2;
JButton num3;
JButton num4;
JButton num5;
JButton num6;
JButton num7;
JButton num8;
JButton num9;
JButton num0;
JButton bkspc;
JButton enter;
JButton threelwphrml;
JButton mswphrml;
JButton wswphrml;
JButton chxswfrml;
JButton chxswclml;
JButton onepcchxml;
JButton twopcchxml;
JButton oringslrg;
JButton oringssml;
JButton frfrieslrg;
JButton frfriessml;
JButton bkchxspghti;
JButton bkspghti;
JButton bksundae;
JButton bkcone;
JButton bpwphrml;
JButton bkjoe;
JButton hersheypie;
JButton softdrinklrg;
JButton softdrinksml;
JButton upgradefrdr;
JButton upgradefr;
JButton upgradedr;
JButton scdiscount;
JButton clear;
JButton total;
public Trial1() {
setPreferredSize (new Dimension (830, 565));
setLayout (null);
add (wphrjrml);
add (twolwphrml);
add (wphrml);
add (jtextarea1);
add (num1);
add (num2);
add (num3);
add (num4);
add (num5);
add (num6);
add (num7);
add (num8);
add (num9);
add (num0);
add (bkspc);
add (enter);
add (threelwphrml);
add (mswphrml);
add (wswphrml);
add (chxswfrml);
add (chxswclml);
add (onepcchxml);
add (twopcchxml);
add (oringslrg);
add (oringssml);
add (frfrieslrg);
add (frfriessml);
add (bkchxspghti);
add (bkspghti);
add (bksundae);
add (bkcone);
add (bpwphrml);
add (bkjoe);
add (hersheypie);
add (softdrinklrg);
add (softdrinksml);
add (upgradefrdr);
add (upgradefr);
add (upgradedr);
add (scdiscount);
add (total);
add (clear);
wphrjrml.setBounds (290, 5, 130, 55);
twolwphrml.setBounds (560, 5, 130, 55);
wphrml.setBounds (425, 5, 130, 55);
jtextarea1.setBounds (5, 5, 275, 295);
num1.setBounds (5, 440, 80, 55);
num2.setBounds (100, 440, 80, 55);
num3.setBounds (195, 440, 80, 55);
num4.setBounds (5, 375, 80, 55);
num5.setBounds (100, 375, 80, 55);
num6.setBounds (195, 375, 80, 55);
num7.setBounds (5, 310, 80, 55);
num8.setBounds (100, 310, 80, 55);
num9.setBounds (195, 310, 80, 55);
num0.setBounds (5, 505, 80, 50);
bkspc.setBounds (100, 505, 80, 50);
enter.setBounds (195, 505, 80, 50);
threelwphrml.setBounds (695, 5, 130, 55);
mswphrml.setBounds (290, 65, 130, 60);
wswphrml.setBounds (425, 65, 130, 60);
chxswfrml.setBounds (695, 65, 130, 60);
chxswclml.setBounds (290, 130, 130, 60);
onepcchxml.setBounds (425, 130, 130, 60);
twopcchxml.setBounds (560, 130, 130, 60);
oringslrg.setBounds (425, 195, 130, 60);
oringssml.setBounds (560, 195, 130, 60);
frfrieslrg.setBounds (695, 195, 130, 60);
frfriessml.setBounds (290, 260, 130, 60);
bkchxspghti.setBounds (695, 130, 130, 60);
bkspghti.setBounds (290, 195, 130, 60);
bksundae.setBounds (425, 260, 130, 60);
bkcone.setBounds (560, 260, 130, 60);
bpwphrml.setBounds (560, 65, 130, 60);
bkjoe.setBounds (290, 325, 130, 60);
hersheypie.setBounds (695, 260, 130, 60);
softdrinklrg.setBounds (425, 325, 130, 60);
softdrinksml.setBounds (560, 325, 130, 60);
upgradefrdr.setBounds (695, 325, 130, 60);
upgradefr.setBounds (290, 390, 130, 60);
upgradedr.setBounds (425, 390, 130, 60);
scdiscount.setBounds (425, 510, 130, 45);
clear.setBounds (290, 455, 130, 50);
total.setBounds (290, 510, 130, 45);
wphrjrml = new JButton ("WPHR JR ML");
wphrjrml.setToolTipText("Whooper Jr Meal - P 99");
twolwphrml = new JButton ("2L WPHR ML");
twolwphrml.setToolTipText("Double Decker Whooper Meal - P 180");
wphrml = new JButton ("WPHR ML");
wphrml.setToolTipText("Whooper Meal - P 129");
jtextarea1 = new JTextArea (5, 5);
jtextarea1.setLineWrap(true);
//JScrollPane(jtextarea1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
num1 = new JButton ("1");
num2 = new JButton ("2");
num3 = new JButton ("3");
num4 = new JButton ("4");
num5 = new JButton ("5");
num6 = new JButton ("6");
num7 = new JButton ("7");
num8 = new JButton ("8");
num9 = new JButton ("9");
num0 = new JButton ("0");
bkspc = new JButton ("BKSPC");
enter = new JButton ("ENTER");
threelwphrml = new JButton ("3L WPHR ML");
threelwphrml.setToolTipText ("Triple Patty Whooper Meal - P 225");
mswphrml = new JButton ("MS WPHR ML");
mswphrml.setToolTipText ("Mushroom Swiss Whooper Meal - P 129");
wswphrml = new JButton ("WS WPHR ML");
wswphrml.setToolTipText ("Western Style Whooper Meal - P 129");
chxswfrml = new JButton ("CHX SW FR ML");
chxswfrml.setToolTipText ("Chicken Sandwich French Style Meal - P 155");
chxswclml = new JButton ("CHX SW CL ML");
chxswclml.setToolTipText ("Chicken Sandwich Classic Style Meal - P 145");
onepcchxml = new JButton ("1PC CHX ML");
onepcchxml.setToolTipText ("1 pc. Chicken Meal - P 89");
twopcchxml = new JButton ("2PC CHX ML");
twopcchxml.setToolTipText ("2 pc. Royal Chicken Meal - P 149");
oringslrg = new JButton ("O RINGS LRG");
oringslrg.setToolTipText ("Onion Rings Large - P 39");
oringssml = new JButton ("O RINGS SML");
oringssml.setToolTipText ("Onion Rings Small - P 27");
frfrieslrg = new JButton ("FR FRIES LRG");
frfrieslrg.setToolTipText ("French Fries Large - P 36");
frfriessml = new JButton ("FR FRIES SML");
frfriessml.setToolTipText ("French Fries Small - P 25");
bkchxspghti = new JButton ("BK CHX SPGHTI");
bkchxspghti.setToolTipText ("BK Chicken with Spaghetti Meal - P 89");
bkspghti = new JButton ("BK SPGHTI");
bkspghti.setToolTipText ("BK Spaghetti - P 39");
bksundae = new JButton ("BK SUNDAE");
bksundae.setToolTipText ("BK Sundae - P 25");
bkcone = new JButton ("BK CONE");
bkcone.setToolTipText ("Ice Cream Cone - P 12");
bpwphrml = new JButton ("BP WPHR ML");
bpwphrml.setToolTipText ("Black Pepper Whooper Meal - P 99");
bkjoe = new JButton ("BK JOE");
bkjoe.setToolTipText ("BK Joe Coffee - P 49");
hersheypie = new JButton ("HERSHEY PIE");
hersheypie.setToolTipText ("Hershey's Sundae Pie - P 75");
softdrinklrg = new JButton ("SOFTDRINK LRG");
softdrinklrg.setToolTipText ("Softdrinks Large - P 26");
softdrinksml = new JButton ("SOFTDRINK SML");
softdrinksml.setToolTipText ("Softdrinks Small - P 16");
upgradefrdr = new JButton ("UPGRADE FR DR");
upgradefrdr.setToolTipText ("Upgrade french fries and drinks to large - P 19");
upgradefr = new JButton ("UPGRADE FR");
upgradefr.setToolTipText ("Upgrade french fries to large - P 11");
upgradedr = new JButton ("UPGRADE DR");
upgradedr.setToolTipText ("Upgrade drinks to large - P 10");
scdiscount = new JButton ("SC DISCOUNT");
scdiscount.setToolTipText ("Senior Citizen's Discount");
total = new JButton ("TOTAL");
clear = new JButton ("CLEAR");
//}
// 1st Trial Code for the calculations in the menu
/*
public void clearActionPerformed(java.awt.event.ActionEvent evt)
{
jtextarea1.setText("");
}
public void whprmlActionPerformed(java.awt.event.ActionEvent evt)
{
double wphrml;
wphrml = Double.parseDouble(129.00);
jtextarea1.setText(String.valueOf(wphrml));
}
public void twolwphrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double twolwphrml;
twolwphrml = Double.parseDouble(180.00);
jtextarea1.setText(String.valueOf(twolwphrml));
}
public void threelwphrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double threelwphrml;
threelwphrml = Double.parseDouble(225.00);
jtextarea1.setText(String.valueOf(threelwphrml));
}
public void wphrjrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double wphrjrml;
wphrjrml = Double.parseDouble(99.00);
jtextarea1.setText(String.valueOf(wphrjrml));
}
public void mswhprmlActionPerformed(java.awt.event.ActionEvent evt)
{
double mswhprml;
mswhprml = Double.parseDouble(99.00);
jtextarea1.setText(String.valueOf(mswhprml));
}
public void wswhprmlActionPerformed(java.awt.event.ActionEvent evt)
{
double wswhprml;
wswphrml = Double.parseDouble(129.00);
jtextarea1.setText(String.valueOf(wswphrml));
}
public void bpwphrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double bpwphrml;
bpwphrml = Double.parseDouble(129.00);
jtextarea1.setText(String.valueOf(bpwphrml));
}
public void chxswfrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double chxswfrml;
chxswfrml = Double.parseDouble(149.00);
jtextarea1.setText(String.valueOf(chxswfrml));
}
public void chxswclmlActionPerformed(java.awt.event.ActionEvent evt)
{
double chxswclml;
chxswclml = Double.parseDouble(139.00);
jtextarea1.setText(String.valueOf(chxswclml));
}
public void onepcchxmlActionPerformed(java.awt.event.ActionEvent evt)
{
double onepcchxml;
onepcchxml = Double.parseDouble(89.00);
jtextarea1.setText(String.valueOf(onepcchxml));
}
public void twopcchxmlActionPerformed(java.awt.event.ActionEvent evt)
{
double twopcchxml;
twopcchxml = Double.parseDouble(149.00);
jtextarea1.setText(String.valueOf(twopcchxml));
}
public void oringslrgActionPerformed(java.awt.event.ActionEvent evt)
{
double oringslrg;
oringslrg = Double.parseDouble(39.00);
jtextarea1.setText(String.valueOf(oringslrg));
}
public void oringssmlActionPerformed(java.awt.event.ActionEvent evt)
{
double oringssml;
oringssml = Double.parseDouble(27.00);
jtextarea1.setText(String.valueOf(oringssml));
}
public void frfrieslrgActionPerformed(java.awt.event.ActionEvent evt)
{
double frfrieslrg;
frfrieslrg = Double.parseDouble(36.00);
jtextarea1.setText(String.valueOf(frfrieslrg));
}
public void frfriessmlActionPerformed(java.awt.event.ActionEvent evt)
{
double frfriessml;
frfriessml = Double.parseDouble(25.00);
jtextarea1.setText(String.valueOf(frfriessml));
}
public void bkchxspghtiActionPerformed(java.awt.event.ActionEvent evt)
{
double bkchxspghti;
bkchxspghti = Double.parseDouble(39.00);
jtextarea1.setText(String.valueOf(bkchxspghti));
}
public void bkspghtiActionPerformed(java.awt.event.ActionEvent evt)
{
double bkspghti;
bkspghti = Double.parseDouble(89.00);
jtextarea1.setText(String.valueOf(bkspghti));
}
public void bksundaeActionPerformed(java.awt.event.ActionEvent evt)
{
double bksundae;
bksundae = Double.parseDouble(25.00);
jtextarea1.setText(String.valueOf(bksundae));
public void bkconeActionPerformed(java.awt.event.ActionEvent evt)
{
double bkcone;
bkcone = Double.parseDouble(12.00);
jtextarea1.setText(String.valueOf(bkcone));
public void bkjoeActionPerformed(java.awt.event.ActionEvent evt)
{
double bkjoe;
bkjoe = Double.parseDouble(49.00);
jtextarea1.setText(String.valueOf(bkjoe));
public void hersheypieActionPerformed(java.awt.event.ActionEvent evt)
{
double hersheypie;
hersheypie = Double.parseDouble(75.00);
jtextarea1.setText(String.valueOf(hersheypie));
public void softdrinklrgActionPerformed(java.awt.event.ActionEvent evt)
{
double softdrinklrg;
softdrinklrg = Double.parseDouble(26.00);
jtextarea1.setText(String.valueOf(softdrinklrg));
public void softdrinksmlActionPerformed(java.awt.event.ActionEvent evt)
{
double softdrinksml;
softdrinksml = Double.parseDouble(16.00);
jtextarea1.setText(String.valueOf(softdrinksml));
}
public void upgradefrdrActionPerformed(java.awt.event.ActionEvent evt)
{
double upgradefrdr;
upgradefrdr = Double.parseDouble(19.00);
jtextarea1.setText(String.valueOf(upgradefrdr));
public void upgradefrActionPerformed(java.awt.event.ActionEvent evt)
{
double upgradefr;
upgradefr = Double.parseDouble(11.00);
jtextarea1.setText(String.valueOf(upgradefr));
}
public void upgradedrActionPerformed(java.awt.event.ActionEvent evt)
{
double upgradedr;
upgradedr = Double.parseDouble(10.00);
jtextarea1.setText(String.valueOf(upgradedr));
}
public void scdiscountActionPerformed(java.awt.event.ActionEvent evt)
{
double scdiscount;
scdiscount = 0.2 * total;
total = 0.8 * total;
jtextarea1.setText(String.valueOf(scdiscount));
}
public void totalActionPerformed(java.awt.event.ActionEvent evt)
{
jtextarea1.setText(total);
}
*/
// 2nd Trial Code for the calculations in the menu
/* public void {
double total;
total = 0.0;
clearActionPerformed(java.awt.event.ActionEvent evt)
{
jtextarea1.setText("");
}
whprmlActionPerformed(java.awt.event.ActionEvent evt)
{
double wphrml;
wphrml = Double.parseDouble(129.00);
total += wphrml;
jtextarea1.setText(String.valueOf(wphrml));
}
twolwphrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double twolwphrml;
twolwphrml = Double.parseDouble(180.00);
total += twolwphrml;
jtextarea1.setText(String.valueOf(twolwphrml));
}
threelwphrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double threelwphrml;
threelwphrml = Double.parseDouble(225.00);
total += threelwphrml
jtextarea1.setText(String.valueOf(threelwphrml));
}
wphrjrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double wphrjrml;
wphrjrml = Double.parseDouble(99.00);
total += wphrjrml
jtextarea1.setText(String.valueOf(wphrjrml));
}
mswhprmlActionPerformed(java.awt.event.ActionEvent evt)
{
double mswhprml;
mswhprml = Double.parseDouble(99.00);
total += mswhprml;
jtextarea1.setText(String.valueOf(mswhprml));
}
wswhprmlActionPerformed(java.awt.event.ActionEvent evt)
{
double wswhprml;
wswphrml = Double.parseDouble(129.00);
total += wswphrml;
jtextarea1.setText(String.valueOf(wswphrml));
}
bpwphrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double bpwphrml;
bpwphrml = Double.parseDouble(129.00);
total += bpwphrml;
jtextarea1.setText(String.valueOf(bpwphrml));
}
chxswfrmlActionPerformed(java.awt.event.ActionEvent evt)
{
double chxswfrml;
chxswfrml = Double.parseDouble(149.00);
total += chxswfrml;
jtextarea1.setText(String.valueOf(chxswfrml));
}
chxswclmlActionPerformed(java.awt.event.ActionEvent evt)
{
double chxswclml;
chxswclml = Double.parseDouble(139.00);
total += chxswclml;
jtextarea1.setText(String.valueOf(chxswclml));
}
onepcchxmlActionPerformed(java.awt.event.ActionEvent evt)
{
double onepcchxml;
onepcchxml = Double.parseDouble(89.00);
total += onepcchxml;
jtextarea1.setText(String.valueOf(onepcchxml));
}
twopcchxmlActionPerformed(java.awt.event.ActionEvent evt)
{
double twopcchxml;
twopcchxml = Double.parseDouble(149.00);
total += twopcchxml;
jtextarea1.setText(String.valueOf(twopcchxml));
}
oringslrgActionPerformed(java.awt.event.ActionEvent evt)
{
double oringslrg;
oringslrg = Double.parseDouble(39.00);
total += oringslrg;
jtextarea1.setText(String.valueOf(oringslrg));
}
oringssmlActionPerformed(java.awt.event.ActionEvent evt)
{
double oringssml;
oringssml = Double.parseDouble(27.00);
total += oringssml;
jtextarea1.setText(String.valueOf(oringssml));
}
frfrieslrgActionPerformed(java.awt.event.ActionEvent evt)
{
double frfrieslrg;
frfrieslrg = Double.parseDouble(36.00);
total += frfrieslrg;
jtextarea1.setText(String.valueOf(frfrieslrg));
}
frfriessmlActionPerformed(java.awt.event.ActionEvent evt)
{
double frfriessml;
frfriessml = Double.parseDouble(25.00);
total += frfriessml;
jtextarea1.setText(String.valueOf(frfriessml));
}
bkchxspghtiActionPerformed(java.awt.event.ActionEvent evt)
{
double bkchxspghti;
bkchxspghti = Double.parseDouble(39.00);
total += bkchxspghti;
jtextarea1.setText(String.valueOf(bkchxspghti));
}
bkspghtiActionPerformed(java.awt.event.ActionEvent evt)
{
double bkspghti;
bkspghti = Double.parseDouble(89.00);
total += bkspghti;
jtextarea1.setText(String.valueOf(bkspghti));
}
bksundaeActionPerformed(java.awt.event.ActionEvent evt)
{
double bksundae;
bksundae = Double.parseDouble(25.00);
total += bksundae;
jtextarea1.setText(String.valueOf(bksundae));
bkconeActionPerformed(java.awt.event.ActionEvent evt)
{
double bkcone;
bkcone = Double.parseDouble(12.00);
total += bkcone;
jtextarea1.setText(String.valueOf(bkcone));
bkjoeActionPerformed(java.awt.event.ActionEvent evt)
{
double bkjoe;
bkjoe = Double.parseDouble(49.00);
total += bkjoe;
jtextarea1.setText(String.valueOf(bkjoe));
hersheypieActionPerformed(java.awt.event.ActionEvent evt)
{
double hersheypie;
hersheypie = Double.parseDouble(75.00);
total += hersheypie;
jtextarea1.setText(String.valueOf(hersheypie));
softdrinklrgActionPerformed(java.awt.event.ActionEvent evt)
{
double softdrinklrg;
softdrinklrg = Double.parseDouble(26.00);
total += softdrinklrg;
jtextarea1.setText(String.valueOf(softdrinklrg));
softdrinksmlActionPerformed(java.awt.event.ActionEvent evt)
{
double softdrinksml;
softdrinksml = Double.parseDouble(16.00);
total += softdrinksml;
jtextarea1.setText(String.valueOf(softdrinksml));
}
upgradefrdrActionPerformed(java.awt.event.ActionEvent evt)
{
double upgradefrdr;
upgradefrdr = Double.parseDouble(19.00);
total += upgradefrdr;
jtextarea1.setText(String.valueOf(upgradefrdr));
upgradefrActionPerformed(java.awt.event.ActionEvent evt)
{
double upgradefr;
upgradefr = Double.parseDouble(11.00);
total += upgradefr;
jtextarea1.setText(String.valueOf(upgradefr));
}
upgradedrActionPerformed(java.awt.event.ActionEvent evt)
{
double upgradedr;
upgradedr = Double.parseDouble(10.00);
total += upgradedr;
jtextarea1.setText(String.valueOf(upgradedr));
}
scdiscountActionPerformed(java.awt.event.ActionEvent evt)
{
double scdiscount;
scdiscount = 0.2 * total;
total = 0.8 * total;
jtextarea1.setText(String.valueOf(scdiscount));
}
totalActionPerformed(java.awt.event.ActionEvent evt)
{
jtextarea1.setText(total);
}
*/
}
public static void main (String[] args) {
JFrame frame = new JFrame ("Trial1");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new Trial1());
frame.pack();
frame.setVisible (true);
}
}