Question

I'm trying to create a JFrame with a background image, a JLabel over the background image centered and toward the bottom, with two buttons on the right and left that say "Stay" and "Leave". This is already created. The issue arises with the order of each of the items. I cannot get the JLabel with text and buttons over the background image, with both of them showing. Here is my code; any advice would be appreciated. Thank you in advance.

public class SceneOne {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        JFrame SceneOne = new JFrame();

        ImageIcon image = new ImageIcon(
                "C:/Users/alan/Downloads/scary_forest_2.jpg");
        JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(imageLabel, BorderLayout.CENTER);
        SceneOne.add(panel);
        SceneOne.setResizable(true);
        imageLabel.setVisible(true);
        SceneOne.pack();

        JButton Leave=new JButton("Leave");
        JButton Stay= new JButton ("Stay");
        JPanel Leave1= new JPanel(new FlowLayout());
        JPanel Stay1=new JPanel(new FlowLayout());
        FlowLayout two = new FlowLayout(FlowLayout.LEFT);
        FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
        Leave1.setLayout(two);
        Stay1.setLayout(three); 
        Stay1.add(Leave);
        Leave1.add(Stay);
        Leave1.setOpaque(false);
        Stay1.setOpaque(false);
        SceneOne.add(Leave1);
        SceneOne.add(Stay1);


        JLabel label1 = new JLabel("Test");
        SceneOne.add(label1);

        label1.setText("<html><font color='red'> It was approximately 11:30 pm. The night sky was black not a single star piercing through the darkness"
                + "except the thick and powerful moonlight."
                + "<br>"
                + "You are alone leaving a costume party at a friend's place."
                + "It was rather boring and you decided to leave early."
                + "A stutter is heard and your"
                + "<br>"
                + "car begins to shake"
                + "Your headlights and car lights crack. The engine is left dead silent."
                + "You are left in a total silence"
                + "and baked in merely the moonlight."
                + "<br>"
                + "There is a mere second of silence till a harsh chill ripes through the"
                + "car like a bullet through paper. You are left dumbfounded. What do you do?</font><html>");
        label1.setHorizontalAlignment(JLabel.CENTER);
        label1.setVerticalAlignment(JLabel.BOTTOM);
        label1.setVisible(true);
        label1.setOpaque(false);



      SceneOne.setComponentZOrder(panel, 0);
        SceneOne.setComponentZOrder(label1, 0);
     //  SceneOne.setComponentZOrder(Leave1,0);
      // SceneOne.setComponentZOrder(Stay1,0);



        SceneOne.setSize(400,320);
        SceneOne.setTitle("The Car");
        SceneOne.setVisible(true);
        SceneOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SceneOne.setLocation(500, 300);
    }

}
Was it helpful?

Solution

First of all follow Java naming conventions. Variable names should NOT start with an upper case character. I have never seen a tutorial, text book or forum example that uses variable names like you are using. Don't make up your own conventions!

The default layout manager for a JFrame is a BorderLayout. When you use the frame.add(...) method the component is added to the CENTER of the BorderLayout by default. Only on component can be added to the CENTER so only the last component is displayed.

If you want components to appear on top of the image, then you need to add the components to the image. The basic code would be:

JLabel label = new JLabel( new ImageIcon(...) );
frame.add(label);

label.setLayout(....);
label.add(leaveButton);
label.add(labelWithText);
label.add(stayButton);

I'll let you work out the exact layout that you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top