Hi. I am coding a game for my basic Java programming course, and I am currently coding a game (Othello, aka Reversi).

I am having issues with the JButton size.

public void game ()
    {
        resize (800, 680);

        game = new Panel ();

        Panel board = new Panel (new GridLayout (row, col, 0, 0));
        //board.setSize (new Dimension (640, 640));

        //initialize tracking array to have all black backgrounds
        tracker = new Color [row] [col];
        for (int i = 0 ; i < row ; i++)
        {
            for (int j = 0 ; j < col ; j++)
            {
                tracker [i] [j] = Color.black;
            }
        }

        [B]//declare a new array of buttons
        a = new JButton [total]; //<--resize the button (I think this is the one)
        //a.setSize (85, 85); //Attempt
        

        //initialize each of the buttons in the array
        //with an empty label
        for (int i = 0 ; i < total ; i++)
        {
            a [i] = new JButton (createImageIcon ("images\\blank.gif")); \\Calling blank image file, 80x80
            a [i].addActionListener (this);
            a [i].setActionCommand ("" + i);
            a [i].setBackground (Color.black);
            board.add (a [i]);
        }
[/B]
        game.setBackground (Color.black);
        //Adding

        JPanel grid = new JPanel ();
        //Creating the layout
        grid.setLayout (new BoxLayout (grid, BoxLayout.Y_AXIS));
        grid.setBackground (Color.black);
        
        grid.add (menuBar);
        grid.add (board);

        game.add (grid);

        screens.add ("3", game);
    }

This is one of the cards (screens), and the layout used for that is CardLayout. I resized the window to 800 by 680.

And then I copied the teacher's 2D button array code, and changed it slightly. I just made it so that every button appears to be blank. The middle four are different (for the start of the game, but I don't know how to fix it yet).

I need to resize the button to 80x80 (the image is exactly 80x80, square). So the square grid can be 640x640 (or maybe slightly bigger if necessary).

It might also be worth noting that I am using Java 1.4.2, Ready to Program v1.7.1

Thank you

Edited 6 Years Ago by IOwnAndPwnU: n/a

You don't say what the problem is, exactly, but you are mixing old-style AWT classes (eg Panel) with Swing classes (eg JButton). This is always dodgy. You should replace all the AWT ? classes with the swing J? equivalent.
Your buttons should resize to fit the image when you pack the window, although you will probably have to override the default insets to avoid blank space around the icon.
What do you know about Java 1.7.1 that we don't? 1.7 isn't out yet. (Current is 1.6u20).

The problem is that the JButton is larger than the image in it. There is excess space around the image, which is 80x80. I need it to appear that all images/buttons are exactly side by side. I said Java 1.4.2, the program I use is v1.7.1 (Ready to Program).

What is the difference between JPanel and Panel? And I realize that it's not the best way, but I have to, since that's what our teacher told us.

So how would I override the default insets to avoid blank space around the icon?

The original Java AWT design was prett limited, so Swing was introduced with a far better architecture (and loads of other small improvements while they were at it).

So what's ReadyTo Program? I thought you were just using those words as simple English, sorry.

The SetMargin method overrides the insets, but I find it's hard to get the button default graqphics out of the way, so I always use JLabels to display graphics, you just have to listen for MouseClicked instead of ActionPerformed.

Edited 6 Years Ago by JamesCherrill: n/a

The original Java AWT design was prett limited, so Swing was introduced with a far better architecture (and loads of other small improvements while they were at it).

So what's ReadyTo Program? I thought you were just using those words as simple English, sorry.

The SetMargin method overrides the insets, but I find it's hard to get the button default graqphics out of the way, so I always use JLabels to display graphics, you just have to listen for MouseClicked instead of ActionPerformed.

Yeah, Ready to Program is a Java Compiler.

I tried

a.setMargin (0, 0);

but I keep getting the error:
No method named "setMargin" was found in type "javx.swing.JButton[]".

Your other solution looks like a good one, but I think it would not work for my course - cause I have to use her way.

So how else could I eliminate the space of a button with an image inside it.

Alright, I tried using the Insets instance ... kind of confused.

I'll show you what I tried:

a = new JButton [total];
        a.setMargin (new Insets (0,0,0,0));

It gives me the same error ("No method named "setMargin" was found in type "javax.swing.JButton[]."

So how would I use this Insets thing (code would be helpful please).

I was trying out MouseListener, but at the top of my code, I have this:

public class Othello extends Applet implements ActionListener

It says on http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html I need to add

public class MouseEventDemo ... implements MouseListener {

I tried:

public class Othello extends Applet implements ActionListener MouseListener
public class Othello extends Applet implements ActionListener implements MouseListener

But they both failed. So how do I get both Listeners in there?

Alright, I managed to get the border removed by adding 2 lines, so the array aspect looks like this:

for (int i = 0 ; i < total ; i++)
        {
            //Set-up the starting game of Othello
            a [i] = new JButton (createImageIcon ("images\\blank.gif"));
            //Since 0, 27 is the 28th space in the board
            a [27] = new JButton (createImageIcon ("images\\white.gif"));
            a [28] = new JButton (createImageIcon ("images\\black.gif"));
            a [35] = new JButton (createImageIcon ("images\\black.gif"));
            a [36] = new JButton (createImageIcon ("images\\white.gif"));
            a [i].addActionListener (this);
            a [i].setActionCommand ("" + i);
            a [i].setBackground (Color.black);
            a [i].setForeground (Color.black);
            //Remove space between buttons and images in button (margin)
[B]            a [i].setMargin (new Insets (0, 0, 0, 0));[/B]
            //Further removes button aspect of images
[B]            a [i].setBorder (null);[/B]
            board.add (a [i]);
        }

I added the two lines bold.

Next question is how would I implement two listeners.

I need the

public class Othello extends Applet implements ActionListener

which is just below my libraries. But I also want to be able to use the MouseListener. So, I'm having trouble implementing both listeners.

(additional untaught concepts is good).

I tried:

public class Othello extends Applet implements ActionListener MouseListener
1.
      public class Othello extends Applet implements ActionListener implements MouseListener

The reason I need the MouseListener is because I may want it such that one a user is over a certain part of the array, a hint shows. Or maybe, I want it so that (since my game is Othello, aka Reversi), such that when the user's mouse is over a possible move, the image changes. Regardless, I need the MouseListener and the ActionListener (that's a must).

Thank you.

This question has already been answered. Start a new discussion instead.