Help with simple Applet code
I am a newbie java programmer (or programmer in general)
the code I am posting about has certain guidelines it must abide by:
applet no bigger than 200x440
applet titled "The Menu Program"
This is an incomplete code so far but I am unable to move past this point because I have no idea why it is doing this.
main code, I left the original code but parts of it are //commented out because I decided to try something new by adding the second code.
Code java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class DinnerMenu extends Applet implements ItemListener
{
int dollar = 0;
int cents = 00;
Label total = new Label("Dinner Price is: $" + dollar + (".") + cents);
Label dm = new Label("Dinner Menu");
Label instruct = new Label(" Please select one item from");
Label instruct2 = new Label(" each category.");
Label instruct3 = new Label(" Your total will be displayed");
Label instruct4 = new Label(" below.");
Label soupl = new Label("Soups********************");
Label entreel = new Label("Entrees********************");
Label dessertl = new Label("Desserts********************");
CheckboxGroup soups = new CheckboxGroup();
Checkbox clam = new Checkbox("Clam Chowder",soups,false);
int s11 = 2;
int s12 = 79;
Checkbox veg = new Checkbox("Vegetable Soup",soups,false);
int s21 = 2;
int s22 = 99;
Checkbox broth = new Checkbox("Peppered Chicken Broth",soups,false);
int s31 = 2;
int s32 = 49;
entree meal = new entree();
/*CheckboxGroup entree = new CheckboxGroup();
Checkbox chic = new Checkbox("Chicken",entree,false);
int e11 = 12;
int e12 = 79;
Checkbox fish = new Checkbox("Fish",entree,false);
int e21 = 10;
int e22 = 99;
Checkbox beef = new Checkbox("Beef",entree,false);
int e31 = 14;
int e32 = 99;*/
CheckboxGroup dessert = new CheckboxGroup();
Checkbox van = new Checkbox("Vanilla Ice Cream",dessert,false);
int d11 = 2;
int d12 = 79;
Checkbox rice = new Checkbox("Rice Pudding",dessert,false);
int d21 = 2;
int d22 = 99;
Checkbox cake = new Checkbox("Cheesecake",dessert,false);
int d31 = 4;
int d32 = 29;
Font ft = new Font("Arial", Font.PLAIN, 14);
Font bigft = new Font("Arial", Font.PLAIN, 28);
Font boldft = new Font("Arial", Font.BOLD, 16);
public void init ()
{
setLayout(new GridLayout(0,1));
setBackground(Color.pink);
setFont(bigft);
add(dm);
setFont(ft);
add(instruct);
add(instruct2);
add(instruct3);
add(instruct4);
setFont(boldft);
add(soupl);
setFont(ft);
add(clam);
add(veg);
add(broth);
setFont(boldft);
add(entreel);
add(meal);
meal.setVisible(true); // comes out blank in the applet
/*setFont(ft);
add(chic);
add(fish);
add(beef);*/
setFont(boldft);
add(dessertl);
setFont(ft);
add(van);
add(rice);
add(cake);
setFont(boldft);
add(total);
// repaint();
}
public void itemStateChanged (ItemEvent e)
{
}
}
and the second code is as follows:
Code java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class entree extends Panel
{
CheckboxGroup ent = new CheckboxGroup();
Checkbox chic = new Checkbox("Chicken",ent,false);
int e11 = 12;
int e12 = 79;
Checkbox fish = new Checkbox("Fish",ent,false);
int e21 = 10;
int e22 = 99;
Checkbox beef = new Checkbox("Beef",ent,false);
int e31 = 14;
int e32 = 99;
Font ft = new Font("Arial", Font.PLAIN, 14);
public void entree ()
{
this.setVisible(true);
setLayout(new GridLayout(1,0));
setBackground(Color.pink);
setFont(ft);
add(chic);
add(fish);
add(beef);
}
}
my questions are:
1. why are all the fonts the same and how can i fix it?
2. why wont the object meal show up in the applet?
3.how can i have cents display both zeros while its still 0 instead of the single zero?
thanks for your help
Re: Help with simple Applet code
applet no bigger than 200x440
applet titled "The Menu Program"
Well, I never tried it before, but is it possible to extend a JFrame but use an object Applet so that you can set the title that way? Also, maybe this works for Applets too, but did you try setAdjustable(false) or maybe it's called setReadjustable(false), or something that like. You could set size for 200 by 440. Only problem is, it'd be that size exactly, not bigger, no smaller either.
Also, typically some of the graphics in Applets and JApplets are done with a method called paint(Graphics g).
Well, for painting stuff onto the Applet itself you do that.
To set the font for components, couldn't you just call the setFont() for each component?
What it appears you're doing is setting the font for the entire applet and keep changing it.
The cent thing could be done a couple of ways.
You could tell it to append a 0 to the String if it's .00, or you could %.2f or something like that.
Also, this link might help.
NumberFormat (Java 2 Platform SE v1.4.2)
Your meal Object won't show up as you have to tell it to setVisible(true) I think in your constructor for that meal object. The constructors for all your other components already have a setVisible(true) in them.
That might be why.
Applets usually don't have Titles. That's left for the HTML code to figure out.
The only way to maybe get a Title would be to use a JFrame as your main object of your applet and put all components in there. May not work with Applets. I usually don't use them.
I know that trick works with a JFrame with a JPanel as the main Object. Then I use setConentPane(panelObject), but that won't work with Applets I don't think.
Ok, this may be a long shot, but I noticed a method called setContentPane(Container pane).
Container is an ancestor of Applet. Therefore an Applet is a Container.
If you extended JFrame, had an Applet object, and setContentPane(AppletObject), it might let you set a Title. I've never tried it.
Also, JFrame has a method called setResizable(boolean aFlag).
I tried setMaximumSize() once, but it can go beyond that. I just hope you won't get in trouble if it can't go any smaller either.
Ok, you'll have to change the maximum values I've got. It's the last one in the JScrollBar Constructor. You can change the minimum, the second to last one, too if you want.
You'll have to change the instance of the class I used it for to the instance of the class you want it for.
The way I have it, it should be an inner class of your JFrame class.
Code java:
private class JWindowAdjuster extends JFrame
{
private JScrollBar length, width;
private JPanel panel;
private JLabel lengthLabel, widthLabel;
private ButtonGroup group;
private JRadioButtonMenuItem fifteen, thirty, five, forty, hundred;
private JMenuBar aBar;
private JMenu adjustment;
public JWindowAdjuster() throws PrinterException, BadLocationException
{
setTitle("Adjust Size of Window");
Dimension d = new Dimension(400,400);
setFinalSize(d);
setResizable(false);
Clock c5 = new Clock();
// final Testing wenguin = new Testing();
setVisible(true);
length = new JScrollBar(JScrollBar.HORIZONTAL, 300, 0, 30, 2300);
width = new JScrollBar(JScrollBar.HORIZONTAL, 300, 0, 30, 2300);
mBar = new JMenuBar();
panel = new JPanel();
panel.setBackground(Color.BLUE);
group = new ButtonGroup();
mBar.setBackground(Color.ORANGE);
setJMenuBar(mBar);
length.setUnitIncrement(1);
lengthLabel = new JLabel("Length:");
adjustment = new JMenu("Adjustment Options");
mBar.add(adjustment);
fifteen = new JRadioButtonMenuItem("By 15");
adjustment.add(fifteen);
thirty = new JRadioButtonMenuItem("By 30");
adjustment.add(thirty);
five = new JRadioButtonMenuItem("By 5");
adjustment.add(five);
group.add(fifteen);
group.add(thirty);
group.add(five);
panel.add(lengthLabel);
panel.add(length);
widthLabel = new JLabel("Width");
panel.add(widthLabel);
panel.add(width);
length.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent arg0) {
// TODO Auto-generated method stub
lengthLabel.setText("Length: " + arg0.getValue());
Testing t5 = Testing.this;
t5.setSize(t5.getWidth(), arg0.getValue());
}
});
width.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent arg0) {
// TODO Auto-generated method stub
widthLabel.setText("Width: " + arg0.getValue());
Testing t5 = Testing.this;
t5.setSize( arg0.getValue(), t5.getHeight());
}
});
setContentPane(panel);
}
public void setFinalSize(Dimension d)
{
this.setMaximumSize(d);
this.setMinimumSize(d);
}
}
Ok, confirmed that the JFrame way will work. However, you would need a main method. If this is going online, this may not be the best route. But it's the only way I know that will get the title to work.
Applets normally have titles set by HTML code.
Re: Help with simple Applet code
applet no bigger than 200x440
applet titled "The Menu Program"
Well, I never tried it before, but is it possible to extend a JFrame but use an object Applet so that you can set the title that way? Also, maybe this works for Applets too, but did you try setAdjustable(false) or maybe it's called setReadjustable(false), or something that like. You could set size for 200 by 440. Only problem is, it'd be that size exactly, not bigger, no smaller either.
Also, typically some of the graphics in Applets and JApplets are done with a method called paint(Graphics g).
Well, for painting stuff onto the Applet itself you do that.
To set the font for components, couldn't you just call the setFont() for each component?
What it appears you're doing is setting the font for the entire applet and keep changing it.
The cent thing could be done a couple of ways.
You could tell it to append a 0 to the String if it's .00, or you could %.2f or something like that.
Also, this link might help.
NumberFormat (Java 2 Platform SE v1.4.2)
Your meal Object won't show up as you have to tell it to setVisible(true) I think in your constructor for that meal object. The constructors for all your other components already have a setVisible(true) in them.
That might be why.
Applets usually don't have Titles. That's left for the HTML code to figure out.
The only way to maybe get a Title would be to use a JFrame as your main object of your applet and put all components in there. May not work with Applets. I usually don't use them.
I know that trick works with a JFrame with a JPanel as the main Object. Then I use setConentPane(panelObject), but that won't work with Applets I don't think.
Ok, this may be a long shot, but I noticed a method called setContentPane(Container pane).
Container is an ancestor of Applet. Therefore an Applet is a Container.
If you extended JFrame, had an Applet object, and setContentPane(AppletObject), it might let you set a Title. I've never tried it.
Also, JFrame has a method called setResizable(boolean aFlag).
I tried setMaximumSize() once, but it can go beyond that. I just hope you won't get in trouble if it can't go any smaller either.
Ok, you'll have to change the maximum values I've got. It's the last one in the JScrollBar Constructor. You can change the minimum, the second to last one, too if you want.
You'll have to change the instance of the class I used it for to the instance of the class you want it for.
The way I have it, it should be an inner class of your JFrame class.
Code java:
private class JWindowAdjuster extends JFrame
{
private JScrollBar length, width;
private JPanel panel;
private JLabel lengthLabel, widthLabel;
private ButtonGroup group;
private JRadioButtonMenuItem fifteen, thirty, five, forty, hundred;
private JMenuBar aBar;
private JMenu adjustment;
public JWindowAdjuster() throws PrinterException, BadLocationException
{
setTitle("Adjust Size of Window");
Dimension d = new Dimension(400,400);
setFinalSize(d);
setResizable(false);
Clock c5 = new Clock();
// final Testing wenguin = new Testing();
setVisible(true);
length = new JScrollBar(JScrollBar.HORIZONTAL, 300, 0, 30, 2300);
width = new JScrollBar(JScrollBar.HORIZONTAL, 300, 0, 30, 2300);
mBar = new JMenuBar();
panel = new JPanel();
panel.setBackground(Color.BLUE);
group = new ButtonGroup();
mBar.setBackground(Color.ORANGE);
setJMenuBar(mBar);
length.setUnitIncrement(1);
lengthLabel = new JLabel("Length:");
adjustment = new JMenu("Adjustment Options");
mBar.add(adjustment);
fifteen = new JRadioButtonMenuItem("By 15");
adjustment.add(fifteen);
thirty = new JRadioButtonMenuItem("By 30");
adjustment.add(thirty);
five = new JRadioButtonMenuItem("By 5");
adjustment.add(five);
group.add(fifteen);
group.add(thirty);
group.add(five);
panel.add(lengthLabel);
panel.add(length);
widthLabel = new JLabel("Width");
panel.add(widthLabel);
panel.add(width);
length.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent arg0) {
// TODO Auto-generated method stub
lengthLabel.setText("Length: " + arg0.getValue());
Testing t5 = Testing.this;
t5.setSize(t5.getWidth(), arg0.getValue());
}
});
width.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent arg0) {
// TODO Auto-generated method stub
widthLabel.setText("Width: " + arg0.getValue());
Testing t5 = Testing.this;
t5.setSize( arg0.getValue(), t5.getHeight());
}
});
setContentPane(panel);
}
public void setFinalSize(Dimension d)
{
this.setMaximumSize(d);
this.setMinimumSize(d);
}
}
Ok, confirmed that the JFrame way will work. However, you would need a main method. If this is going online, this may not be the best route. But it's the only way I know that will get the title to work.
Applets normally have titles set by HTML code.
Re: Help with simple Applet code
the html portion I left out because i thought it was a no brainer that i was using html...
<html>
<p align=center>
<applet code="DinnerMenu.class" height=440 width=200>
</applet>
</p>
</html>
how do i fix my other questions?
Re: Help with simple Applet code
ok i understand that i am changing the font for the entire applet when i do that... but doesnt it read the statement to change font --> add label --> change font... so the label should be placed with the font that was there prviously... or am i completely wrong?
as with setting the 'meal' object to visible, i have the constructor in the actual class as 'this.setVisible(true);' ... that didnt work then i added 'meal.setVisible(true);' right after i added it to the actual applet and that still didnt work...
this isnt the first project ive worked on when it seems that the object i make isnt even there. So if its a reoccurence I must be the moron haha.
I went to the site you gave me and... uh... ya i didnt understand a word that was said on there... im thinking of adding an if statement:
Code java:
if (soup&&meal&&dessert==false)
String zero = new String("0");
then place the string where i want it via coordinates.
any ideas?
thanks again
Re: Help with simple Applet code
I'd suggest breaking the problem down (posting many questions with chunks of code such as this can end up getting a bit confusing on both ends). Deal with one question at a time. If the font isn't working, strip down the code to a minimal example and post an SSCCE that demonstrates the problem you are having...this helps to you break the problem down (and sometimes in the process allows you to see what's going on) as well as easily demonstrates to us the scope of the problem. Once that is solved the other issues can be dealt with in turn
Re: Help with simple Applet code
Quote:
Originally Posted by
that_guy
ok i understand that i am changing the font for the entire applet when i do that... but doesnt it read the statement to change font --> add label --> change font... so the label should be placed with the font that was there prviously... or am i completely wrong?
as with setting the 'meal' object to visible, i have the constructor in the actual class as 'this.setVisible(true);' ... that didnt work then i added 'meal.setVisible(true);' right after i added it to the actual applet and that still didnt work...
this isnt the first project ive worked on when it seems that the object i make isnt even there. So if its a reoccurence
I must be the moron haha.
I went to the site you gave me and... uh... ya i didnt understand a word that was said on there... im thinking of adding an if statement:
Code java:
if (soup&&meal&&dessert==false)
String zero = new String("0");
then place the string where i want it via coordinates.
any ideas?
thanks again
Usually if you don't set a component class or object, it'll use your class itself to call that method.
Re: Help with simple Applet code
fine...
lets start with the question of "why doesnt the meal object show up when i run the applet?"
when the applet runs a pink box shows up of the dimensions i told it to (which is good)...
on the box i can see that everything i placed with the .add() is in a column in the order i put them there (which is good)
between entrees ans desserts there is a blank spot showing that it understands something is supposed to go there (the checkboxes from the entree class) but nothing is there but space (which is the problem)
if you compile the souce code i provided earlier then run it off an applet with html code as follows:
Code java:
<html>
<title>The Dinner Menu</title>
<p align=center>
<applet code="DinnerMenu.class" height=440 width=200>
</applet>
</p>
</html>
then you should see the problem i am having
... so my question is: "how do i make the meal object show up in the applet?"
Re: Help with simple Applet code
I presume you want this to be a constructor and not a method, and given it has a return value it is treated as a method (and thus never called). Remove the void and see what happens...
Re: Help with simple Applet code
wow... ok then it works AND the font for the object is what its supposed to be....
now on to question 2: "How do I make the fonts different?"
"Dinner Menu" at the top is supposed to be "Arial", Font.PLAIN, 28
the checkboxes are suppsed to be "Arial", Font.PLAIN, 14
and the headers of each group (soups, entrees, desserts) are supposed to be "Arial", Font.BOLD, 16
currently all (except the meal object checkboxes) are the latter font.
Re: Help with simple Applet code
Call setFont with the appropriate font on all the components you wish to have a custom font.
Re: Help with simple Applet code
Quote:
Originally Posted by
copeg
Call setFont with the appropriate font on all the components you wish to have a custom font.
i am sorry, but what do you mean by that?
changing the font for the entire applet then adding a component then changing it again in the init() method doesnt work and i dunno why...
are you suggesting something like
Code java:
soupl.setFont("Arial", Font.BOLD, 16);
that?
Re: Help with simple Applet code
Each component (be it a JLabel, JCheckbox, etc..) must have its font individually set....by default calling setFont on the container (applet) won't change the fonts of any added components - you must do so manually.
Re: Help with simple Applet code