0
import java.awt.Color;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Ahh {
static boolean pass = false;

public static void PLAY(JFrame e, JFrame h, JFrame g, JFrame s) {
    JPanel p_play = new JPanel();

    p_play.setLayout(null);
    menu.setBounds(600, 620, 75, 25);
    e.setVisible(false);
    h.setVisible(false);
    g.setVisible(false);

    s.setVisible(true);
    p_play.add(menu);
    menu.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try {
                main(null);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });
    s.add(p_play);
}

public static void HELP(JFrame e, JFrame h, JFrame g, JFrame s) {
    JPanel p_help = new JPanel();

    p_help.setLayout(null);
    menu.setBounds(600, 620, 75, 25);
    e.setVisible(false);
    h.setVisible(true);
    g.setVisible(false);

    s.setVisible(false);
    p_help.add(menu);
    menu.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try {
                main(null);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });
    h.add(p_help);
}

public static void EXIT(JFrame e, JFrame h, JFrame g, JFrame s) {

    System.exit(0);

}

static JButton menu = new JButton("MENU");

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    JFrame.setDefaultLookAndFeelDecorated(true);

    JPanel p = new JPanel();
    JLabel l = new JLabel();

    JTextField text = new JTextField("(Enter Your Name)", 50);

    // frame
    JFrame game = new JFrame();
    JFrame start_frame = new JFrame();
    JFrame help_frame = new JFrame();
    JFrame exit_frame = new JFrame();

    // frame settings

    game.setSize(700, 700);
    game.setBackground(Color.gray);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setTitle("POINT");

    start_frame.setSize(700, 700);
    start_frame.setBackground(Color.YELLOW);
    start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    start_frame.setTitle("PLAY");

    help_frame.setSize(700, 700);
    help_frame.setBackground(Color.YELLOW);
    help_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    help_frame.setTitle("HELP");

    exit_frame.setSize(700, 700);
    exit_frame.setBackground(Color.YELLOW);
    exit_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    exit_frame.setTitle("EXIT");

    // buttons
    JButton b = new JButton("Enter");
    JButton start = new JButton("START");
    JButton help = new JButton("HELP");
    JButton exit = new JButton("EXIT");

    // placeing the buttons
    p.setLayout(null);
    start.setBounds(100, 500, 75, 25);// start button
    help.setBounds(300, 500, 75, 25);// help button
    exit.setBounds(500, 500, 75, 25);// exit button
    b.setBounds(280, 100, 100, 50);// enter name button
    text.setBounds(50, 10, 580, 20);// name input
    l.setBounds(50, 50, 580, 20);// name output
    p.setBounds(100, 100, 75, 25);

    p.add(text);
    // button funciton
    text.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String name = text.getText();

            name = "Welcome: " + name;
            l.setText(name);

        }

    });
    // button funciton
    p.add(b);
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String name = text.getText();

            name = "Welcome: " + name;
            l.setText(name);
        }
    });
    p.add(start);
    // button funciton
    start.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            PLAY(exit_frame, help_frame, game, start_frame);

        }
    });
    // button funciton
    p.add(help);
    help.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            HELP(exit_frame, help_frame, game, start_frame);
        }
    });
    // button funciton
    p.add(exit);
    exit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            EXIT(exit_frame, help_frame, game, start_frame);

        }
    });

    p.add(l);

    // pictures
    ImageIcon menu_pic = new ImageIcon("pics/Jolteon.jpg");
    JLabel m_pic = new JLabel(menu_pic);
    m_pic.setBounds(0, 0, 650, 650);
    game.getContentPane().add(m_pic);

    game.add(p);
    game.setVisible(true);

}

}

Hi I am currently making a menu for a school project right now and I am trying to add a picture for my background. However when I add the picture in it covers all my buttons and anything else in the frame. I want to be able to have the picture behind everything else. I put my code for my picture in the last few lines of code to make it easier for you too read.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • you should set the image as a background image instead of adding it like anther component. https://stackoverflow.com/questions/19125707/simplest-way-to-set-image-as-jpanel-background – guleryuz Jun 02 '18 at 05:32
  • `public static void PLAY(JFrame e, JFrame h, JFrame g, JFrame s) {` What the..? 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. But generally: 1) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) .. – Andrew Thompson Jun 02 '18 at 07:12
  • .. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (A problem of an image covering other GUI elements should only take about 30-50 lines of code, as opposed to the over 200 LOC seen above!) 3) 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). – Andrew Thompson Jun 02 '18 at 07:13
  • And I almost missed.. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 02 '18 at 07:18

0 Answers0