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.