Java swing layout, 3 panels - java

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();
}
}

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.

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..
how you want your subpanels to be arranged and
how would you want them to react when the frame is resized and
how you want the components in the panels to be arranged.
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);
}
});
}

Related

jtextarea wont fill panel with GridBagConstraints

From my code I expect my JTextArea to fill the top left border seen below:
But as you can see its taking up a tiny section in the middle.
I am using GridBagConstraints on the panel which contains the components.
There is a main class which calles up a class called frame. This class creates the JFrame and sets the size as well as other things. This is then called from the MainFrame.java which has extended the jframe which creates 3 panels and sets their layout. this is seen below
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
private Panel1 storyPanel;
private Panel2 statsPanel;
private Panel3 commandsPanel;
public MainFrame(String title)
{
super(title);
// Setting Layout
GridBagConstraints gbc = new GridBagConstraints();
storyPanel = new Panel1();
storyPanel.setLayout(new GridBagLayout());
statsPanel = new Panel2();
commandsPanel = new Panel3();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(statsPanel, BorderLayout.EAST);
p.add(commandsPanel, BorderLayout.SOUTH);
}
}
The panel in questions is Panel1 or storyPanel. I have set the layout and the code calls the Panel1.java as seen below:
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
add(scroll, gbc);
}
}
I don't understand why my gbc.fill isnt making the JTextArea fill the top left border of the screen shot.
Thanks in advance for any reply's
-Tom T
changing the layout to border layout
import javax.swing.*;
import java.awt.*;
public class Panel1 extends JPanel
{
public Panel1()
{
//GridBagConstraints gbc = new GridBagConstraints();
//gbc.weightx = 0.1;
//gbc.weighty = 0.1;
BorderLayout b = new BorderLayout();
//Set size of Panel1
int xsizeP1 = (Frame.xsize() / 2);
int ysizeP1 = (Frame.ysize() / 3 * 2);
setPreferredSize(new Dimension(xsizeP1, ysizeP1));
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(" test ");
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//gbc.gridx = 0;
//gbc.gridy = 0;
//gbc.weightx = 1;
//gbc.weighty = 1;
//gbc.fill = GridBagConstraints.BOTH;
add(scroll, b.CENTER);
}
}
Don't set the layout of storyPanel, as it's contents have already been added and laid out, so changing the layout manager here will discard any properties you applied. Instead, set the layout in Panel1's constructor before you add any components.
Use GridBagConstraints.BOTH for the fill property. The fill property can only have one specified value
Use weightx and weighty to specify how much of the available space the component should use
For example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainFrame extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new MainFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private Panel1 storyPanel;
// private Panel2 statsPanel;
// private Panel3 commandsPanel;
public MainFrame(String title) {
super(title);
storyPanel = new Panel1();
Container p = getContentPane();
p.add(storyPanel, BorderLayout.WEST);
p.add(new JLabel("East"), BorderLayout.EAST);
p.add(new JLabel("South"), BorderLayout.SOUTH);
}
public class Panel1 extends JPanel {
public Panel1() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//Set size of Panel1
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(20, 20);
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//GridBagConstraints setup for components
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
add(scroll, gbc);
}
}
}
Having said all that, a BorderLayout would be simpler
public class Panel1 extends JPanel {
public Panel1() {
setLayout(new BorderLayout());
//Set size of Panel1
setBorder(BorderFactory.createLineBorder(Color.black));
//Adding JTextArea and adding settings to it
JTextArea storyLine = new JTextArea(20, 20);
storyLine.setLineWrap(true);
storyLine.setWrapStyleWord(true);
storyLine.setEditable(false);
//Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
JScrollPane scroll = new JScrollPane(storyLine);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scroll);
}
}
i tried a border layout and set it to center expecting it to resize but that failed as well
Would suggest that you're making a fundamental mistake some where, as it works fine for me. Remember, set the layout BEFORE you add any components to the container

Java, setting layout to null

I have been using a null layout and I alot of people will say it shouldn't be done this way. Is there a better way?
Some code as example:
import javax.swing.*;
public class Main{
public static void main(String args[]){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Click");
//JFrame, frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//JPanel, panel
panel.setLayout(null); //<<---- Is this correct?
frame.add(panel);
//JButton, button
button.setBounds(25, 25, 100, 60); //<<---- Is this correct?
panel.add(button);
}
}
Is there a better way?
Layout managers, layout managers, layout managers. If the default provided layout managers aren't doing what you want, either use a combination of layout managers or maybe try some of the freely available layout managers, like MigLayout (and use them in combination with other layout managers as required)
With your code...
Using a GridBagLayout
The button is slightly bigger, because it's taking into account the actual requirements of the button (text and font size), but will always add an additional 100 pixels to the width and 60 pixels to the height.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Click");
//JFrame, frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//JPanel, panel
panel.setLayout(new GridBagLayout());
frame.add(panel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 100;
gbc.ipady = 60;
gbc.insets = new Insets(25, 25, 0, 0);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
panel.add(button, gbc);
}
}
Now, if all I do is add button.setFont(button.getFont().deriveFont(64f)); we end up with...
Your code on the left, my code on the right...
And if you think that's been overly dramatic, different OS's will do worse things to you then that

BorderLayout, GridLayout, GridBagLayout? Which should I use?

I'm trying to create this basic GUI, but cannot get my panels to setup correctly.(Numbers are pixel sizes)
I've tried using this tutorial as a reference (http://www.youtube.com/watch?v=Kl3klve_rmQ) but, mine never works the same.
My code declares variables in the top of the class, then creates a constructor which add the components (panels, buttons, etc), then it calls the constructor in the main method.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FinalProject extends JPanel
{
private static final long serialVersionUID = 1L;
static JPanel nav;
static JPanel queue;
static JPanel menu;
GridBagConstraints gbc = new GridBagConstraints();
public FinalProject()
{
nav = new JPanel();
nav.setLayout(new GridBagLayout());
nav.setBackground(Color.RED);
gbc.gridy = 0;
gbc.gridx = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
add(nav, gbc);
queue = new JPanel();
queue.setLayout(new GridBagLayout());
queue.setBackground(Color.GREEN);
gbc.gridy = 1;
gbc.gridx = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
add(queue, gbc);
menu = new JPanel();
menu.setLayout(new GridBagLayout());
menu.setBackground(Color.BLUE);
gbc.gridy = 2;
gbc.gridx = 2;
gbc.gridheight = 1;
gbc.gridwidth = 1;
add(menu, gbc);
}
public static void main(String[] args)
{
FinalProject p = new FinalProject();
JFrame f = new JFrame();
f.add(nav);
f.add(queue);
f.add(menu);
f.setTitle("Subway");
f.setSize(800, 500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);
f.add(p);
}
}
How should I go about getting this layout right? Panels in Panels, Panels independent of each other, etc.?
No, don't use a GridBagLayout for this as you'll be adding more complexity than is actually needed. Myself, I try to avoid using this layout and all its potential pitfalls as much as possible, and usually you can get all you need by nesting JPanels, each using its own more simple layout. For instance here, all you need is a BorderLayout:
Place the top JPanel in the BorderLayout.NORTH position
Place the left JPanel in the BorderLayout.WEST position
Place the center JPanel in the BorderLayout.CENTER position.
That's it.
Again, please check out the Swing and the layout manager tutorials as the information is well presented there.
Edit
Note that nothing shows up on your JFrame because you're not adding your JPanel to the JFrame!
Edit 2
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.*;
public class SimpleLayout {
private static final Color GREEN = new Color(200, 255, 200);
private static final Color BLUE = new Color(200, 200, 255);
private static void createAndShowGui() {
JFrame frame = new JFrame("SimpleLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// note that a JFrame's contentPane uses BorderLayout by default
frame.getContentPane().add(new ColorPanel(Color.pink, 800, 80), BorderLayout.NORTH);
frame.getContentPane().add(new ColorPanel(GREEN, 300, 420), BorderLayout.WEST);
frame.getContentPane().add(new ColorPanel(BLUE, 500, 420), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ColorPanel extends JPanel {
private static final float FONT_POINTS = 24f;
private int prefW;
private int prefH;
public ColorPanel(Color color, int prefW, int prefH) {
setBackground(color);
this.prefW = prefW;
this.prefH = prefH;
// GBL can be useful for simply centering components
setLayout(new GridBagLayout());
String text = String.format("%d x %d", prefW, prefH);
JLabel label = new JLabel(text, SwingConstants.CENTER);
label.setFont(label.getFont().deriveFont(FONT_POINTS));
label.setForeground(Color.gray);
add(label);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
}
This displays as:
GridLayout I've never actually used but I'm not sure if it can do this.
BorderLayout can do this and is simpler to use than GridBagLayout.
GridBagLayout can do this.
In general GridBagLayout is one of the most flexible LayoutManagers and is well worth learning so I'd recommend using it here to get used to it on a simple case before you need it anyway for something more complex.
If you are familiar with HTML then think of GridBagLayout as working like HTML tables.
Very quickly - create the three panels and set the sizes/borders/whatever you need in them.
Add one in cell 0,0 with a colspan of 2.
Add one in cell 1,0
Add one in cell 1,1
After that you are done although you will probably want to specify resize/anchor/etc behaviour.

Best Layout for Dynamically Sized Panel

What I am looking for is a suggestion for the best layout to use for my scenario. I basically have any number of child panels which can be in a container panel that can be dynamically resized by the user. All of the child panels will be 300 pixels in width, and can have a variable height. I would like the panels to be placed into the panel from left-to-right, top-to-bottom, just like the FlowLayout. However, anything I try with the FlowLayout will vertically center the panel with less height. I would instead like the panel to be anchored to the top of the screen.
I have created the following example using the FlowLayout to show what I mean.
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DynamicPanel extends JPanel {
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.add(new DynamicPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public DynamicPanel() {
setupGUI();
}
private void setupGUI() {
this.setLayout(new FlowLayout(FlowLayout.LEFT));
this.add(getPanel(1, 4));
this.add(getPanel(2, 2));
}
private JPanel getPanel(int panelNum, int numButtons) {
JPanel panel = new JPanel(new GridBagLayout()) {
#Override
public Dimension getPreferredSize() {
Dimension ret = super.getPreferredSize();
ret.width = 300;
return ret;
}
};
panel.add(new JLabel("Panel "+panelNum), getGrid(0, 0, 1.0, 0));
for(int i = 0; i < numButtons; i++) {
panel.add(new JButton("Button"), getGrid(0, i+1, 1.0, 0));
}
return panel;
}
/*
* Returns the GridBagConstraints for the given x, y grid location
*/
private GridBagConstraints getGrid(int x, int y, double xweight, double yweight) {
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.gridx = x;
c.gridy = y;
c.weightx = xweight;
c.weighty = yweight;
return c;
}
}
In this example, I would like the labels Panel1, and Panel2 to be straight across from each other, instead of Panel2 being set lower because the associated panel is centered.
I guess I could use GridBagLayout, and add a component listener to the container panel, and edit the GridBagContraints accordingly for each child panel when the container panel is resized, but I am wondering if there is a better way to do this? In case this matters, in the actual program the child panels will be custom panels, not just a list of buttons.
Thanks in advance for any help!
I was able to achieve this
Using this...
private void setupGUI() {
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
this.add(getPanel(1, 4), gbc);
this.add(getPanel(2, 2), gbc);
}
The problem is, you're going to need to get your hands a little dirty, as no layout manager will do exactly what you want (with the possible exception of MigLayout, but I've never used it)
What I would do, is create a JPanel per row, set it's layout to GridBagLayout and use the above concept to layout to layout the number of columns you need, then do this for each row...

Setting frame size when using GridLayout in Swing

In the below code, I am using gridbaglayout to have all my buttons displayed in two lines on a frame size of 600 X 400. Commenting out setSize() or pack() in the below code did not help. my question is how to get frame of size 600 X 400, and the bottom of frame has a panel with alpabet buttons. Thanks for help.
import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.*;
public class HangmanGUI {
public static void main(String[] args){
new HangmanGUI();
}
//constructor for Hangman
/**
* Instantiates a new hangman gui.
*/
public HangmanGUI() {
JFrame myframe= new JFrame();
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout());
GridBagConstraints gbc = new GridBagConstraints();
myframe.setSize(600,400);
int x =0; int y=5;
for (char alphabet = 'A';alphabet<='Z';alphabet++){
gbc.gridx=x;
gbc.gridy=y;
myPanel.add(new JButton(alphabet+""),gbc);
x++;
if (x>15){
y =6;x=0;
}
}
myframe.add(myPanel);
myframe.pack();
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
When packed, the frame size is computed based on the preferred size of it's content pane.
Personally, I'd care less about the window size and the functionality of the program and let the underlying framework figure it all out...but, if it's important to you...
Start with, something like, a JPanel and override it's getPreferredSize method...
public class BigPane extends JPanel {
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
}
Set this panel as the frame's content pane...
JFrame myframe= new JFrame();
myFrame.setContentPane(new BigPane());
// This is important as the panels default layout is FlowLayout...
myFrame.setLayout(new BorderLayout());
If you want you button pane to positioned in the south position, then you simply need to supply the correct layout constraint for the layout, in this case, BorderLayout...
myFrame.add(myPanel, BorderLayout.SOUTH);
This will allow you to add a "main" component to the CENTER position.
As has already being suggested, it might be better to use a GridLayout for the buttons, but this will depend on what you want to achieve.
Take a look at Laying out components within a container for more details
In your last Q&A you were advised to use GridLayout as opposed to GridBagLayout. GridBagConstraints are only used in the latter.
This is probably not how you want the GUI to look, but take it as a basic guide of what GridLayout is actually good for.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class HangmanGUI {
/**
* Instantiates a new hangman gui.
*/
public HangmanGUI() {
JPanel gui = new JPanel(new BorderLayout(2,2));
BufferedImage bi = new BufferedImage(
600,200,BufferedImage.TYPE_INT_RGB);
gui.add(new JLabel(new ImageIcon(bi)));
JFrame myframe= new JFrame();
JPanel myPanel = new JPanel();
gui.add(myPanel,BorderLayout.PAGE_END);
myPanel.setLayout(new GridLayout(2,0,0,0));
int x =0; int y=5;
for (char alphabet = 'A';alphabet<='Z';alphabet++){
myPanel.add(new JButton(alphabet+""));
x++;
if (x>15){
y =6;x=0;
}
}
myframe.add(gui);
myframe.pack();
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new HangmanGUI();
}
}
JFrame myframe= new JFrame();
myframe.getContentPane().setLayout(new BorderLayout());
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout(2,13));
GridBagConstraints gbc = new GridBagConstraints();
myframe.setSize(600,400);
int x =0; int y=5;
for (char alphabet = 'A';alphabet<='Z';alphabet++){
gbc.gridx=x;
gbc.gridy=y;
myPanel.add(new JButton(alphabet+""),gbc);
x++;
if (x>15){
y =6;x=0;
}
}
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I added the BorderLayout to the ContentPane and then added the Panel to the bottom of the ContentPane.
Also removed the pack.
And added the constraints in your new GridLayout to specify 2 rows of 13 columns

Categories