Reputation: 1
So I'm trying to make my first small java game in my own spare time. I am having trouble with the layout.
I want the game to be a specific size (600 height, 800 width) and I would like 3 "Panels" within the main frame. One which is the main game frame and would be 500 height and 600 width, an inventory/info panel on the right which should have 500 height and 200 width and finally a text panel at the bottom to hold information which would have a height of 100 and a width of 800. So far, here is what I have. (I didn't play around with the panel height as I found nothing changed).
How would I go about making a frame with those 3 panels inside it. I had a look on how to use GridBagLayout() but it seems I have made it worse and do not fully understand how to use it, even with the documentation (yes, im stupid).
LMK if you don't understand parts of my code or my post for that matter. Thank you.
package Frame;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class frame {
//Sets variables such as height, width and title
private JFrame frame;
private Canvas canvas;
private JPanel mainWindow, infoWindow, textWindow;
//main constructor to create the frame of the game. Is called in Launcher
//Sets the parameters of the frame. User defined in main.
public frame(){
createDisplay();
}
//sets the properties of the display
private void createDisplay() {
frame = new JFrame();
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.setTitle("Island Man");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
canvas = new Canvas();
canvas.setSize(new Dimension(800, 600));
canvas.setMaximumSize(new Dimension(800, 600));
canvas.setMinimumSize(new Dimension(800, 600));
mainWindow = new JPanel();
mainWindow.setBackground(Color.CYAN);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 2;
c.weighty = 2;
frame.add(mainWindow, c);
infoWindow = new JPanel();
infoWindow.setBackground(Color.GREEN);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
c.weightx = 0;
c.weighty = 2;
frame.add(infoWindow, c);
textWindow = new JPanel();
textWindow.setBackground(Color.MAGENTA);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
c.weightx = 0;
c.weighty = 3;
frame.add(textWindow, c);
frame.add(canvas);
frame.pack();
}
}
Upvotes: 0
Views: 1452
Reputation: 17454
How do I make a frame that has these 3 panels with those dimensions?
There are too many ways to do it. I most direct ways would be setting the size accordingly for each panel and add them to the main panel, then add the main panel to the frame:
The exact Layout to use is dependent on..
The following shows one possible way by making use of the FlowLayout.
class MainPanel extends JPanel
{
public MainPanel(){
setPreferredSize(new Dimension(800, 600));
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
add(new GamePanel());
add(new InventoryPanel());
add(new TextPanel());
}
}
class GamePanel extends Panel
{
public GamePanel(){
setPreferredSize(new Dimension(500, 600));
setBackground(Color.ORANGE);
}
}
class InventoryPanel extends Panel
{
public InventoryPanel(){
setPreferredSize(new Dimension(200, 600));
setBackground(Color.YELLOW);
}
}
class TextPanel extends Panel
{
public TextPanel(){
setPreferredSize(new Dimension(100, 600));
setBackground(Color.CYAN);
}
}
Adding the main panel to the frame:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
Upvotes: 1
Reputation: 4084
One way is to use a BorderLayout with the main panel in CENTER, the inventory panel in EAST, and the text panel in SOUTH. Be sure to set the preferred size of each of the panels.
Upvotes: 1