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:
EDIT 2
I have just realised that, thanks to @HovercraftFullOfEels that I was passing mainFrame into the BoxLayout constructor rather than panel.