0

I am attempting to create a full screen JFrame which will then consist of three JPanels - a simple map of the world taking up about the upper 2/3 of the screen, and then the bottom 1/3 of the screen divided into two JPanels the width of the screen, one on top of the other.

Here is my class which holds the main function:

import javax.swing.*;

public class Risk extends JFrame {

    private static JFrame mainFrame;
    private static JPanel panel;
    private static int displayWidth, displayHeight, mapWidth, mapHeight;

    public static void main(String[] args) {

        DisplaySizeCalculator display = new DisplaySizeCalculator();
        displayWidth = display.getScreenWidth();
        displayHeight = display.getScreenHeight();

        mapWidth = displayWidth;
        mapHeight = (displayHeight - 200);
        mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //  mainFrame.setSize(displayWidth, displayHeight);
        mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        panel = new JPanel();
        panel.setLayout(new BoxLayout(mainFrame, BoxLayout.Y_AXIS));

        panel.add(new Map(mapWidth, mapHeight));

        panel.add(new JButton("Hi"));

        mainFrame.add(panel);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

}

I have two other classes - Map and DisplaySizeCalculator. Map is a class which extends JPanel and has a constructor taking a width and a height and also a paint method which draws the map based on this width and height. DisplaySizeCalculator is class which gets the dimensions of the screen on which the program is run.

As of now, my program outputs this.

I have tried many ways to add other components to the frame, all with no success. The best way I thought to achieve this was to create one JPanel to contain the other three. This panel would use a BoxLayout to add each component vertically. I would then add this panel to mainFrame. When I tried this I got an error saying BoxLayout can't be shared.

I have used many different combinations to add another JPanel but I get either a completely blank frame, or else just the second panel showing without the map, and many other outputs which are incorrect.

What would be the best solution for my problem? I am new to using Java UI tools and so I really am a bit lost with trying to figure this out.

Let me know if you need any more information from my code. Thank you

EDIT

I have edited my Risk class above to show me attempting to add the map and another component to a larger JPanel, and then adding that to mainFrame.

Here are the other classes as well as the error:

Map.java

DisplaySizeCalculator.java

Constants.java

Error

EDIT 2

I have just realised that, thanks to @HovercraftFullOfEels that I was passing mainFrame into the BoxLayout constructor rather than panel.

KOB
  • 4,084
  • 9
  • 44
  • 88
  • 1
    Can you show the full error and the code you got when you got the error? – Ferrybig Feb 14 '16 at 21:54
  • The error occurs when you pass the wrong container into the BoxLayout constructor. You must pass in the same JPanel that you're adding the BoxLayout to. In the future, please show the code showing the error and the full error message. Right now, you're not showing the most important code -- the BoxLayout code, and it boggles my mind to figure out why. – Hovercraft Full Of Eels Feb 14 '16 at 21:56
  • e.g., `JPanel myPanel = new JPanel();` nextline: `myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.PAGE_AXIS));` – Hovercraft Full Of Eels Feb 14 '16 at 21:58
  • [More similar questions on this site](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=boxlayout+can%27t+be+shared+site:http:%2F%2Fstackoverflow.com%2F) – Hovercraft Full Of Eels Feb 14 '16 at 22:00
  • @Ferrybig I added the rest of the code and the error to my post – KOB Feb 14 '16 at 22:04
  • @HovercraftFullOfEels I have added the rest of my code to the post, as well as editing it to show the BoxLayout code and the error. – KOB Feb 14 '16 at 22:05
  • Your problem is exactly as I've specified! You've got `panel.setLayout(new BoxLayout(mainFrame, BoxLayout.Y_AXIS));`, and that's not right. – Hovercraft Full Of Eels Feb 14 '16 at 22:23
  • `panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));` is what you should be doing as all similar questions/answers will tell you. – Hovercraft Full Of Eels Feb 14 '16 at 22:23
  • In the future, search on the error message as I will show you. Please click [this link](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=boxlayout+can%27t+be+shared+site:http:%2F%2Fstackoverflow.com%2F) to see one decent way to do this. This will give you solutions you need and save this site from needless duplication of questions. – Hovercraft Full Of Eels Feb 14 '16 at 22:24

0 Answers0