Results 1 to 2 of 2
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default [SOLVED] I'm having problems using JButtons with ImageIcons and Actions. Any suggest

    I am trying to create a JFrame that has four JButtons, and these JButtons are all supposed to cycle through the same four images. I created the JButtons in one class, and the JFrame and JPanel in a separate class. When I try to run the application, I get four JButtons in a window, but the buttons do not do anything. Is there something fundamentally wrong in creating buttons in one class and using them in another class?

    Also, is there is an easier way to create ImageIcons than typing the entire string starting at the hard drive? I can't seem to find out how.

    e.g("/image.PNG) instead of ("C:/Documents and Settings/Username/My Documents/workspace/blockade/src/pics/image.PNG")"

    This is my code to create the JButtons.

    Java Code:
    package testarea;
    
    
    import pics.*;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
    @SuppressWarnings("serial")
    public class Movebuttontest extends JButton implements ActionListener{
    
    
    	public ImageIcon up, right, left,notmoving;
    	JButton arrows;
    	
    	public Movebuttontest()
    
    {
    	up = new ImageIcon("C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/Forward Arrow.PNG");
    	right = new ImageIcon("C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/Right Arrow.PNG");
    	left = new ImageIcon("C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/Left Arrow.PNG");
    	notmoving = new ImageIcon("C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/No Move.PNG");
    	
    	
    	
    	arrows = new JButton (notmoving);
    
    	arrows.setActionCommand("notmoving");
    	
    	arrows.setSize(50, 50);
    	
    	arrows.addActionListener(this);
    	
    
    }
    	
        public void actionPerformed(ActionEvent e) {
            if ("notmoving".equals(e.getActionCommand())) {
                arrows.setIcon (left);
                arrows.setActionCommand("left");
      
             
            
                if ("left".equals(e.getActionCommand())) {
                    arrows.setIcon (up);
                    arrows.setActionCommand("up");
            
            
            	if ("up".equals(e.getActionCommand())) {
                    arrows.setIcon (right);
                    arrows.setActionCommand("right");
            
            
            	if ("right".equals(e.getActionCommand())) {
                    arrows.setIcon (up);
                    arrows.setActionCommand("left");
            	
            	}
            }
        }
    }
    }
    }


    This is my code to create the JFrame, JPanel, and implement the JButtons.

    Java Code:
    package testarea;
    
    import testarea.Movebuttontest;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Movescreentest extends Movebuttontest{
    
    
    
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		Movebuttontest arrows1 = new Movebuttontest();
    		Movebuttontest arrows2 = new Movebuttontest();
    		Movebuttontest arrows3 = new Movebuttontest();
    		Movebuttontest arrows4 = new Movebuttontest();
    	
    		arrows1.setBounds(50,20,50,50);
    		arrows2.setBounds(50,90,50,50);
    		arrows3.setBounds(50,160,50,50);
    		arrows4.setBounds(50, 230,50,50);
    		
    		JPanel mypanel = new JPanel();
    		JFrame myframe = new JFrame();
    		
    		mypanel.setLayout(null);
    		
    		mypanel.add(arrows1);
    		mypanel.add(arrows2);
    		mypanel.add(arrows3);
    		mypanel.add(arrows4);
    		
    		myframe.setContentPane(mypanel);
    		myframe.setSize(150,350);
    		myframe.setVisible(true);
    	}
    
    }

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Solved at different thread

    Solved this in a different thread;)

    unfortunately I cant post links until I have 20 posts so you'll have to look for the post. This is the thread name:
    Problems with JButtons and JFrame.EXIT_ON_CLOSE. Please Help!!!

    Just search for it. :)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  2. Problems with ImageIcons
    By The_L in forum AWT / Swing
    Replies: 2
    Last Post: 10-24-2008, 02:04 AM
  3. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  4. Actions
    By alley in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 02:52 PM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Posting Permissions

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