(Java GUI)Changing the Text of a JButton once its pressed.

  • Comp Sci
  • Thread starter S.Parker
  • Start date
  • Tags
    Text
In summary, Grid creates a grid with 10 rows and 10 columns. It also creates 10 JToggleButton buttons and sets them to be disabled. It then loops through 10 mines and sets each mine to be "0" if it is not already set. Once the mines are all set, it sets the "0" button on the grid to be enabled.
  • #1
S.Parker
4
0

Homework Statement


Im a big rookie in Java ( and pretty much every language). I am trying to make a Minesweeper game for an assignment and I am still in the beta stages. I am stuck however on how to display the mines as a "0" once the button is pressed. I created the Jbuttons using a 2d array, but I can't seem to put the "0" on the specific button I want.

Here is my code ( please excuse the ugliness/ probable inefficiency) I really am clueless when it comes to GUI's.


Homework Equations


Code:
import java.awt.*;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Grid extends JPanel {
    // Initializes rows,columns and mines
    int rows = 10;
    int cols = 10;
    int i, j = 0;
    int mines = 10;
    boolean[][] setmine = new boolean[rows][cols];
    boolean[][] clickable = new boolean[rows][cols];
    private JToggleButton squares[][], squares2[][];

    // Contructor for creating a grid(with default size 400x400
    public Grid() {
        this.setSize(600, 600);
        this.setLayout(new GridLayout(rows, cols));
        squares = new JToggleButton[rows][cols];
        buildButtons();
    }

    private void buildButtons() {
        // loops are used for creating the "buttons" on the grid.
        int MinesNeeded = 10;
        // builds buttons
        // ----------------------------------------
        for (i = 0; i < rows; i++) {
            for (j = 0; j < cols; j++) {
                squares[i][j] = new JToggleButton();
                // squares[i][j].setEnabled(false);
                squares[i][j].setSize(600, 600);

                // --------------------------------------------------
                // This part randomises the mines
                // -----------------------------------------------------
                while (MinesNeeded > 0) {

                    int x = (int) Math.floor(Math.random() * rows);
                    int y = (int) Math.floor(Math.random() * cols);
                    if (!setmine[x][y]) {
                        setmine[x][y] = true;

                        MinesNeeded--;
                    }
                }
                // ----------------------------------------------------------------------------

                this.add(squares[i][j]);

                if (setmine[i][j] == true) {

                    squares[i][j].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                            // this is the problem
                            squares[i][j].setText("0");
                        }
                    });
                }
            }
        }
    }

    public static void main(String[] args) {
        Grid g = new Grid();
        JFrame frame = new JFrame("Minesweeper");

        frame.add(g);
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The Attempt at a Solution

 
Physics news on Phys.org
  • #2
I think the problem is that when actionPerformed is called, you have already completely gone through your loops, and so i=j=10, and of course, squares[10][10] will throw an index out of range exception. Instead of using squares[j] in that method, try utilizing the method parameter ae in order to set the text on the correct button.
 
  • #3
gabbagabbahey said:
I think the problem is that when actionPerformed is called, you have already completely gone through your loops, and so i=j=10, and of course, squares[10][10] will throw an index out of range exception. Instead of using squares[j] in that method, try utilizing the method parameter ae in order to set the text on the correct button.


Thank you for the swift reply. I am almost 100% sure that is the problem. Could you guide me on how to use the method parameter to set the text? (I don't understand what the method parameter does in Action Listener ( I used ActionListener for the first time yersterday))
 
  • #4
Nevermind, I got it to work. Thanks a lot
 
  • #5


I would approach this problem by first understanding the basic concepts of Java GUI and how to manipulate components such as JButtons. From the provided code, it seems that the programmer has successfully created a grid of JButtons and randomized the placement of mines. However, the issue lies in changing the text of a specific JButton once it is clicked.

To solve this problem, I would suggest using an ActionListener for each JButton that will be responsible for changing its text. In the provided code, the ActionListener is only added for JButtons that contain a mine, but it should be added for all JButtons. This way, when a JButton is clicked, its ActionListener will be triggered and the text can be changed accordingly.

Additionally, instead of using a 2D array to store the JButtons, I would recommend using a 2D array of JButton objects. This way, each JButton can be accessed and manipulated easily using its coordinates in the grid.

Furthermore, I would also suggest using a separate method to check for the presence of mines and display the appropriate number on the JButton. This method can be called from within the ActionListener and can take in the coordinates of the JButton as parameters.

Overall, my approach would be to break down the problem into smaller, manageable tasks and use proper programming techniques to solve them. With a thorough understanding of Java GUI and proper implementation, I am confident that the programmer can successfully display the mines as "0" on the specific JButton they want.
 

1. How can I change the text of a JButton once it's pressed?

To change the text of a JButton once it's pressed, you can use the setText() method. This method takes in a string as a parameter and updates the text of the button.

2. Can I change the text of a JButton without pressing it?

Yes, you can change the text of a JButton without pressing it. You can use the setText() method in your code to update the text of the button at any time.

3. How can I change the font of the text in a JButton?

To change the font of the text in a JButton, you can use the setFont() method. This method takes in a Font object as a parameter, which can be created using the Font class.

4. Is it possible to change the color of the text in a JButton?

Yes, it is possible to change the color of the text in a JButton. You can use the setForeground() method, which takes in a Color object as a parameter, to change the color of the text.

5. How can I change the size of a JButton?

To change the size of a JButton, you can use the setSize() method. This method takes in two integer parameters, representing the width and height of the button, respectively. You can also use the setPreferredSize() method to set a preferred size for the button.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
842
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top