View Full Version : Java programming help

10-21-2007, 07:07 AM
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.

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);

Image image2= new ImageIcon(getClass().getResource("cs.png"));
csJButton= new JButton("CS", image2);

Image image3= new ImageIcon(getClass().getResource("bungie.png"));
bungieJButton= new JButton("bungie", image3);

ButtonHandler handler= new ButtonHandler();

private class ButtonHandler implements ActionEvent
public void actionPerformed(ActionEvent event )
if(c.getSource()== haloJButton)
else if(c.getSource()== csJButton)


public static void main(String args[])
ImageWindow win=new ImageWindow();

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!!! :D

10-21-2007, 10:39 AM
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).

10-21-2007, 04:34 PM
Since you are trying to extend the JFrame you are going to want to do

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.