Seticon

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soty
    New Member
    • Mar 2008
    • 25

    Seticon

    PLEASE I NEED HELP ON THIS CODE.


    I'M TRYING TO WRITE A CODE ON VENDING MACHINE. BUT FIRST OF ALL I NEED TO PUT PICTURES ON THE BUTTONS THAT I'M USING.

    THE ThING IS I'M HAVING ERRORS WHEN USING THE SETICON. this is my code below....... (i jus want to put pictures on button before i proceed with the code.)

    thanks







    import java.awt.*;
    import java.applet.*;
    import java.awt.event. ActionListener;
    import javax.swing.Ima geIcon;
    import javax.swing.JTe xtPane;


    public class vendmachine extends Applet
    {
    Color Armygreen = new Color(120, 100, 50);

    Panel panel1 = new Panel();
    Label header = new Label("WELCOME TO UJU'S VENDING MACHINE");


    Panel panel2 = new Panel();
    Button chip = new Button("Chips") ;
    Button cookie = new Button("Coookie ");
    Button walnut = new Button("Walnut" );
    Button mintGum = new Button("MintGum ");
    Button lifesaver = new Button("lifeSav er");
    Button popcorn = new Button("Popcorn ");
    Button nacho = new Button("Nacho") ;
    Button mandm = new Button("M&M");
    Button mixednut = new Button("mixed Nut");




    Panel panel3 = new Panel();
    JTextPane mytextArea = new JTextPane();



    Panel panel4 = new Panel();


    Panel panel5 = new Panel();



    public void init()
    {
    setSize(400, 420);
    setLayout(new BorderLayout()) ;


    add(panel1);
    add(panel1, BorderLayout.NO RTH);
    panel1.add(head er);
    panel1.setPrefe rredSize(new Dimension(400,5 0));
    panel1.setBackg round(Color.GRA Y);



    add(panel2);
    add(panel2, BorderLayout.WE ST);
    panel2.setPrefe rredSize(new Dimension(270 , 50));
    panel2.add(chip );
    panel2.add(cook ie);
    panel2.add(waln ut);
    panel2.add(mint Gum);
    panel2.add(life saver);
    panel2.add(popc orn);
    panel2.add(nach o);
    panel2.add(mand m);
    panel2.add(mixe dnut);
    panel2.setBackg round(Color.BLU E);



    /* add(panel3);
    add(panel3,Bord erLayout.CENTER );
    // panel3.add(myte xtArea);
    panel3.setPrefe rredSize(new Dimension(400,1 00));
    // mytextArea.setB ackground(Color .PINK);
    panel3.setBackg round(Color.CYA N);*/



    add(panel4);
    add(panel4,Bord erLayout.SOUTH) ;
    panel4.setPrefe rredSize(new Dimension(400,1 00));
    panel4.setBackg round(Color.RED );



    add(panel5);
    add(panel5,Bord erLayout.CENTER );
    panel5.setPrefe rredSize(new Dimension(400,1 00));
    panel5.setBackg round(Color.GRE EN);




    }



    public void paint(Graphics g)
    {
    //Image chips;
    //Image mintGum, nachos, cookies, lifeSaver, MandM,walnut, popcorn,mixedNu t;


    chip.SetIcon(ne w ImageIcon("chip s1.gif"));
    mintGum.SetIcon (new ImageIcon("mint Gum.gif"));
    nacho.SetIcon(n ew ImageIcon("nach os.gif"));
    cookie.SetIcon( new ImageIcon("cook ies.gif"));
    lifesaver.SetIc on(new ImageIcon("life saver.gif"));
    mandm.SetIcon(n ew ImageIcon("m_m. gif"));
    walnut.SetIcon( new ImageIcon("waln ut.gif"));
    popcorn.SetIcon (new ImageIcon("popc orn.gif"));
    mixednut.SetIco n(new ImageIcon("mixf ruit.gif"));



    // g.drawImage(chi ps, 20, 20, this);
    }


    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Set the icons to the buttons when you initialize those JButtons, i.e just after
    you've constructed them. The JButtons take care of the rest themselves.

    kind regards,

    Jos

    Comment

    • soty
      New Member
      • Mar 2008
      • 25

      #3
      Originally posted by JosAH
      Set the icons to the buttons when you initialize those JButtons, i.e just after
      you've constructed them. The JButtons take care of the rest themselves.

      kind regards,

      Jos

      do u mean

      Icon chip instead of Image chip?

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        He means move all the setIcon calls up to the top where you initialize the buttons (create them with the constructors).

        Comment

        • soty
          New Member
          • Mar 2008
          • 25

          #5
          Originally posted by Laharl
          He means move all the setIcon calls up to the top where you initialize the buttons (create them with the constructors).

          pls i really dont understand wat u mean by move it up. cos i did but it still not working. can u write a line of code to explain wat u r tryna say. i'm still very knew to java.

          thank you very much

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            Cut the setIcon calls from where they are and paste them up top.

            Comment

            • soty
              New Member
              • Mar 2008
              • 25

              #7
              Originally posted by Laharl
              Cut the setIcon calls from where they are and paste them up top.


              hi this is wat i did but its still not working dats why i asked for explanation. is this wat u meant?

              thank you



              import java.awt.*;
              import java.applet.*;
              import java.awt.event. ActionListener;
              import javax.swing.Ico n;
              import javax.swing.Ima geIcon;
              import javax.swing.JTe xtPane;


              public class vendmachine extends Applet
              {
              Color Armygreen = new Color(120, 100, 50);

              Panel panel1 = new Panel();
              Label header = new Label("WELCOME TO UJU'S VENDING MACHINE");


              Panel panel2 = new Panel();
              Button chip = new Button();
              Button cookie = new Button();
              Button walnut = new Button();
              Button mintGum = new Button();
              Button lifesaver = new Button();
              Button popcorn = new Button();
              Button nacho = new Button();
              Button mandm = new Button();
              Button mixednut = new Button();

              chip.SetIcon(ne w ImageIcon("chip s1.gif"));
              mintGum.SetIcon (new ImageIcon("mint Gum.gif"));
              nacho.SetIcon(n ew ImageIcon("nach os.gif"));
              cookie.SetIcon( new ImageIcon("cook ies.gif"));
              lifesaver.SetIc on(new ImageIcon("life saver.gif"));
              mandm.SetIcon(n ew ImageIcon("m_m. gif"));
              walnut.SetIcon( new ImageIcon("waln ut.gif"));
              popcorn.SetIcon (new ImageIcon("popc orn.gif"));
              mixednut.SetIco n(new ImageIcon("mixf ruit.gif"));*/




              Panel panel3 = new Panel();
              JTextPane mytextArea = new JTextPane();



              Panel panel4 = new Panel();


              Panel panel5 = new Panel();



              public void init()
              {
              setSize(400, 420);
              setLayout(new BorderLayout()) ;


              add(panel1);
              add(panel1, BorderLayout.NO RTH);
              panel1.add(head er);
              panel1.setPrefe rredSize(new Dimension(400,5 0));
              panel1.setBackg round(Color.GRA Y);



              add(panel2);
              add(panel2, BorderLayout.WE ST);
              panel2.setPrefe rredSize(new Dimension(270 , 50));
              panel2.add(chip );
              panel2.add(cook ie);
              panel2.add(waln ut);
              panel2.add(mint Gum);
              panel2.add(life saver);
              panel2.add(popc orn);
              panel2.add(nach o);
              panel2.add(mand m);
              panel2.add(mixe dnut);
              panel2.setBackg round(Color.BLU E);



              /* add(panel3);
              add(panel3,Bord erLayout.CENTER );
              // panel3.add(myte xtArea);
              panel3.setPrefe rredSize(new Dimension(400,1 00));
              // mytextArea.setB ackground(Color .PINK);
              panel3.setBackg round(Color.CYA N);*/



              add(panel4);
              add(panel4,Bord erLayout.SOUTH) ;
              panel4.setPrefe rredSize(new Dimension(400,1 00));
              panel4.setBackg round(Color.RED );



              add(panel5);
              add(panel5,Bord erLayout.CENTER );
              panel5.setPrefe rredSize(new Dimension(400,1 00));
              panel5.setBackg round(Color.GRE EN);




              }



              }

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                You do have a dangling */ at the end of the setIcon calls that needs to be deleted.

                Comment

                • soty
                  New Member
                  • Mar 2008
                  • 25

                  #9
                  Originally posted by Laharl
                  You do have a dangling */ at the end of the setIcon calls that needs to be deleted.

                  i removed it and its still giving me the same error

                  this is the error----------

                  <identifier>exp ected

                  illegal start of type

                  package chip does not exist

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by soty
                    i removed it and its still giving me the same error

                    this is the error----------

                    <identifier>exp ected

                    illegal start of type

                    package chip does not exist
                    Your code must be full of compilation errors; for one: the name of the method is
                    setIcon (lower case 's') not SetIcon. Who told you to use a capital S?

                    kind regards,

                    Jos

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      I wouldn't mix swing and awt as well ...

                      Comment

                      • soty
                        New Member
                        • Mar 2008
                        • 25

                        #12
                        Originally posted by JosAH
                        Your code must be full of compilation errors; for one: the name of the method is
                        setIcon (lower case 's') not SetIcon. Who told you to use a capital S?

                        kind regards,

                        Jos

                        i asked my classmate and he told me it was a capital S.. but av changed it tho.

                        its still giving me the same error.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by soty
                          i asked my classmate and he told me it was a capital S.. but av changed it tho.

                          its still giving me the same error.
                          Don't ask anyone who knows zilch; read the API Documentation.

                          What are the compiler error diagnostic messages? (copy them verbatim here
                          as well as the relevant code).

                          And remember: don't listen to little PC hackers who think that the whole world
                          is Microsoft crap; it isn't.

                          kind regards,

                          Jos

                          Comment

                          • soty
                            New Member
                            • Mar 2008
                            • 25

                            #14
                            Originally posted by r035198x
                            I wouldn't mix swing and awt as well ...


                            pls i dont understand. r u saying i shouldn't use applets?....tho i'm still trying to understand the relationship btw applet and applications, by removing awt i cant function wif applet right?


                            also r u saying i should use main() instead? and if dats the case how would i build the applet

                            and if am using swing how would i use it without using awt?


                            thank you.

                            Comment

                            • soty
                              New Member
                              • Mar 2008
                              • 25

                              #15
                              Originally posted by JosAH
                              Don't ask anyone who knows zilch; read the API Documentation.

                              What are the compiler error diagnostic messages? (copy them verbatim here
                              as well as the relevant code).

                              And remember: don't listen to little PC hackers who think that the whole world
                              is Microsoft crap; it isn't.

                              kind regards,

                              Jos

                              hi this is the error when i tried running the code




                              init:
                              deps-jar:
                              Compiling 1 source file to /Users/ask4soteria/NetBeansProject s/vendmachine/build/classes
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:32: <identifier> expected
                              chip.setIcon(ne w ImageIcon("chip s1.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:33: <identifier> expected
                              mintGum.setIcon (new ImageIcon("mint Gum.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:34: <identifier> expected
                              nacho.setIcon(n ew ImageIcon("nach os.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:35: <identifier> expected
                              cookie.setIcon( new ImageIcon("cook ies.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:36: <identifier> expected
                              lifesaver.setIc on(new ImageIcon("life saver.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:37: <identifier> expected
                              mandm.setIcon(n ew ImageIcon("m_m. gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:38: <identifier> expected
                              walnut.setIcon( new ImageIcon("waln ut.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:39: <identifier> expected
                              popcorn.setIcon (new ImageIcon("popc orn.gif"));
                              /Users/ask4soteria/NetBeansProject s/vendmachine/src/vendmachine.jav a:40: <identifier> expected
                              mixednut.setIco n(new ImageIcon("mixf ruit.gif"));
                              9 errors
                              BUILD FAILED (total time: 0 seconds)


                              thank you

                              Comment

                              Working...