Currently, the problem I am trying to solve is how I get both my image and button to show up.
When I have the following line in the code the image shows up but when I remove it my image doesn't display but the button does:
setLayout (new FlowLayout()) ;
Images for example ^
import java.awt.*;
public class Panel extends JFrame {
private ImageIcon FirstPageImage;
private JLabel FirstPageLabel;
private JLayeredPane SignupButtonLayer;
private JButton Button;
public Panel(){
setLayout (new FlowLayout()) ;
FirstPageImage = new ImageIcon(getClass().getResource("FirstPageAnimationUsing.gif"));
FirstPageLabel = new JLabel(FirstPageImage);
FirstPageImage.setImage(FirstPageImage.getImage().getScaledInstance(343,820,Image.SCALE_DEFAULT));
add(FirstPageLabel);
Button = new JButton();
SignupButtonLayer = new JLayeredPane();
Button.setOpaque(true);
Button.setBackground(Color.cyan);
Button.setBounds(94,617,159,82);
SignupButtonLayer.add(Button, JLayeredPane.DEFAULT_LAYER);
add(SignupButtonLayer);
}
public static void main(String[] args) {
Panel gui = new Panel();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.pack();
gui.setTitle("Reminder App");
gui.setSize(360,850);
}
}
Refer to How to Use Layered Panes.
JLayeredPanea preferred size. Since yourJLayeredPanecontains only a singleJButton, that size should be big enough to display the entireJButton.setBounds– that you call onButton– are relative to its container, i.e.SignupButtonLayer. Setting the x to 94 and the y to 617 means thatButtonis placed outside of the bounds ofSignupButtonLayer. Hence you don't see it. In the below code, I set x and y both to 0 (zero) so that the top, left corner ofButtonaligns with the top, left corner ofSignupButtonLayer.setOpaque(true)forButtonsince that is the default, anyway.pack()– which is usually preferred – orsetSize()but don't call both.setVisible(true)should be called only once your GUI is completely built. In the below code I call it after callingpack()andsetTitle().The below code simply resolves your problem, i.e. displaying both the image and the button together – while using
FlowLayoutfor the [content pane of the]JFrame. Notice that the preferred size ofSignupButtonLayeris slightly larger than the size arguments in methodsetBounds.