creating an array of buttons in a jframe
hi, i need help im trying to create a simple button game where there are 5x3 buttons in a jframe where the user can click on one to find the button with the prize on it, i know how to create the jframe but im trying to approch this in a object orientated way, with classes not a procedural way, and having no luck, any suggestions?
# 1
here an example to start with :
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyGame extends JPanel{
public MyGame(int rows, int cols){
super(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JButton b = new JButton(" Button [x="+i+", y="+j+"]");
b.addActionListener(new MyActionListener(i, j));
this.add(b);
}
}
}
class MyActionListener implements ActionListener{
private int x;
private int y;
public MyActionListener(int x, int y){
this.x=x;
this.y=y;
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(MyGame.this, " Message from Button [x="+x+", y="+y+"]");
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MyGame(3, 5);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Hope That Helps
# 2
oh thanks thats a great help, getting somewhere now , however how would i randomize the postion of the prize, as ive got the prize in the same place each time doh!
# 3
ok, I suggest this :
package test;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyGame extends JPanel{
private Random rand ;
private double[] prize = {1.0, 2.5, 3.0, 4.6, 5.0, 6.0, 7.7, 8.1, 9.2, 10.0};//10 doubles for example
public MyGame(int rows, int cols){
super(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JButton b = new JButton(" Button [x="+i+", y="+j+"]");
b.addActionListener(new MyActionListener(i, j));
this.add(b);
}
}
rand = new Random(System.currentTimeMillis());
}
class MyActionListener implements ActionListener{
private int x;
private int y;
public MyActionListener(int x, int y){
this.x=x;
this.y=y;
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(MyGame.this, " Your Prize : "+prize[rand.nextInt(prize.length+1)]);
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MyGame(3, 5);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Hope That Helps
# 4
i think ive confused u, there is only one prize in the grid and it is only on one button, they hav to find it.
# 5
Ok, here what you want, but don't forget dukes:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyGame extends JPanel{
private Random rand ;
private int rows;
private int cols;
private int prizeX;
private int prizeY;
public MyGame(int rows, int cols){
//super(new GridLayout(rows, cols));
super(new BorderLayout());
this.rows = rows;
this.cols = cols;
JPanel p = new JPanel(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JButton b = new JButton(" Button [x="+i+", y="+j+"]");
b.addActionListener(new MyActionListener(i, j));
p.add(b);
}
}
this.add(p, BorderLayout.CENTER);
this.add(new MyReStartButton(), BorderLayout.SOUTH);
rand = new Random(System.currentTimeMillis());
prizeX = rand.nextInt(rows);
prizeY = rand.nextInt(cols);
System.out.println(prizeX+", "+prizeY);
}
class MyActionListener implements ActionListener{
private int x;
private int y;
public MyActionListener(int x, int y){
this.x=x;
this.y=y;
}
public void actionPerformed(ActionEvent e) {
if(x==prizeX&&y==prizeY){
JOptionPane.showMessageDialog(MyGame.this, " Winner !! ");
}else{
JOptionPane.showMessageDialog(MyGame.this, " Try again ");
}
}
}
class MyReStartButton extends JButton{
public MyReStartButton(){
super("Start new Game");
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
MyGame.this.prizeX = rand.nextInt(rows);
MyGame.this.prizeY = rand.nextInt(cols);
System.out.println(prizeX+", "+prizeY);
}
});
}
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new MyGame(3, 5);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setLayout(new BorderLayout());
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
# 6
im trying to extend the game further, how wood i go about only allowing the user 5 go's at guessing and finshing the game if they dont succed?