Results 1 to 9 of 9
  1. #1
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default [SOLVED] Buttons in JFrame

    I have 3 applets like this one that have walkers. What I need help with is adding an action listener to the buttons. I have tried several times and keep getting this error. I did have oneWalkerButton.addActionListener(this);

    in java.awt.Button cannot be applied to walking

    What is supposed to happen is the person should be able to pick how many walkers they want. 1,2 or 3.

    Can someone please explain how to do this? I am new to java and I have been trying to figure this out for myself for a couple of weeks with no luck.

    Thank you for any guidance!


    Java Code:
    import java.applet.*;
       import java.awt.*;
       import java.awt.event.*;
       import java.util.*;
       import javax.swing.*;
    
    
        public class Walking extends Applet
        		   
       {
          private int count = 0;
          private int sleep = 100;
          private int startx = 45,oldx = 42;
           public void start()
          {
             count = 0;
             startx = 45;
          }
           public void update(Graphics gr)
          {
             paint(gr);
          }
           public void paint(Graphics gr)
          {
             if((count%2)==0)
             {
                gr.setColor(getBackground());
                gr.drawOval(oldx - 15,30,45,45); 
                gr.drawLine(oldx,75,oldx,160); 
                gr.drawLine(oldx,160,oldx+5,160); 
                gr.drawLine(oldx+5,110,oldx+10, 155); 
                gr.drawLine(oldx+10,155,oldx+20, 155); 
                gr.drawLine(oldx,110,oldx+20,140);
                gr.drawLine(oldx+20,140,oldx+30,140); 
                gr.setColor(getForeground());
                gr.drawOval(startx - 15,30,45,45); 
                gr.drawLine(startx,75,startx,110); 
                gr.drawLine(startx,110,startx,160);
                gr.drawLine(startx,160,startx+5,160); 
                gr.drawLine(startx+5,110,startx+10,155);
                gr.drawLine(startx+ 10,155,startx + 20,155); 
                gr.drawLine(0, 165,300,165); 
                
             
             }
             else
             {
                gr.setColor(getForeground());
                gr.drawLine(startx,110,startx+20,140);
                gr.drawLine(startx+20,140,startx+30,140);
                gr.setColor(getBackground());
                gr.drawLine(startx+5,110,startx+10, 155);
                gr.drawLine(startx+10,155,startx+20, 155);
                oldx = startx;
                startx +=3;
             }
             ++count;
             if(startx>180 ) 
                startx = 45;
             try
             {
                Thread.sleep(sleep);
             }
                 catch(InterruptedException e)
                {
                   showStatus(e.toString());
                }
             repaint();
          }
       
          Button oneWalkerButton;
          Button twoWalkersButton;
          Button threeWalkersButton;
        
           public void init() 
          { 
             setLayout(null); 
             oneWalkerButton = new Button("One walker"); 
             twoWalkersButton = new Button ("Two walkers"); 
             threeWalkersButton = new Button("Three walkers"); 
          
          
             oneWalkerButton.setBounds(0,180,75,20);
             add(oneWalkerButton); 
             twoWalkersButton.setBounds(0,210,75,20);
             add(twoWalkersButton); 
             threeWalkersButton.setBounds(0,240,85,20);
             add(threeWalkersButton); 
           
          
           
          }
           public void actionPerformed(ActionEvent e)
          {
          }
       }
    Last edited by Nemo1959; 09-15-2008 at 01:13 AM. Reason: Correction for Title of post

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Buttons in JFrame
    What does this have to do with JFrame?

  3. #3
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Sorry it is an applet. I had another problem with a Jframe. My mistake.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    See Lesson: Writing Event Listeners for info.
    Java Code:
    //  <applet code="WalkingRx" width="400" height="400"></applet>
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class WalkingRx extends Applet implements ActionListener
    {
        private int count = 0;
        private int sleep = 100;
        private int startx = 45, oldx = 42;
    
        public void start()
        {
            count = 0;
            startx = 45;
        }
    
        public void update(Graphics gr)
        {
            paint(gr);
        }
    
        public void paint(Graphics gr)
        {
            if((count%2)==0)
            {
                gr.setColor(getBackground());
                gr.drawOval(oldx - 15,30,45,45);
                gr.drawLine(oldx,75,oldx,160);
                gr.drawLine(oldx,160,oldx+5,160);
                gr.drawLine(oldx+5,110,oldx+10, 155);
                gr.drawLine(oldx+10,155,oldx+20, 155);
                gr.drawLine(oldx,110,oldx+20,140);
                gr.drawLine(oldx+20,140,oldx+30,140);
                gr.setColor(getForeground());
                gr.drawOval(startx - 15,30,45,45);
                gr.drawLine(startx,75,startx,110);
                gr.drawLine(startx,110,startx,160);
                gr.drawLine(startx,160,startx+5,160);
                gr.drawLine(startx+5,110,startx+10,155);
                gr.drawLine(startx+ 10,155,startx + 20,155);
                gr.drawLine(0, 165,300,165);
            }
            else
            {
                gr.setColor(getForeground());
                gr.drawLine(startx,110,startx+20,140);
                gr.drawLine(startx+20,140,startx+30,140);
                gr.setColor(getBackground());
                gr.drawLine(startx+5,110,startx+10, 155);
                gr.drawLine(startx+10,155,startx+20, 155);
                oldx = startx;
                startx +=3;
            }
            ++count;
            if(startx>180)
                startx = 45;
            try
            {
                Thread.sleep(sleep);
            }
            catch(InterruptedException e)
            {
                showStatus(e.toString());
            }
            repaint();
        }
    
        Button oneWalkerButton;
        Button twoWalkersButton;
        Button threeWalkersButton;
    
        public void init()
        {
            setLayout(null);
            oneWalkerButton = new Button("One walker");
            twoWalkersButton = new Button ("Two walkers");
            threeWalkersButton = new Button("Three walkers");
    
            oneWalkerButton.setBounds(0,180,75,20);
            add(oneWalkerButton);
            twoWalkersButton.setBounds(0,210,75,20);
            add(twoWalkersButton);
            threeWalkersButton.setBounds(0,240,85,20);
            add(threeWalkersButton);
    
            oneWalkerButton.addActionListener(this);
            twoWalkersButton.addActionListener(this);
            threeWalkersButton.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            Button button = (Button)e.getSource();
            if(button == oneWalkerButton)
            {
                System.out.println("oneWalkerButton");
            }
            if(button == twoWalkersButton)
            {
            }
        }
    }

  5. #5
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Question Buttons on Applet

    I added this to my code
    Java Code:
     [B]  public class Walking extends Applet implements ActionListener    	[/B]	   
       {

    and also this :
    Java Code:
      [B]oneWalkerButton.addActionListener(this);
             
             twoWalkersButton.addActionListener(this);
             threeWalkersButton.addActionListener(this);
    [/B]
    Can someone please advise my how I bring up the differnt applets using the three different buttons? The other 2 applets are named walker2 and walker3.

    Button one should bring up one walker, button2 should bring up 2 and button 3 should bring up all 3. I am new to java and I am having a hard time with this.

    I inadvertently posted the title as JFRAME it's not a JFrame but Applets

  6. #6
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    The part I need help on is what I need to put in here
    Java Code:
        public void actionPerformed(ActionEvent e)
          {
    Last edited by Nemo1959; 09-15-2008 at 02:42 PM. Reason: code tag correction

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default

    what I need to put in here
    Put there the code that you want executed when the action event occurs.
    If the code is going to take too long or is going to change the GUI then you should use a thread to do the work and just start the thread in the actionPerformed method.

  8. #8
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Let me see if I understand you. Basically on that part I would put the button source and repost the code for each walker/walkers under that button? So if there are two walkers I would have to have the code for walking and walker 2 under that button.

    "If the code is going to take too long or is going to change the GUI then you should use a thread to do the work and just start the thread in the actionPerformed method."

    What exactly do you mean by that? Could you explain it in dummy terms?

    Forgive me but this book by Joyce Farrell that I have to use stinks and doesn't explain a lot of this very well.

    Thanks for assistance so far! :)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default

    A long time in GUI terms would be a task that took longer than the user of the GUI would want to wait before he could tell the program to do something else or to tell it to quit doing what it was doing. For example if your code were to start copying all of the files from the C: drive to a CD, you would want to let the GUI get control so that the user could STOP the copy if he wanted. If the copy were being done on the GUI thread (not on its own thread) the GUI would NOT be able to be responsive to the user. Don't worry too much about this now. But do remember it as it WILL happen to you and you'll wonder, "Why doesn't the GUI respond to me?". The answer will be, because its thread is tied up by the code.

    code for each walker/walkers
    What is a walker in java programming terms?

Similar Threads

  1. Functionality of Buttons
    By ljk8950 in forum AWT / Swing
    Replies: 6
    Last Post: 08-15-2008, 01:44 PM
  2. Demonstration of Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  3. How to use SWT Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  4. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 01:23 AM
  5. Next, Finish Buttons !!!
    By pele in forum SWT / JFace
    Replies: 1
    Last Post: 07-14-2007, 05:22 PM

Posting Permissions

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