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 14 of 14

Thread: Help with simple Applet code

  1. #1
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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.

    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:


    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


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

    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.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

    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.

  4. #4
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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?
    Last edited by that_guy; January 8th, 2011 at 11:49 PM. Reason: syntax

  5. #5
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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:

    if (soup&&meal&&dessert==false)
              String zero = new String("0");

    then place the string where i want it via coordinates.

    any ideas?


    thanks again

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help with simple Applet code

    Quote Originally Posted by that_guy View Post
    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:

    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.

  8. #8
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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:

    <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?"

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with simple Applet code

    public void entree ()

    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...

  10. The Following User Says Thank You to copeg For This Useful Post:

    that_guy (January 10th, 2011)

  11. #10
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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.

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with simple Applet code

    Call setFont with the appropriate font on all the components you wish to have a custom font.

  13. #12
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Help with simple Applet code

    Quote Originally Posted by copeg View Post
    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

    soupl.setFont("Arial", Font.BOLD, 16);

    that?

  14. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  15. #14
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Help with simple Applet code

    ok thank you so much

Similar Threads

  1. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  2. im dying here..on a simple code
    By Jason in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 28th, 2010, 10:33 PM
  3. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  4. I need help with my ohm's law applet code. can't figure it out!
    By dracula2001 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 06:16 PM
  5. Help! how would you code these simple games?
    By makarov in forum Java Applets
    Replies: 1
    Last Post: November 14th, 2009, 12:31 PM