Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java programming help

    Hi, I am doing this assignment for class where I need to set the background to a specific image each time you click on the associated button. I have it set up when the program is executed a JFrame pops up and three buttons are there. I want to be able to click on the button, then the background is set to the image associated with that button. However, when you click on any other button the background is repainted with another image.

    Code:
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.*;
    import java.awt.event.*;
    import java.swing.event.*;
    
    public class ImageWindow extends JFrame
    {
       private JButton haloJButton;
       private JButton csJButton;
       private JButton bungieJButton;
       private Container c;
       
       
       public ButtonFrame() // Line 18
       {
          super("Image Window");
    	  setLayout(new FlowLayout());
    	  
    	  Image image1= new ImageIcon(getClass().getResource("halo.png"));
    	  haloJButton= new JButton("Halo", image1);
    	  add(haloJButton);
    	  
    	  Image image2= new ImageIcon(getClass().getResource("cs.png"));
    	  csJButton= new JButton("CS", image2);
    	  add(csJButton);
    	  
    	  Image image3= new ImageIcon(getClass().getResource("bungie.png"));
    	  bungieJButton= new JButton("bungie", image3);
    	  add(bungieJButton);
    	  
    	  ButtonHandler handler= new ButtonHandler();
    	  haloJButtton.addActionListener(handler);
    	  csJButtton.addActionListener(handler);
    	  bungieJButtton.addActionListener(handler);
    	}
    	
    	private class ButtonHandler implements ActionEvent 
    	{
    	  public void actionPerformed(ActionEvent event )
    	  {
    	    if(c.getSource()== haloJButton)
    		   c.setBackground("halo.png");
    		else if(c.getSource()== csJButton)
    		   c.setBackground("cs.png");
    		else
    		   c.setBackground("bungie.png");
    		
    	   repaint();
    	   }
    	}
       
       public static void main(String args[])
       {
       ImageWindow win=new ImageWindow();
       win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    }
    For some reason on line 18, which i commented in, I keep getting an invalid method declaration, because the return type is incorrect. I am not sure what the problem is. Any and all help is very appreciated!!!

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    What is ButtonFrame? A class or a method?

    I'm guessing that it's suppose to be the constructor for you ImageWindow class, in which case it should be named as such. public ImageWindow()

    If it's suppose to be a method then it needs a return type. In this case I'd suspect you'd want public void ButtonFrame()

    There's two possible solutions for you. Based on what you're trying to do you'll have to decided which is appropriate (or neither if I'm totally of base here).
    Last edited by Gox; 10-21-2007 at 10:44 AM.

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Since you are trying to extend the JFrame you are going to want to do

    Code:
    public ImageWindow()
    {
    That is because it is your constructor to the class ImageWindow. Because of that, it must be named the same as your class name.


    In other news, there is a Java forum where you can post questions like this under Java / JSP. Please in the future, do so as that is where most of us Java folks wander.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

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