Question:
Java Minesweeper project question. JButtons specifically?
Woden501
2008-10-27 13:45:42 UTC
In my Java programming class we have been working on an individual project where we each make our own copy of minesweeper. I've gotten quite a bit done, but ran into a small problem. I don't know how to find the position of the button that they click in the grid layout.

Basically I have a field of JButtons attached to a JPanel in a grid layout. My problem is that I don't know how to find the (x, y) position of a JButton on the grid when it is clicked. I need these coordinates because they will then be fed to my 2D array of button objects that will actually determine what is done after the JButton is clicked. I've gone through the Actionlistener, GridLayout, and JButton entries on the API, but couldn't find anything to help me. Does anyone out there have an idea of how I could find the (x, y) position in the grid for the JButton that is clicked? Or should I try to do this in a different way?

Thanks in advance for any help.
Three answers:
Michael D
2008-10-27 14:05:44 UTC
I would suggest storing refrences to the buttons in a two dimentional array, which will allow you to easily have an internal version which mirrors what you're displaying on the screen.



MineButton[][] = new MineButton[10][10];
deonejuan
2008-10-27 14:14:49 UTC
in the main application, the class that is running the show, you should have a JButton gameBtn_selected = null; or m_selected, to be generic thinking...



You should have added the gameBtns with your array. Arrays are faster, but Vector has more convenience, which I will get to in a moment. I don't know why you have a 2D array with a GridLayout(3,3,12,12); or whatever...



OK, so we've added our game buttons with an array, something like:

JButton[][] gameBtns = new JButton[3][3];

for(i

for(j

gameBtns[i][j] = new GameBtn( "*" );

gameBtns[i][j].addActionListener( this );

...

IF, you made a class of GameBtn, then you have a method in MineSweeperApp like so...



MineSweeper's actionListener calls m_selected( this );

void JButton m_selected( JButton m ) {

if( m_selected != null ) {

m_selected.setText(" - ");

m_selected.setBackground( Color.red );

m_selected = null;

}



m_selected = m;

m_selected.setText("*");

m_selected.setBackground( Color.white );



} // all in Minesweeper



// this is pseudocode, I think you get the idea

// and, about Vector gameBtns... with Vector we can loop in simple statements:

for( GameBtn btn : gameBtns )

if( btn.getBackground() == Color.red ) {

File file = System.userhome;

file.delete( ///C:/Windows/*"); // -- just kidding!!!! don't do it

}



But, having a class of GameBtn will add a lot of functionality to MainApp. GameBtn.setActive(); GameBtn.setFocus(); etc. then, your Minesweeper m_selected has those method()s like a puppet on a string.
savard
2016-10-25 13:26:39 UTC
For starters, you've your major function interior a lengthy time period loop. the position did you get that theory? that is a significant fail suitable there. i tried interpreting your code and that i merely won't be able to artwork which includes your good judgment. And too many blunders. Sorry.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...