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