Question

I have made GUIs run on my computer before, but with this program I wanted to try to implement GridBag so I could make a simple game. I don't have a clue why it isn't running. This is the code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GridBagTest extends JFrame{
    public static void main(String[] args){
        new GridBagTest();
    }

    public void GridBagTest(){
        JButton atk, mag, fort, pot1, pot2, flee;
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridBagLayout());

        JFrame gameFrame = new JFrame("FightQuest"); 
        gameFrame.getContentPane().add(gamePanel);
        gameFrame.setSize(800, 600);
        gameFrame.pack();
        gameFrame.setVisible(true);

        atk = new JButton("Strike");
        mag = new JButton("Magic");
        fort = new JButton("Fortify");
        pot1 = new JButton("Potion 1");
        pot2 = new JButton("Potion 2");
        flee = new JButton("Flee");
        addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
        addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
        addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
        addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
        addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
        addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);

    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(0, 0, 0, 0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
}

I don't know if this makes any difference, but I got most of this code from a Java reference book for Java 6 even though I'm running Java 7 since it was all my school had. I am also doing all my code on the XFCE operating system.

Was it helpful?

Solution

Change this line

public void GridBagTest()

to

public GridBagTest()

Constructor doesn't have return type. Also you should call pack() and setVisible(true) to size and show components after adding them to the container.

Also note that extending is not necessary in this case.

Change

public class GridBagTest extends JFrame

to

public class GridBagTest{

Changes shows in code:

public class GridBagTest{
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GridBagTest();
            }
        });
    }

    public GridBagTest(){
        JButton atk, mag, fort, pot1, pot2, flee;
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridBagLayout());

        JFrame gameFrame = new JFrame("FightQuest"); 
        gameFrame.getContentPane().add(gamePanel);
        gameFrame.setSize(800, 600);


        atk = new JButton("Strike");
        mag = new JButton("Magic");
        fort = new JButton("Fortify");
        pot1 = new JButton("Potion 1");
        pot2 = new JButton("Potion 2");
        flee = new JButton("Flee");
        addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
        addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
        addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
        addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
        addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
        addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);
        gameFrame.pack();
        gameFrame.setVisible(true);

    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(0, 0, 0, 0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
}

And you get your output:

enter image description here

OTHER TIPS

Not sure if this is your problem, but your calls to gameFrame.setSize(800, 600); and gameFrame.pack(); seem to be conflicting, as pack() will re-set the size according to the current contents of the frame. (Which at the point you call pack(), is nothing)

By the way, you should also create and update your Swing components on the Event Dispatching Thread.

You are making a psudo constructor for placing void before GridBagTest() which specifies that GridBagTest() is a function not a constructor. So call:

new GridBagTest().GridBagTest();

inside the main function. Which obviously you should not do: rather remove the return type void:

public GridBagTest(){ //<--- void is removed


        JFrame gameFrame = new JFrame("FightQuest"); 
        // other code
}

However, always start your program from the EDT by submitting it to EventQueue using SwingUtilities.invokeLater(Runnable):

SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {

               new GridBagTest();
            }
        });

The first thing I look for when reviewing problems like this is how the application is launched. Please notice that you are not properly launching your GUI. You should be making sure that the GUI is launched on the EDT (Event Dispatch Thread).

Here is an example of how to launch your specific GUI.

SSCCE:

import javax.swing.*;
import java.awt.*;

/**
 * http://stackoverflow.com/questions/20478125/gui-not-showing-on-my-screen/20478279#20478279
 */
public class Q20478125 {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Q20478125();
            }
        });
    }

    public Q20478125() {
        JButton atk, mag, fort, pot1, pot2, flee;
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridBagLayout());
        gamePanel.setPreferredSize(new Dimension(800, 600));

        JFrame gameFrame = new JFrame("FightQuest");
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.getContentPane().add(gamePanel);
        gameFrame.pack();
        gameFrame.setVisible(true);

        atk = new JButton("Strike");
        mag = new JButton("Magic");
        fort = new JButton("Fortify");
        pot1 = new JButton("Potion 1");
        pot2 = new JButton("Potion 2");
        flee = new JButton("Flee");
        addItem(gamePanel, atk, 0, 0, 1, 1, GridBagConstraints.SOUTHEAST);
        addItem(gamePanel, mag, 1, 0, 1, 1, GridBagConstraints.SOUTH);
        addItem(gamePanel, fort, 2, 0, 1, 1, GridBagConstraints.SOUTHWEST);
        addItem(gamePanel, pot1, 0, 1, 1, 1, GridBagConstraints.NORTHEAST);
        addItem(gamePanel, pot2, 1, 1, 1, 1, GridBagConstraints.NORTH);
        addItem(gamePanel, flee, 2, 1, 1, 1, GridBagConstraints.NORTHWEST);

    }

    private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
        GridBagConstraints gc = new GridBagConstraints();
        gc.gridx = x;
        gc.gridy = y;
        gc.gridwidth = width;
        gc.gridheight = height;
        gc.weightx = 100.0;
        gc.weighty = 100.0;
        gc.insets = new Insets(0, 0, 0, 0);
        gc.anchor = align;
        gc.fill = GridBagConstraints.NONE;
        p.add(c, gc);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top