0

I have loaded a image into a JLabel but when I add the label to the container it fills the whole JFrame which is good but the other 2 Buttons don't go on top of the image and when I click where the buttons are meant to be, nothing happens so it is like the buttons aren't there even though it is meant to add them. All you can see is the image.

Here is my code below:

public  void add(Container pane){

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }
    URL resource = getClass().getResource("Graphitebackground.v2.jpg");


      ImageIcon i2 = new ImageIcon(resource);  
   JLabel label = new JLabel(i2);
    label.setPreferredSize(new Dimension(1000,1000));
    label.setVisible(true);
    pane.add(label);
    JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}

JLabel label2 = new JLabel("");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;

pane.add(label2, c);

JLabel Label3 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;

pane.add(Label3, c);

JLabel Label4 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;

pane.add(Label4, c);

JButton b1 = new JButton("Yes");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
pane.add(b1, c);
    b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            try {
                Command();
            } catch (AWTException ex) {

            } catch (InterruptedException ex) {

            }
        }
    });

    JButton b2= new JButton("No");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;      //make this component tall
c.weightx = 0.0;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 2;
pane.add(b2, c);
    b2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            for (Window window : Window.getWindows()) {
                window.dispose();
            }

        }
    });


JLabel button1 = new JLabel("");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;       //reset to default
c.weighty = 1.0;   //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0);  //top padding
c.gridx = 1;       //aligned with button 2
c.gridwidth = 1;   //2 columns wide
c.gridy = 2;
   //third row
pane.add(button1, c);

}



public  void Start1(){











   JFrame frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    //Set up the content pane.
    add(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true); 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

    frame.setVisible(true);

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cufe
  • 18
  • 6
  • 1) For better help sooner, post 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) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! .. – Andrew Thompson Aug 18 '18 at 12:48
  • .. 4) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Aug 18 '18 at 12:49

1 Answers1

1
ImageIcon i2 = new ImageIcon(resource);  
JLabel label = new JLabel(i2);
label.setPreferredSize(new Dimension(1000,1000));
label.setVisible(true);
pane.add(label);
...
pane.setLayout(new GridBagLayout());

Comments about the above code:

  1. Don't set the preferred size. Each component will determine its own preferred size based on the properties of the component. In this case the preferred size will be the size of the image.

  2. No need for setVisible(true). Swing components are visible by default (except top level containers like JFrame, JDialog...).

  3. Don't try adding component to a panel before you set the layout manager. It may work for some layout managers but it is a bad habit and can cause problems.

when I add the label to the container it fills the whole JFrame which is good but the other 2 Buttons don't go on top of the image

Swing actually paints the components in the reverse order they are added to the panel. So because the label is the first added, it is the last painted and it paints on top of the buttons.

The problem is you need to change the hierarchy of your components. It should be:

  • frame
    • background image (label)
      • button 1
      • button 2

So your logic should be something like:

JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
background.add(button1, constraint...);
background.add(button2, constraint...);
frame.add(background);

Note, a JLabel is not a true container so the buttons must fit in the size of the image or they will not display properly.

For a true background image you should paint the image on a JPanel and then add the buttons to the panel. See Background Panel for an example of this approach.

camickr
  • 321,443
  • 19
  • 166
  • 288