CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2010
    Posts
    7

    Exclamation JAVA Applet Help

    Hello. I am busy writing a java applet, but I have encountered the following problems:

    1. When I click on the Blue button, it is suppose to change the background colour to blue. When I click on the Black button, it is suppose to change the background colour to black.Butwhen I click on them, these two actions are not performed. My buttons are working, as I am able to change the caption of the buttons when I click on the buttons, but it does not want to change the background color.

    2. When my applet loads, it only loads the west button.When I hover over the position where the second button is supposed to be, it suddenly appears. I have attached my .java and .class files with a screenshot.

    I will REALY appreciate your help if you can help me asap as this is quite urgent. Thank you.

    Here is my code :

    <CODE>
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.Color;
    import javax.swing.JApplet;
    import java.awt.Graphics;



    /**
    *
    * @author Koos
    */
    public class myApplet extends JApplet implements ActionListener{

    /**
    * Initialization method that will be called after the applet is loaded
    * into the browser.
    */

    private JButton btnSetToBlack,btnSetToBlue;
    private BorderLayout layout;

    public void init() {
    layout = new BorderLayout(50,50);
    setLayout(layout);
    btnSetToBlack = new JButton("BLACK");
    btnSetToBlue = new JButton("BLUE");

    add(btnSetToBlack,BorderLayout.EAST);
    add(btnSetToBlue,BorderLayout.WEST);


    this.setSize(800,600);
    this.setBackground(Color.GRAY);


    this.setVisible(true);

    btnSetToBlack.addActionListener(this);
    btnSetToBlue.addActionListener(this);
    }

    // TODO overwrite start(), stop() and destroy() methods
    public void paint(Graphics g)
    {

    // g.drawRect(100, 50, 150, 200);
    }

    public void start()
    {
    }

    public void actionPerformed(ActionEvent event)
    {
    if (event.getSource() == btnSetToBlack)
    {
    this.setBackground(Color.BLACK);
    btnSetToBlack.setText("clicked");
    repaint();
    }
    if (event.getSource() == btnSetToBlue)
    {
    this.setBackground(Color.BLUE);
    btnSetToBlue.setText("clicked");
    repaint();
    }


    }

    }

    </CODE>
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2010
    Posts
    7

    Re: JAVA Applet Help

    Sorry for not tagging correctly.Here is my code again :


    Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.Color;
    import javax.swing.JApplet;
    import java.awt.Graphics;
    
    
    
    /**
     *
     * @author Koos
     */
    public class myApplet extends JApplet implements ActionListener{
    
        /**
         * Initialization method that will be called after the applet is loaded
         * into the browser.
         */
    
        private JButton btnSetToBlack,btnSetToBlue;
        private BorderLayout layout;
        
       public void init() {
          layout = new BorderLayout(50,50);
          setLayout(layout);
          btnSetToBlack = new JButton("BLACK");
          btnSetToBlue = new JButton("BLUE");
    
          add(btnSetToBlack,BorderLayout.EAST);
          add(btnSetToBlue,BorderLayout.WEST);
    
          
          this.setSize(800,600);
          this.setBackground(Color.GRAY);
        
         
          this.setVisible(true);
          
          btnSetToBlack.addActionListener(this);
          btnSetToBlue.addActionListener(this);
        }
    
        // TODO overwrite start(), stop() and destroy() methods
       public void paint(Graphics g)
       {
         
          //  g.drawRect(100, 50, 150, 200);
       }
    
       public void start()
       {
       }
    
       public void actionPerformed(ActionEvent event)
       {
            if (event.getSource() == btnSetToBlack)
           {
                this.setBackground(Color.BLACK);
                btnSetToBlack.setText("clicked");
                repaint();
           }
           if (event.getSource() == btnSetToBlue)
           {
               this.setBackground(Color.BLUE);
               btnSetToBlue.setText("clicked");
               repaint();
           }
          
        
       }
     
    }

  3. #3
    Join Date
    Mar 2010
    Location
    Mexico
    Posts
    9

    Re: JAVA Applet Help

    Good day,

    Try with this:

    Code:
    getContentPane().setBackground(Color.RED);
    Thanks

  4. #4
    Join Date
    Mar 2010
    Posts
    7

    Re: JAVA Applet Help

    Quote Originally Posted by xlarsx View Post
    Good day,

    Try with this:

    Code:
    getContentPane().setBackground(Color.RED);
    Thanks


    No it still does nothing.I also uploaded the html file if u guys want to have a look.
    Last edited by Stiffmys; March 6th, 2010 at 03:44 AM.

  5. #5
    Join Date
    Mar 2010
    Posts
    7

    Re: JAVA Applet Help

    Nobody has a solution yet? Please this is quite urgent as I am writing a semester test on this work on Monday.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JAVA Applet Help

    Please this is quite urgent as I am writing a semester test on this work on Monday.
    Unfortunately your urgency isn't our problem.

    However I have just had time to have a quick look at this:
    The solution to part of the problem has already been given to you.

    The reason you are seeing drawing issues is that you have overridden the paint() method and not called super.paint() and are not actually doing any painting yourself.

    If you remove the paint() method (or add super.paint() to it) and change the setBackground() call as explained by xlarsx it should all work.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Mar 2010
    Posts
    7

    Re: JAVA Applet Help

    Thank you guys very very much.It is working now.Just one more question: I would like to add a JPanel inside the borders of those two buttons with a button on the JPanel,but I just cant seem to get it right.I've tried various different ways to do it but every time,my JPanel does not show up,or my whole applet just crashes.Your help will be greatly apreciated.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: JAVA Applet Help

    Quote Originally Posted by Stiffmys View Post
    I would like to add a JPanel inside the borders of those two buttons with a button on the JPanel,but I just cant seem to get it right.
    I was going to have a shot at this, but I couldn't make out what you were talking about.

    Judge a man by his questions, rather than his answers...
    Voltaire
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Mar 2010
    Posts
    7

    Re: JAVA Applet Help

    Quote Originally Posted by dlorde View Post
    I was going to have a shot at this, but I couldn't make out what you were talking about.
    Sorry for not being clear. I have added a picture as attachment as how I want my applet to look. In the middle you will see a white retancle. That is the JPanel. On the JPanel I would like to add a circle and a button.

    My problem is, I can't seem to figure out how to add the JPanel in the first place, and secondly how to add the circle and button. In every way I tried so far, my panel either take up the whole applet space or it doesn't appear at all or my button take up the whole space and take the left and right buttons away. I have set the appropriate sizes of everyhing, but I still can't seem to get it to work. I guess I'm going all wrong about this.

    Hope you understand better now and please have a look at the pic. Thanx
    Attached Images Attached Images

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: JAVA Applet Help

    OK, well if you're using a BorderLayout for the main panel, and you've set the preferred width of the two panels (buttons?) 'black' and 'blue' on the EAST and WEST areas, you need to set the central panel in the BorderLayout.CENTER area. This will automatically take up all space not occupied by the EAST and WEST components.

    You can position your circle in the centre panel using another layout manager (for example, a GridBagLayout with contraints anchor set to GridBagLayout.CENTER and constraints fill set to GridBagLayout.NONE). You haven't shown where you want the other button to go, so I can't really help there.

    There are many ways of doing these things, and you can use as many panels and layouts as you like to get things positioned the way you want.

    It's all explained in How To Layout Components.

    The truth is, when all is said and done, one does not teach a subject, one teaches a student how to learn it. Teaching may look like administering a dose, but even a dose must be worked on by the body if it is to cure. Each individual must cure his or her own ignorance...
    J. Barzun
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    Mar 2010
    Posts
    7

    Re: JAVA Applet Help

    Thanx alot, I'll give ita shot.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured