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

    Angry [SOLVED] Problems with JButtons and JFrame.EXIT_ON_CLOSE. Please Help!!!

    I hate to re-post this, but I really need an answer and I have an additional question that is related to the original

    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. I checked the strings and they are correct. Is there something fundamentally wrong in creating buttons in one class and using them in another class?

    My other problem is this: I have a program that is supposed to create two Containers (Windows on the screen)with ImageIcons in them. This worked, but when I close one window, the other closes at the same time. I am using the JFrame.EXIT_ON_CLOSE method, I am not sure if that is affecting it or not. This code involves one class embedded within 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{
    
    	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);
    	}
    }
    This is the code relating to my second question:
    Java Code:
    package testarea;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.ImageIcon;
    
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    public class Blockadepiece extends JFrame
    { 
    	private static final long serialVersionUID = 8446872171818934966L;
    		private Container contents;
    		private JLabel labelimage;
    		private JLabel labelimage2;
    		
    		public Blockadepiece( )
    		{
    			super("Is this a boat?");
    			contents = getContentPane();
    			
    			contents.setLayout(new FlowLayout());
    			
    			
    			//images
    			//Remember multiple ImageIcons!!!
    			ImageIcon ship = new ImageIcon( "C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/Frigate.PNG" );
    			ImageIcon ship2 = new ImageIcon ("C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/Shippy.PNG");
    			
    			
    			labelimage = new JLabel( "Yikes!!! CANNONS!",ship, JLabel.LEFT );
    			labelimage2 = new JLabel("Not more!",ship2, JLabel.LEFT);
    			
    			contents.add(labelimage);
    			contents.add(labelimage2);
    			
    			setSize(200,200);
    			setVisible(true);
    			setLocation (0,0);
    		}
    		
    		public class Blockadepiece2 extends JFrame
    		{
    		/**
    			 * 
    			 */
    			private static final long serialVersionUID = 1L;
    				private Container content2;
    				private JLabel image;
    				
    				public Blockadepiece2 ( )
    				{
    					super("Another one!!!");
    				
    					content2 = getContentPane();
    				
    					content2.setLayout(new FlowLayout());
    				
    				
    					//images
    					//Remember multiple ImageIcons!!!
    					ImageIcon ship3 = new ImageIcon( "C:/Documents and Settings/Brandon/My Documents/workspace/blockade/src/pics/Frigate.PNG" );
    					
    					
    				
    					image = new JLabel( "Yikes!!! CANNONS!",ship3, JLabel.LEFT );
    					
    				
    					content2.add(image);
    					
    					setSize(200,200);
    					setVisible(true);
    					setLocation (400,400);
    				}}
    		public static void main (String [] args)
    		{
    			Blockadepiece blockadepiece = new Blockadepiece();
    			blockadepiece.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		}	
    		
    		{	
    			Blockadepiece2 blockade = new Blockadepiece2 ();
    			blockade.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) ;
    		}
    }
    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!

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

    Default

    In your Movebuttontest code, which JButton are you changing the state of and adding an actionlistener to? the contained JButton field, arrows, or the object of the class -- the "this" object that subclasses JButton?

    In your Movescreentest class, which button are you adding to your GUI, the arrows field held by the Movebuttontest class or a Movebuttontest object itself?

    Figure this out and you'll at least answer some of your questions.

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

    Default

    You also should look at the tutorial on how to use layout managers as it will make setting up your GUI a whole lot easier. I'd also use some String constants in the code for your actionCommand strings.

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

    Default

    I'm not completely clear on what you mean, but I believe I am changing the state of arrows, and adding Movebuttontest objects. As for ActionListeners, I'm not completely sure how they work. I've looked, but cant quite get it, so I am just muddling through as well as I can with them.

    The idea was that Movebuttontest would create one JButton that I could duplicate to create 4 identical buttons using
    arrows1 = new Movebuttontest etc. and then put those inside a frame created by another class (Movescreentest).


    Also, I looked at layout managers and couldn't find one I liked and could use, and I am unfamiliar with String constants.
    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!

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

    Default

    There are two JButtons associated with each Movescreentest object. One is the Movescreentest object itself which inside of its class is the "this" object (remember that Movescreentest extends JButton, and so it is a JButton). The other JButton is the arrows field that the Movescreentest holds. So you must understand that these are two completely separate JButton objects, that changes to one will not have any affect on the other. This has nothing to do with Swing or GUI and everything to do with basic Java.

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

    Default

    Thanks. I completely forgot that Movebuttontest was a JButton itself. I just removed all references to arrows and got the results I wanted. Problem solved
    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. Replies: 1
    Last Post: 03-09-2009, 03:18 AM
  2. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  3. Problems with JFrame losing the Design view
    By chris@gaiag.net in forum NetBeans
    Replies: 7
    Last Post: 07-23-2008, 07:35 AM
  4. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM
  5. problems with jDialog in a JFrame
    By bbq in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 04:14 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
  •