CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  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

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