0

I am writing code for a Reverse Raffle in which once a button is pushed, that panel disappears. I want to put an Image behind the panels to slowly be revealed as the Panels are set to not Visible, but for some reason it isn't showing up, can anyone help? (Please excuse all the commented out code, I've done a lot of troubleshooting with this and I've yet to clean it all up yet.)

I've tried changing the frame.setContentPane to frame.add but that puts the image on top of the panels, and after the panels are clicked, the image goes away.

public static void createAndShowGUI(){


        JFrame frame = new JFrame("Raffle");
        RaffleBoard myDemo = new RaffleBoard();
        JLabel label = null;
        frame.setBackground(Color.BLUE);

        try {
            label = new JLabel(new ImageIcon(ImageIO.read(new File("logo.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        frame.add(label);
        label.setLocation(0,0);
        frame.pack();


        try {
          frame.setContentPane(myDemo.createContentPane());
        }
        catch(IOException e) {
          e.printStackTrace();
        }




        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(myDemo.getWidth(),myDemo.getHeight());
        frame.setVisible(true);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `frame.setContentPane` will replace the frames previous `contentPane` which contained the label, so, no, it won't show up. A generally better solution would be to create a "background" pane which could render the image and on to which other components can be added – MadProgrammer Mar 26 '19 at 20:42
  • See [How to set a background picture in JPanel](https://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430) for some more details – MadProgrammer Mar 26 '19 at 20:42
  • `frame.setSize(myDemo.getWidth(),myDemo.getHeight());` is some what erroneous, because until the component has undergone a layout pass, its size will be indeterminate. A better solution is simply to use `frame.pack()` and allow the layout manager to make decisions about how best to deal with it, of course, then you'll have issues with `setResizable` – MadProgrammer Mar 26 '19 at 20:44
  • Even so, If add the label to a new JPanel and change frame.setContentPane to frame.add, the image shows up on top of the other panels and disappears once their clicked. Idk if able to tell from what is given what the problem is or not, I'm still fairly new to java – tjzeiger Mar 26 '19 at 20:48
  • You seem to have a lack of understanding of how the layout management API works. Maybe start with [How to Use BorderLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) as that's the default layout manager used by `JFrame` – MadProgrammer Mar 26 '19 at 21:00
  • Okay i read through the 2 links you sent. I tried imitating the code in the first but it didn't seem to work. And I guess I fail to see how to the second affects why different parts of the GUI are visible and some are not. Sorry for being a pain i just want to make sure i understand why this is happening for future reference – tjzeiger Mar 27 '19 at 00:26
  • Components are add into a container hierarchy. When you use `setContentPane` you are replacing the "base" container hierarchy, so anything that was added to the original container hierarchy will no longer be displayed on the screen (it has no path to the native peer). `BorderLayout` managers only one component in each of its 5 available locations. When you add a new component to a given location, `BorderLayout` "forgets" about the previous component and will no longer provide layout support for it – MadProgrammer Mar 27 '19 at 01:05
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. .. – Andrew Thompson Mar 27 '19 at 05:08
  • .. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Mar 27 '19 at 05:08

0 Answers0