1

My image was displaying properly before I had a JButton on top of it. Now that I have added a JButton to my code, my image does not display. In the ActionPerformed method I am telling the button to setVisbible(false). When I click the button, it disapears and all that is behind it is the background.

public class Main extends JFrame implements ActionListener {

public static void main(String[] args) {
    Main main = new Main();

}

ImageIcon GIF = new ImageIcon("src/Zombie Steve gif.gif");
JButton button = new JButton("Click me!");
JLabel Label = new JLabel(GIF);

public Main() {

    button.addActionListener(this);

    Label.setHorizontalAlignment(0);

    JFrame Frame = new JFrame("zombieSteveGIF");
    Frame.setSize(650, 650);
    Frame.setVisible(true);
    Frame.add(Label);
    Frame.add(button);
    Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    while (true) {
        Frame.getContentPane().setBackground(Color.BLUE);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Frame.getContentPane().setBackground(Color.GREEN);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Frame.getContentPane().setBackground(Color.RED);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

public void actionPerformed(ActionEvent e) {
    button.setVisible(false);

}

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chris
  • 11
  • 2
  • Did you try to set `opaque(false)`? for button – Maxim Shoustin Jul 28 '13 at 08:40
  • @MaximShoustin I tried it, unfortunately still will not show my image, only the background. – Chris Jul 28 '13 at 08:43
  • 2
    You can't have two different components at the center location of a BorderLayout. Read http://stackoverflow.com/questions/1466240/how-to-set-an-image-as-a-background-for-frame-in-swing-gui-of-java – JB Nizet Jul 28 '13 at 08:46
  • @JBNizet Thank you! I've found where I went wrong and now I have it working :) – Chris Jul 28 '13 at 08:52
  • It is encouraged to answer your own questions. – Bartek Banachewicz Jul 28 '13 at 08:57
  • Note that swing is not thread safe. You should not access swing components outside the event dispatch thread. See: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html .Doing it properly means that you need to move the delay loops out of EDT. Swing [Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) are ideal for your situation. – kiheru Jul 28 '13 at 09:02
  • 1) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. 2) Don't set the size of top level containers. Instead layout the content & call `pack()`. .. – Andrew Thompson Jul 28 '13 at 15:08
  • 3) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 4) By time of deployment, those resources will likely be an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form `URL`. – Andrew Thompson Jul 28 '13 at 15:09

1 Answers1

3

Your problem is that you have a BorderLayout (the default for JFrames), and you are adding two components in the same position. The default is BorderLayout.CENTER, and by adding two components with just the default constraints, the first one is removed and the second put in its place.

As for fixing your problem, what do you want to achieve? If you want the components to show on top of one another, you can use the OverlayLayout. If you don't want this, try some other layout manager.

tbodt
  • 16,609
  • 6
  • 58
  • 83