Im trying to make a hangman game in java using JPanels and labels, but I am struggling to get all of my panels showing up. I have to make a drawing of the hangman in a panel below the text field, but when I run my code the text field takes up the entire GUI and doesn't show my drawing at all. If anyone can help that would be great. Heres my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class GameMaker extends JFrame {
private JLabel label;
private JButton button;
private JButton button1;
private JLabel wordLabel = new JLabel("Word to Guess");
private JTextField text = new JTextField(5);
private JPanel wordPanel = new JPanel();
private JPanel displayPanel = new JPanel();
private JPanel renameThisField;
private Random random = new Random();
private String[] guesses = {"Brendan", "Vaughn", "Josh"};
private char[] randomWordToGuess =
guesses[random.nextInt(guesses.length)].toCharArray();
private int amountOfGuesses = randomWordToGuess.length;
private char[] playerGuess = new char[amountOfGuesses];
private Graphics g;
public GameMaker(){
createComponents();
}
public void createComponents(){
boolean wordIsGuessed = false;
int tries = 0;
for (int i = 0; i<playerGuess.length; i++){
playerGuess[i] = ' ';
printArray(playerGuess);
}
JLabel wordToGuessLabelBlank = new JLabel("_ _ _ _ _ _ _");
button = new JButton("Submit Letter");
ActionListener listener = new SubmitLetter();
button.addActionListener(listener);
button1 = new JButton("adasdasd");
ActionListener listener1 = new SubmitLetter();
button1.addActionListener(listener1);
label = new JLabel();
JLabel wordLength = new JLabel("The word to Guess is " + playerGuess.toString() +
"letters long.");
JPanel panel = new JPanel();
final int FIELD_WIDTH = 1;
final JTextField rateField = new JTextField(FIELD_WIDTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(wordLabel);
panel.add(wordToGuessLabelBlank);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(wordLength);
panel.add(rateField);
JComponent component = new BodyComponent();
panel.add(component);
panel.add(button);
panel.add(label);
add(panel);
}
public void printArray(char[] array){
for(int i = 0; i < array.length; i++){
System.out.println(array[i] + " ");
}
}
public class SubmitLetter implements ActionListener
{
public void actionPerformed(ActionEvent event){
}
/*This is where the magic happens.
* increment the numClicks and display results
*/
}
}
Related
I was wondering if there is an easier way to add vertical labels to the game board instead of adding individual Jlabels and moving them. I currently have a single letter set up and when i try to change the font size to anything over 10 font the text will become a small dot or just disaster.
public class View {
private JFrame frameMain;
private JPanel panelBoard;
private JPanel panelTitle;
private JPanel panelMain;
private JPanel panelY;
private JPanel panel2;
private JTextArea text;
private JLabel jlabel;
private JLabel jlabelY;
private List<JButton> list;
public View(){
frameMain = new JFrame();
frameMain.setLayout(new FlowLayout());
list = new ArrayList<>();
panelMain = new JPanel();
panelY = new JPanel();
panelY.setLayout(null);
panelBoard = new JPanel();
panelTitle = new JPanel();
panelMain.setLayout(new BorderLayout());
Board x = new Board();
GridLayout grid = new GridLayout(15,15);
x.createBoard();
panelBoard.setLayout(grid);
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
list.add(new JButton());
}
}
for(JButton x5:list){
panelBoard.add(x5);
}
jlabel = new JLabel("game");
jlabelY = new JLabel("A");
Dimension size = jlabelY.getPreferredSize();
jlabelY.setBounds(17,10 ,size.width,size.height);
jlabelY.setFont(new Font("Ariel", Font.BOLD, 10));
panelTitle.setPreferredSize(new Dimension(50,50));
panelY.setPreferredSize(new Dimension(25,600));
panelBoard.setPreferredSize(new Dimension(400,400));
panelMain.setPreferredSize(new Dimension(600,600));
panelY.add(jlabelY);
panelTitle.add(jlabel);
panelMain.add(panelTitle, BorderLayout.NORTH);
panelMain.add(panelBoard, BorderLayout.CENTER);
panelMain.add(panelY, BorderLayout.WEST);
frameMain.add(panelMain);
frameMain.setSize(600, 600);
frameMain.pack();
frameMain.setVisible(true);
}
You "could" do this using a GridLayout, but where's the fun in that. The following example makes use of GridBagLayout to layout the text and the buttons.
Trying to align components across containers is, well, let's just "hard" and leave it there, for this reason, the row labels and buttons are added to the same container. This ensures that the height of each row is based on the needs of the components within the row.
There's a few other ways you could do this, but this gives you the basic idea.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new View();
}
});
}
public class View {
private JFrame frameMain;
private JPanel panelBoard;
private JPanel panelTitle;
private JPanel panelMain;
private JPanel panel2;
private JTextArea text;
private JLabel jlabel;
private JLabel jlabelY;
private List<JButton> list;
public View() {
frameMain = new JFrame();
frameMain.setLayout(new FlowLayout());
list = new ArrayList<>();
panelMain = new JPanel();
panelBoard = new JPanel();
panelTitle = new JPanel();
panelMain.setLayout(new BorderLayout());
panelBoard.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for (int y = 0; y < 15; y++) {
gbc.gridx = 0;
gbc.gridy = y;
JLabel rowLabel = new JLabel(Character.toString('A' + y));
rowLabel.setFont(new Font("Ariel", Font.BOLD, 24));
panelBoard.add(rowLabel, gbc);
for (int x = 0; x < 15; x++) {
gbc.gridx++;
JButton btn = new JButton();
list.add(btn);
panelBoard.add(btn, gbc);
}
}
jlabel = new JLabel("game");
panelTitle.add(jlabel);
panelMain.add(panelTitle, BorderLayout.NORTH);
panelMain.add(panelBoard, BorderLayout.CENTER);
frameMain.add(panelMain);
frameMain.pack();
frameMain.setVisible(true);
}
}
}
Help is needed :)
My friends and I are coding hangman in school for a project, and we're really stumped on one part. We made a JFrame and an outer JPanel (Background), then divided it into two columns (the left column is functional as we like). Our problem lies in the right column, rightPanel. We need to set up a JLabel so we can have the lines correlating to the word length a player chooses, but our JLabel doesn't work. I've tried to just do a simple System.out.println("test"); and nothing shows up within the panel (blankSpaces). The layout has been changed as well just to try and make something show up. We also tried to set the JLabel within a method which would be called in the panel blankSpaces, but nothing seems to work. These are just two of the classes we have for the game, so if any other code is needed for explanation please let me know :)
//the rightPanel
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class RightPanel extends JPanel {
//unnecessary code rn
Guess dalia = new Guess();
WordSelection clementine = new WordSelection();
private String letterGuess = "";
public JTextField jt;
//where we tried to make a test or just declare the label
private JLabel label3;
JLabel test;
private JPanel parent;
JPanel panel3;
//constructor
public RightPanel() {
this.setLayout(new GridLayout(0, 1));
this.add(letters(parent));
this.add(lettersGuessed(parent));
this.add(blankSpaces(parent));
this.add(notLetterButtons(parent));
}
//where the alphabet is supposed to be but we just made it using coordinates
public JPanel letters(JPanel Parent) {
GridLayout layout1 = new GridLayout(1, 1);
JPanel panel1 = new JPanel(layout1);
return panel1;
}
// where the player will guess letters
public JPanel lettersGuessed(JPanel parent) {
GridLayout layout2 = new GridLayout(1, 1);
JPanel panel2 = new JPanel(layout2);
panel2.add(generateTextField());
return panel2;
}
//textfield that is currently useless
public JTextField generateTextField() {
// TODO Auto-generated method stub
// JLabel jl = new JLabel();
jt = new JTextField(1);
jt.setFont(new Font("Verdana", 1, 30));
jt.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
letterGuess = jt.getText().substring(0, 1);
// jl.setText(input);
}
});
return jt;
}
//where we coded the alphabet
#Override
public void paint(Graphics g) {
int y = this.getHeight();
int x = this.getWidth();
// super.paint(g);
String firstLine = "A B C D E F G";
String secondLine = "H I J K L M N";
String thirdLine = "O P Q R S T U";
String fourthLine = "V W X Y Z";
g.setFont(new Font("Verdana", 1, 30));
// alphabet
g.drawString(firstLine, x / 3, y / 25);
g.drawString(secondLine, x / 3, y / 12);
g.drawString(thirdLine, x / 3, y / 8);
g.drawString(fourthLine, (int) (x / 2.65), y / 6);
}
// where the blank spaces in the amount of letters to be guessed will appear
public JPanel blankSpaces(JPanel parent) {
GridLayout layout3 = new GridLayout(1, 1);
panel3 = new JPanel(layout3);
// panel3.add(label3);
/*test = new JLabel("dalia is my bffl");
test.setLayout(new BorderLayout());
test.setVerticalTextPosition(JLabel.CENTER);
test.setHorizontalTextPosition(JLabel.CENTER);
test.setVisible(true);
panel3.add(test);*/
return panel3;
}
// where player will be able to choose if they want a new word
public JPanel notLetterButtons(JPanel parent) {
// int rows = 2; int cols = 4;
GridLayout layout4 = new GridLayout(2, 4);
JPanel panel4 = new JPanel(layout4);
/*
JPanel[][] parent = new JPanel[2][4];
Container f1 = new Container();
f1.setLayout(new GridLayout(2, 4));
for(int m = 0; m < 2; m++) {
for(int n = 0; n < 4; n++) {
parent[m][n] = new JPanel();
f1.add(parent[m][n]);
}
}*/
int y = this.getHeight();
int x = this.getWidth();
//new button
JButton button1 = new JButton();
button1.setText("QUIT");
button1.addActionListener(e -> quitAction() );
panel4.add(button1);
//new button
JButton button2 = new JButton();
button2.setText("SUBMIT");
button2.addActionListener(e -> submitAction() );
panel4.add(button2);
//new button
JButton button3 = new JButton();
button3.setText("QUOTE");
panel4.add(button3);
//new button
JButton button4 = new JButton();
button4.setText("FRENCH");
panel4.add(button4);
//new button
JButton button5 = new JButton();
button5.setText("5");
button5.addActionListener(e -> fiveAction() );
panel4.add(button5);
//new button
JButton button6 = new JButton();
button6.setText("6");
button6.addActionListener(e -> sixAction() );
panel4.add(button6);
//new button
JButton button7 = new JButton();
button7.setText("7");
button7.addActionListener(e -> sevenAction() );
panel4.add(button7);
//new button
JButton button8 = new JButton();
button8.setText("RESTART");
panel4.add(button8);
return panel4;
// panel4.add(gt);
// gt.pack();
// gt.setVisible(true);
// return f1;
}
private Object fiveAction() {
clementine.wordPick(5);
//String underline = "";
//for (int x = 0; x < 5; x++) {
// underline += "_ ";
//}
JLabel lbl = new JLabel ( "i hate this");
label3.setText("_ _ _ _ _");
panel3.add(label3);
return label3;
}
private Object sixAction() {
clementine.wordPick(6);
JLabel label6 = new JLabel("_ _ _ _ _ _");
return label6;
}
private Object sevenAction() {
clementine.wordPick(7);
return null;
}
private JButton submitAction() {
String letterGuess = jt.getText();
dalia.runThrough();
return null;
}
private Object quitAction() {
System.exit(0);
return null;
}
}
//Background class
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class Background extends JPanel {
private static final int FRAME = 400;
//private Dimension rightPanelSize;
HangmanPicture hangmanPicture = new HangmanPicture();
RightPanel rightPanel = new RightPanel();
public Background(JFrame frame) {
this.setLayout(new GridLayout(0, 2));
this.setPreferredSize(frame.getSize());
this.add(hangmanPicture);
this.setPreferredSize(frame.getSize());
Dimension frameSize = frame.getSize();
this.add(rightPanel);
this.setPreferredSize(frame.getSize());
//double up = height / 4;
//double down = height * (3 / 4);
// int width = (int) (frameSize.getWidth()/2);
//int height = (int) (frameSize.getHeight());
//this.rightPanelSize = new Dimension(width, height);
}// heyy
enter code here
}
Thank you!!
I am creating a game similar to the star wars game sabacc. I am trying to create a Jtextfield that has three card suites already on the screen. The user will press a button and depending on the button they press the card suit will change to a different suit. If they get three of the same suites they win. I am having trouble getting text onto the screen though. As of right now I keep getting an error saying non static method can not be referenced by a static content.
Here is my code for the main application :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardApp extends JFrame implements ActionListener {
private JButton oneButton,
twoButton,
threeButton;
private int width = 25;
private int height = 15;
public CardApp() {
//JPanel boardPanel = new JPanel(new GridLayout(height,width));
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
JTextField TextField = new JTextField(30);
Hand settingTheText = new Hand();
TextField.setText(settingTheText.ListOfCards());
oneButton = new JButton("1");
twoButton = new JButton("2");
threeButton = new JButton("3");
// Listen for events on each button
oneButton.addActionListener(this);
twoButton.addActionListener(this);
threeButton.addActionListener(this);
// Add each to the panel of buttons
buttonPanel.add(oneButton);
buttonPanel.add(twoButton);
buttonPanel.add(threeButton);
// Add everything to a main panel attached to the content pane
JPanel mainPanel = new JPanel(new BorderLayout());
getContentPane().add(mainPanel);
mainPanel.add(TextField, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setTitle("Sabacc Example by Angela Rucci");
setSize(375, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
int pressed = 0;
if (e.getSource() == oneButton){
pressed = 1;}
if (e.getSource() == twoButton){
pressed = 2;}
if (e.getSource() == threeButton){
pressed = 3;}
Hand handObject = new Hand();
///This IS WHERE IM GETTING MY ERROR!//
String screenText = handObject.ListOfCards();
TextField.setText(screenText);
}
public static void main(String[] args) {
CardApp c = new CardApp();
}
}
This is the other file where i am getting my list of suits
package cardapp;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","D"};
Random randomInt = new Random ();
String RandomSuit;
String RandomShuffle;
String ThreeSuits;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed = 0;
public int Discards(int pressedNumber){
return pressed;
}
public void Randomizer (){
int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number
//------------------Decide what hand to randomize --------------------------//
if (pressed==1){
LeftSuit= Suits[RandomSuitNumber];
}
if (pressed==2){
MiddleSuit=Suits[RandomSuitNumber];
}
if (pressed==3){
RightSuit=Suits[RandomSuitNumber];
}
//----------------20% chance of new random set------------------------------------//
int ProabilityRandomNum = randomInt.nextInt(5);//this will create a random number for probability array
RandomShuffle= probability[ProabilityRandomNum];//this will pick a random letter in proability array
//------------If proability array equals R then change all of the suits----------//
if (RandomShuffle.equals("R")){
JOptionPane.showMessageDialog(null, "Randomized Hand!");
int leftNumber = randomInt.nextInt(4);
int middleNumber = randomInt.nextInt(4);
int rightNumber = randomInt.nextInt(4);
LeftSuit= Suits[leftNumber];
MiddleSuit= Suits[middleNumber];
RightSuit= Suits[rightNumber];}
ThreeSuits = (LeftSuit + MiddleSuit + RightSuit);
}
public String ListOfCards (){
return ThreeSuits;
}
public void GameOver(){
if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit &&
RightSuit== LeftSuit){
JOptionPane.showMessageDialog(null, "WINNER!!");
}
}
}
The variables are local to the method. the JTextField TextField is visible to the CardApp() only. if you want it to be available to the whole class, put it as a private class member :
public class CardApp extends JFrame implements ActionListener {
private JButton oneButton,
twoButton,
threeButton;
private int width = 25;
private private int height = 15;
// available to all methods
// better naming convention was JTextfield tf = new JTextField(30);
// even stackoverflow thinks its a class name :)
// see the color highlighting
private JTextField TextField = new JTextField(30);
public CardApp() {
//JPanel boardPanel = new JPanel(new GridLayout(height,width));
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
//JTextField TextField = new JTextField(30);
Hand settingTheText = new Hand();
TextField.setText(settingTheText.ListOfCards());
}
//
// code continues here ...
//
}
By default GridLayout(5,3) would add the components in this way:
A B C
D E F
G H I
J K L
M N O
To have the components disposed in the following positions:
A F K
B G L
C H M
D I N
E J O
I have this code:
//imports...
public class GridLayoutProblem {
private static final int NUM_ROWS = 5, NUM_COLMS=3;
private JPanel mainPanel = new JPanel();
private JPanel buttonPannel = new JPanel(new GridLayout(NUM_ROWS, NUM_COLMS));
private JButton btnA = new JButton("A");
private JButton btnB = new JButton("B");
//same with C, D...
private JButton btnO = new JButton("O");
private JComponent[] buttons = {
btnA, btnB, btnC, btnD, btnE,
btnF, btnG, btnH, btnI, btnJ,
btnK, btnL, btnM, btnN, btnO
};
public GridLayoutProblem(){
int i=0;
for (JComponent button : buttons){
int index = i%NUM_ROWS*NUM_COLMS+i/NUM_ROWS;
buttonPannel.add(button,index);
i++;
}
mainPanel.add(buttonPannel);
}
//...
But it results in:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position.
I did a quick test and it seems you cannot skip indexes and add elements to higher index.
So your option is to do something like this,
for (int i = 0; i < NUM_ROWS*NUM_COLMS; i++){
int index = i%NUM_COLMS*NUM_ROWS+i/NUM_COLMS; // Note the change in calculation. Just interchange rows and colms from your algo.
buttonPannel.add(button[index],i);
}
Change buttonPannel.add(button,index); to buttonPannel.add(buttons[index]);. (You don't need a foreach-loop) GridLayout always adds the components like you showed in the first example, but if you can make your calculation for index right (see other answer), so it adds it like "A,F,K,B...", you can achieve what you want.
Run the code below to see how the buttons are being added:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
//imports...
public class GridLayoutProblem {
private static final int NUM_ROWS = 5, NUM_COLMS=3;
private static JPanel mainPanel = new JPanel();
private JPanel buttonPannel = new JPanel(new GridLayout(NUM_ROWS, NUM_COLMS));
private JButton btnA = new JButton("A");
private JButton btnB = new JButton("B");
private JButton btnC = new JButton("C");
private JButton btnD = new JButton("D");
private JButton btnE = new JButton("E");
private JButton btnF = new JButton("F");
private JButton btnG = new JButton("G");
private JButton btnH = new JButton("H");
private JButton btnI = new JButton("I");
private JButton btnJ = new JButton("J");
private JButton btnK = new JButton("K");
private JButton btnL = new JButton("L");
private JButton btnM = new JButton("M");
private JButton btnN = new JButton("N");
private JButton btnO = new JButton("O");
private JComponent[] buttons = {
btnA, btnB, btnC, btnD, btnE,
btnF, btnG, btnH, btnI, btnJ,
btnK, btnL, btnM, btnN, btnO
};
public static void main(String[] args) {
new GridLayoutProblem();
}
public GridLayoutProblem(){
JFrame frame = new JFrame();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < NUM_ROWS * NUM_COLMS; i++) {
int index = i%NUM_COLMS*NUM_ROWS+i/NUM_COLMS;
buttonPannel.add(buttons[index]);
frame.revalidate();
frame.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
mainPanel.add(buttonPannel);
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}
So I am working on this small program when I ran in to this problem.Its mostly about location set up with GridBagLayout it wont show me the text for the JLable.Another problem is that every ones in a will my progress that was working stops working and later comes back.Any idea what it is?I would also like some one too help me with the location problem I want to place the JTextField in the bottom left corner any help?I cant go around this problem and no info online to specifically help me.So maybe you can help me.Hers my code so far...
package Main;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class code extends JFrame{
public static JTextField consol;
public static String title = "Metal-Lock:The Start";
public static Dimension size = new Dimension(650, 550);
public static JPanel panel;
public static JButton enter;
public static JLabel output;
public static void main(String args[]) {
code frame = new code();
}
public code() {
setTitle(title);
setSize(size);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// VISITOR LIST
CONSOL();
P1();
P2();
}
//******************************************************************************************************************************
public void CONSOL() {
consol = new JTextField(30);
consol.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String input = consol.getText();
output.setText(input);
}});
final JButton enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String input = consol.getText();
output.setText(input);
panel.add(consol);
add(panel);
panel.add(enter);
add(panel);
output = new JLabel();
panel.add(output);
add(panel);
}
});
}
//******************************************************************************************************************************
public void P1() {
}
//******************************************************************************************************************************
public static JLabel grid1;
public static JLabel grid2;
public static JLabel grid3;
public static JLabel grid4;
public static JLabel grid5;
public void P2() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints R = new GridBagConstraints();
JLabel grid1 = new JLabel ("Hello"); panel.add(grid1, R);
R.gridx = 0; R.gridy = 0;
JLabel grid2 = new JLabel ("Hello"); panel.add(grid2, R);
R.gridx = 0; R.gridy = 0;
JLabel grid3 = new JLabel ("Hello"); panel.add(grid3, R);
R.gridx = 0; R.gridy = 0;
JLabel grid4 = new JLabel ("Hello"); panel.add(grid4, R);
R.gridx = 0; R.gridy = 0;
JLabel grid5 = new JLabel ("Hello"); panel.add(grid5, R);
R.gridx = 0; R.gridy = 0;
}
}
Just went really quickly through your code.
Change these lines:
GridBagConstraints R = new GridBagConstraints();
R.gridx = 0; R.gridy = 0;
JLabel grid1 = new JLabel ("Hello");
//important to set these R values BEFORE you ad grid1.
panel.add(grid1, R);
Change this in all lines there...
You are adding 6 Labels all to the location gridx=0 and gridy=0, which is wrong. Imagine it like an excel tabel, you are inserting every label to field 1.
If you want to add fields do it like this
E.g.
x y
z
//a.gridx=0; a.gridy=0;
//y.gridx=1; y.gridy=0;
//z.gridx=0; z.gridy=1;
x coordinates are horizontal.
y are vertical starting from the top left corner.
Read this article, it's really good:
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html