DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    superwebmonkey Guest

    Can't make buttons work!!


    So, I started writing my first applet...step by step.
    Made text appear..cheanged the color.. made it ask the html for height and
    width...added a button..
    But now I'm stuck I can't make the button DO anything!
    Please help.
    Here is the unfinished code, I know the last part isn't correct...that's
    where my problem is!!

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    public class MemoryApplet extends Applet{
    String text="This is from Memory!!";
    public void init(){
    setBackground(Color.red);
    text = "This is from Memory!!";
    Button b;
    b = new Button("Click!");
    add(b);
    }
    public void paint(Graphics g)
    {
    g.setColor(Color.blue);
    g.drawRect(0, 0,
    getSize().height -1,
    getSize().width -1);
    g.setColor(Color.black);
    g.drawString(text, 100, 100);

    }
    class MyMouseAdapter extends MouseAdapter{
    public void mouseClicked (MouseEvent e){
    if (e.mouseClicked);{
    Button c;
    c = new Button("YEAH!!");
    add(c);}
    }
    }

  2. #2
    Chris L. Guest

    Re: Can't make buttons work!!


    I use anonymous inner classes for my button functions. You could just place
    this in your MemoryApplet class(assuming button name is 'b'):


    b.addActionListener(new ActionListener()
    { public void actionPerformed(ActionEvent evt)
    {
    //button code here
    }
    }); //<--make sure you get that bracket

    Chris L.


    "superwebmonkey" <superwebmonkey@hotmail.com> wrote:
    >
    >So, I started writing my first applet...step by step.
    >Made text appear..cheanged the color.. made it ask the html for height

    and
    >width...added a button..
    >But now I'm stuck I can't make the button DO anything!
    >Please help.
    >Here is the unfinished code, I know the last part isn't correct...that's
    >where my problem is!!
    >
    >import java.applet.Applet;
    >import java.awt.*;
    >import java.awt.event.*;
    >public class MemoryApplet extends Applet{
    > String text="This is from Memory!!";
    > public void init(){
    > setBackground(Color.red);
    > text = "This is from Memory!!";
    > Button b;
    > b = new Button("Click!");
    > add(b);
    > }
    > public void paint(Graphics g)
    > {
    > g.setColor(Color.blue);
    > g.drawRect(0, 0,
    > getSize().height -1,
    > getSize().width -1);
    > g.setColor(Color.black);
    > g.drawString(text, 100, 100);
    >
    > }
    >class MyMouseAdapter extends MouseAdapter{
    > public void mouseClicked (MouseEvent e){
    > if (e.mouseClicked);{
    > Button c;
    > c = new Button("YEAH!!");
    > add(c);}
    > }
    > }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links