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

    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;
    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.setSize(50, 50);
        public void actionPerformed(ActionEvent e) {
            if ("notmoving".equals(e.getActionCommand())) {
                arrows.setIcon (left);
                if ("left".equals(e.getActionCommand())) {
                    arrows.setIcon (up);
            	if ("up".equals(e.getActionCommand())) {
                    arrows.setIcon (right);
            	if ("right".equals(e.getActionCommand())) {
                    arrows.setIcon (up);

    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();
    		arrows4.setBounds(50, 230,50,50);
    		JPanel mypanel = new JPanel();
    		JFrame myframe = new JFrame();

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

    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