Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java Minesweeper project question. JButtons specifically?
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.
I have a bunch of JButtons placed onto a JPanel in a 10*10 grid layout.
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
When I click on one I want to be able to send the x, y position of the button that was clicked to another class.
The purpose is so that I can access an object stored in a 2D array that is stored at the same x, y position.
How could I do this?
3 Answers
- 1 decade agoFavorite Answer
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];
- deonejuanLv 71 decade ago
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<GameBtn> 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<GameBtn> 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.
- ?Lv 45 years ago
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.