Results 1 to 2 of 2
  1. #1
    Deathmonger is offline Member
    Join Date
    Feb 2008
    Posts
    16
    Rep Power
    0

    Default Issue with Buttons and ActionPerformed

    Hi, I'm sure this is a simple fix, but I don't know what to use to fix it. I am playing around with a program that allows the user to interact with an object. The program I'm starting allows the user to choose a direction, and move a square 5 pixels. However, I can't seem to get it to work. When you click on the "Up" button, the square is supposed to move 5 pixels up, and when you click on "Left," the square moves 5 pixels left, and so on...

    Here is the code I created so far, and when I click on one of these buttons, the square doesn't do anything. I'm sure it's a simple fix, I just don't know what it is.

    Any help is greatly appreciated. Thanks

    Code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class InteractionX {
    	
    	static JFrame frame;
    	static Container contain;
    	static JButton up;
    	static JButton down;
    	static JButton left;
    	static JButton right;
    	static Panel controls;
    	static Panel canvasArea;
    	
    	static int x = 75;
    	static int y = 75;
    	
    	static class InteractXCanvas extends Canvas {
    		
    		public void paint(Graphics g) {
    			g.fillRect(x,y,10,10);
    		}
    		
    	}
    	
    	static class InteractXMovement implements ActionListener {
    		InteractXCanvas IXC = new InteractXCanvas();
    		
    		public void actionPerformed(ActionEvent ae) {
    			String whichButton;
    			whichButton = ae.getActionCommand();
    			int x2 = 0;
    			int y2 = 0;
    			
    			if(whichButton.equals("Up")) {
    				y = y + 5;
    				
    			}
    			if(whichButton.equals("Down")) {
    				y = y-5;
    				
    			}
    			if(whichButton.equals("Left")) {
    				x = x-5;
    				
    			}
    			if(whichButton.equals("Right")) {
    				x = x+5;
    				
    			}
    			
    			IXC.repaint();
    			
    		}
    	}
    	
    	
    	
    	public static void main(String[] args) {
    		InteractXCanvas IXC = new InteractXCanvas();
    		InteractXMovement IXM = new InteractXMovement();
    		
    		frame = new JFrame("Interaction World");
    		contain = new Container();
    		up = new JButton("Up");
    		down = new JButton("Down");
    		left = new JButton("Left");
    		right = new JButton("Right");
    		controls = new Panel();
    		canvasArea = new Panel();
    		
    		up.addActionListener(IXM);
    		down.addActionListener(IXM);
    		left.addActionListener(IXM);
    		right.addActionListener(IXM);
    		
    		frame.setSize(500,500);
    		IXC.setSize(100,100);
    		contain = frame.getContentPane();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		contain.setLayout(new GridLayout(2,3));
    		
    		controls.add(up);
    		controls.add(down);
    		controls.add(left);
    		controls.add(right);
    		canvasArea.add(IXC);
    		
    		frame.setVisible(true);
    		
    		
    	}
    	
    	
    }
    Jai guru deva om, Nothing's gonna change my world

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

Similar Threads

  1. GUI buttons question...
    By glenrowan in forum New To Java
    Replies: 3
    Last Post: 01-18-2011, 05:23 PM
  2. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 04:02 AM
  3. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 06:10 PM
  4. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 04:19 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
  •