Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:25:31+00:00 2026-06-07T10:25:31+00:00

I’m having trouble generating an array of buttons for a clone of battleship for

  • 0

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]);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:25:32+00:00Added an answer on June 7, 2026 at 10:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a jquery bug and I've been looking for hours now, I can't
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an array which has BIG numbers and small numbers in it. I
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.