English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to create a 7 by 7 grid of JButtons in java. I quite frankly have very little idea about how to go about this. its eventally gonna be the game ConnectFour, but I just need the board to get started. this is what i got so far,


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

public class ConnectFour extends JFrame{

public ConnectFour(){
JPanel board = new JPanel(new GridLayout(7, 7));
JButton[][] b = new JButton[7][7];
...


i know thats not much... thanks

2006-11-02 16:27:11 · 2 answers · asked by snoboarder2k6 3 in Computers & Internet Programming & Design

2 answers

You should look at the java swing tutorials available here
http://java.sun.com/docs/books/tutorial/ui/index.html

and the API on the buttons etc., as there is a lot involved in making the buttons, and adding action listeners, etc.
http://java.sun.com/j2se/1.5.0/docs/api/

The JButton API is here
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JButton.html

The addActionListener method API is here
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractButton.html#addActionListener(java.awt.event.ActionListener)

Which is part of the AbstractButton API
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractButton.html

You would be best of making Actions to go with the buttons, Actions can be found here
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Action.html

AbstractActions, which you can build on are here.
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/AbstractAction.html

2006-11-05 01:54:03 · answer #1 · answered by Mark aka jack573 7 · 0 1

You don't need a double array of buttons. You don't need an array at all. The GridLayout will manage placement of the buttons you add to the JPanel called "board".

e.g.,

JButton one = new JButton("One");
board.add(one);
board.add(new JButton("Two"));
etc...

See the links listed by the poster above to learn how to make the JButtons do things (attach an action listener).

2006-11-06 13:23:11 · answer #2 · answered by vincentgl 5 · 0 0

fedest.com, questions and answers