0

I have a problem I would like to program a menu for a game console in Java, I use several JPanel and I change with frame.setContentPane (newMenu); between the JPanel now I would like that if I enlarge the JFrame that the JPanel enlarges so that the JPanel in the resolution 800x600 remains but the JFrame is for example in the resolution 1920x1080. I want to have it like RetroGames have black stripes left and right and no black stripes at the top and bottom.

Code: JFrame

    frame = new JFrame(CGC_NAME + " [Version: " + CGC_VERSION + "]");
    frame.setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    frame.setLayout(null);
    displayPanel = new JPanel();
    displayPanel.setBounds(0,0, Menu.MENU_WIDTH, Menu.MENU_HEIGHT);
    frame.add(displayPanel);

    Menu.changeMenu(null, Menu.homeMenu);

    frame.setFocusable(true);
    frame.requestFocus();
    frame.addKeyListener(new Keyboard());

Code JPanel

    @Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    renderMenu(g);

    repaint();
}

protected abstract void runMenu();  
protected abstract void renderMenu(Graphics g);

public static void changeMenu(Menu oldMenu, Menu newMenu) {
    //CGC.frame.setContentPane(newMenu);

    if (oldMenu != null) {
        CGC.displayPanel.remove(oldMenu);
    }

    CGC.displayPanel.setLayout(new BorderLayout());
    CGC.displayPanel.add(newMenu, BorderLayout.CENTER);
    CGC.frame.revalidate();
    CGC.frame.repaint();
    newMenu.runMenu();
}

Can someone help me? Cortablo

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Welcome to SO. Don't use null layout. Remove `frame.setLayout(null); and `displayPanel.setBounds(0,0, Menu.MENU_WIDTH, Menu.MENU_HEIGHT);` . Use a [layout manger](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), for example `setLayout(new BorderLayout());` – c0der Jun 02 '18 at 13:00
  • how i use the BorderLayout? – Cortablo Jun 02 '18 at 13:06
  • 1
    [A Visual Guide to Layout Managers](https://stackoverflow.com/users/3992939/c0der) – c0der Jun 02 '18 at 13:47
  • Please check [this useful Google search](https://www.google.com/search?q=java+jpanel+resize+with+jframe+site:stackoverflow.com) for when you have future similar questions. – Hovercraft Full Of Eels Jun 02 '18 at 13:56
  • But there is not what I need, I just want to place a 4: 3 JPanel centered on a 16: 9 frame – Cortablo Jun 02 '18 at 14:03
  • *"But there is not what I need"* Who are you replying to? Tip: Add @c0der (or whoever, the `@` is important) to *notify* the person of a new comment. General tips: 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also [this Q&A](https://stackoverflow.com/q/21142686/418556) for a section of the GUI (the chess board itself) which fills the available space while maintaining a constant aspect ratio (in that case, it's simply 1:1 - square). – Andrew Thompson Jun 02 '18 at 14:28
  • @Cortablo Look at the "visual guide" linked earlier. You should never `setLayout(null)`. Instead use a layout manager that will resize all components as necessary. – Code-Apprentice Jun 02 '18 at 14:29
  • @Code-Apprentice Which LayoutManager do I take best and how do I implement it? Sorry if I have to ask a bit I am Java beginner I hardly know it. – Cortablo Jun 02 '18 at 15:44
  • @Cortablo That's for you to figure out. The link which c0der gave above shows you all of the standard options. As a beginner the best thing you can do is read, read, read, and read some more. – Code-Apprentice Jun 02 '18 at 15:47
  • *"Which LayoutManager do I take best and how do I implement it?"* **No** standard (inbuilt to Java) layout manager will cause a component to be a **fixed aspect ratio.** It requires the help of the component itself (in providing a preferred size that is the correct aspect ratio) as well as the layout / constraint of the parent component (to not stretch the component in either width or height). – Andrew Thompson Jun 02 '18 at 16:51

0 Answers0