Peter Chappy
Peter Chappy

Reputation: 1179

Debugging JFrames with array of Buttons

I'm having trouble generating an array of buttons for a clone of battleship for my class, and can't seem to figure out why it is not working. Any advice would help... I have the main class creating the jFrame, then the grid class, more specifically the generator method builds the array of buttons.

import java.awt.*;

import javax.swing.*;

public class warship {

/**
 * @param args
 */

public static void main(String[] args) {
    JFrame gui = new JFrame();
    gui.setSize(700, 350);
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setLayout(new FlowLayout());
    grid oceanGrid = new grid();
    oceanGrid.Generator();
    gui.add(oceanGrid);
    gui.setVisible(true);

}

}

grid.java

     import java.awt.Dimension;
     import java.awt.GridLayout;
     import java.awt.LayoutManager;

     import javax.swing.ImageIcon;
     import javax.swing.JButton;
     import javax.swing.JPanel;
     import javax.swing.border.TitledBorder;


     @SuppressWarnings("serial")
public class grid extends JPanel{
private static int rows = 7;
private static int col = 10;

public void Generator(){

    ImageIcon wIcon = new ImageIcon    ("H:\\workspace\\Warship\\src\\images\\water.jpg");
    JPanel jPan1 = new JPanel();
    jPan1.setLayout((LayoutManager) new GridLayout(rows,col,1,1));
    jPan1.setSize(350,350);

    //Set Border 
    TitledBorder bdr = javax.swing.BorderFactory.createTitledBorder(null, "Targeting Grid",
            javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
            javax.swing.border.TitledBorder.DEFAULT_POSITION,
            new java.awt.Font("Arial", 0, 16));
    bdr.setTitleColor(java.awt.Color.RED);
    jPan1.setLayout((LayoutManager) new GridLayout(rows,col,1,1));      
    jPan1.setBorder(bdr);

    //Creates the array of buttons
    JButton b[]=new JButton[rows*col];
    for (int i = 0, j= rows*col; i < j; i++){
        b[i] = new JButton(wIcon);
        b[i].setSize(20, 20);
        b[i].setMaximumSize(new Dimension(20,20));
        b[i].setPreferredSize(new Dimension(20,20));
        System.out.println("loop test " + i);
        jPan1.add(b[i]);
    }
}
}

Upvotes: 2

Views: 1065

Answers (3)

JayCD
JayCD

Reputation: 57

1 the JPanel in grid is unneeded.
2 the JPanel in grid isn't added to anything using the .add() method.
It seems other people already got to this however.

As said, you should remove the line "JPanel jPan1 = new JPanel();"
and replace the word "jPan1." with the word "this." all lower cases.

Here is a correct indenting of your code edited, or at least one that is easier to read.

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;


@SuppressWarnings("serial")
public class grid extends JPanel{
    private static int rows = 7;
    private static int col = 10;

    public void Generator(){

        ImageIcon wIcon = new ImageIcon ("H:\\workspace\\Warship\\src\\images\\water.jpg");

        this.setLayout((LayoutManager) new GridLayout(rows,col,1,1));
        this.setSize(350,350);

        //Set Border 
        TitledBorder bdr = javax.swing.BorderFactory.createTitledBorder(null,         "Targeting Grid",
            javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
            javax.swing.border.TitledBorder.DEFAULT_POSITION,
            new java.awt.Font("Arial", 0, 16));
        bdr.setTitleColor(java.awt.Color.RED);

        this.setLayout((LayoutManager) new GridLayout(rows,col,1,1));      
        this.setBorder(bdr);

    //Creates the array of buttons
        JButton b[]=new JButton[rows*col];
        for (int i = 0, j= rows*col; i < j; i++){
            b[i] = new JButton(wIcon);
            b[i].setSize(20, 20);
            b[i].setMaximumSize(new Dimension(20,20));
            b[i].setPreferredSize(new Dimension(20,20));
            System.out.println("loop test " + i);
                this.add(b[i]);
        }
    }
}

do note that anything after this point is just helpful criticism of style at least.

I would have used a constructor in grid so you don't have to call that method. Like this:

public Generator(){
    super();

    //code in Generator() here.
}

and now you don't need to call the method "Generator()"

and these two lines

javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION,

could be shorter like this.

TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,

Upvotes: 1

Joseph Elcid
Joseph Elcid

Reputation: 877

I think this is the mistake you are making:
Your class grid extend JPanel, but you declare and initialize another JPanel in which you add your buttons. So you are not actually adding the buttons to your grid but to another panel you do not use.

The solution is to remove this line

JPanel jPan1 = new JPanel();

and to replace all occurences of jPan1 with this

This way you will be adding the buttons to your grid.

Upvotes: 4

Matt Westlake
Matt Westlake

Reputation: 3651

I believe you are missing the pack() command right before your setVisible(true).

Upvotes: 1

Related Questions