1

I am working on a menu that should come up with 2 buttons, "resume" and "Exit to main menu". The problem is that the JPanel is showing without any problems but the JButtons are not there, even though I have added them. The following part of code is the handling of the graphical side of the menu.

if(secMenuFlag){
        JPanel menu = new JPanel();
        JButton resume = new JButton("Resume"), exit = new JButton("Exit to Main Menu");
        menu.setLayout(null);

        menu.setLocation((frame.getWidth() - menuSize[0]) / 2, (frame.getHeight() - menuSize[1]) / 2);
        menu.setSize(menuSize[0], menuSize[1]);
        menu.setBackground(new Color(0, 0, 0));

        resume.addActionListener(this);
        resume.setFont(new Font("Sans-serif", Font.BOLD, 18));
        resume.setBackground(Color.white);
        resume.setLocation(100, 100);

        exit.addActionListener(this);
        exit.setFont(new Font("Sans-serif", Font.BOLD, 18));
        exit.setBackground(Color.white);
        exit.setLocation(200, 100);

        menu.add(resume);
        menu.add(exit);
        super.add(menu, 0);

    }
F.johansson
  • 83
  • 14
  • Did you try to play with button locations? – Maxim Shoustin Jul 30 '17 at 15:16
  • Yes, I have tried putting negative values on both x and y but nothing seems to happen :/ – F.johansson Jul 30 '17 at 15:21
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) `exit.setFont(new Font("Sans-serif", Font.BOLD, 18));` Better to use the constants, e.g. `exit.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));` – Andrew Thompson Jul 30 '17 at 15:21
  • 1
    .. 3) `menu.setLayout(null);` **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 Jul 30 '17 at 15:22

1 Answers1

2

set the bounds of the buttons. I have done for a resume, please follow the same procedure for the exit.

    JPanel menu = new JPanel();
    JButton resume = new JButton("Resume"), exit = new JButton("Exit to Main Menu");
    menu.setLayout(null);

    JFrame frame;
    frame = new JFrame("check");
    frame.setLayout(null);
    frame.setSize(300, 300);
    int[] menuSize = new int[2];
    menuSize[0] = 200;
    menuSize[1] = 300;

    menu.setLocation((frame.getWidth() - menuSize[0]) / 2, (frame.getHeight() - menuSize[1]) / 2);
    menu.setSize(menuSize[0], menuSize[1]);
    menu.setBackground(new Color(255, 255, 255));

    // resume.addActionListener((ActionListener) this);
    resume.setBounds(20, 20, 100, 100);
    resume.setFont(new Font("Sans-serif", Font.BOLD, 18));
    resume.setBackground(Color.BLACK);
    resume.setLocation(100, 100);

    resume.setVisible(true);

    // exit.addActionListener((ActionListener) this);
    exit.setFont(new Font("Sans-serif", Font.BOLD, 18));
    exit.setBackground(Color.BLACK);
    exit.setLocation(200, 100);
    exit.setVisible(true);

    menu.add(resume);
    menu.add(exit);
    frame.add(menu);
    frame.setVisible(true);