0

I am writting a program that will have a 9x9 button which will have each button go from 1 to 81 starting in the upper left to going right

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

public class MS extends JFrame implements ActionListener {

    static JFrame bframe;
    static JPanel p;

    public MS() {
        p = new JPanel(new GridLayout(9,9));  
        private static JButton[][] jgo = new JButton[9][9];
        int count = 1;
        for(int row=0; row < 9; row++)
            for(int col=0; col < col; col++) {
                jgo[row][col] = new JButton("%d",count);
                p.add(jgo[row][col]);
                count++;
            }
        }

    public static void main(String[] args) {  
        bframe=new MS();    //CREATE me and 
        bframe.add(p);      //add the JPanel

                bframe.setSize(810,810);
        bframe.setLocation(0,0);                //where my upper left hand corner goes
        bframe.setVisible(true);                //I start out invisible
        bframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //need this for the window manager
    }
}

My error occurs in my constructor it has something to do how I make the buttons and set the value.

1
  • 1
    And "my error" is what? And ew, what's with the statics? Commented Mar 11, 2013 at 23:27

2 Answers 2

3

Here you go the list of constructors for JButton class

JButton()
      Creates a button with no set text or icon.
JButton(Action a)
      Creates a button where properties are taken from the Action supplied.
JButton(Icon icon)
      Creates a button with an icon.
JButton(String text)
      Creates a button with text.
JButton(String text, Icon icon)
      Creates a button with initial text and an icon.

There is not any one like the one you are using.

JButton("%d",count); // JButton(String,int); or JButton(format,int);

Instead you can use

JButton(""+count)://JButton(String text)
Sign up to request clarification or add additional context in comments.

Comments

3

JButton doesn't have any constructor that in the form of JButton(String, int) for formatting purposes, You probably want to do:

new JButton(Integer.toString(count))

instead of

new JButton("%d",count)

Side Notes:

You can't use the private keyword in a constructor:

 private static JButton[][] jgo = new JButton[9][9];
    ^ 

This line will exit immediately:

for (int col = 0; col < col; col++) {

as col < col will evaluate as true and the inner loop won't be executed.

This statement can only be valid in the class block.

Static variables are generally considered poor design. The variables bframe and p could be used be used in local scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.