One implementation of the Minesweeper program would have a grid of JButtons. How
ID: 3595809 • Letter: O
Question
One implementation of the Minesweeper program would have a grid of JButtons. How can we add information for the X and Y position of the grid to be stored as data with each JButton? Also, how can we quickly (in a constant time operation) access the X and Y position when the button is clicked (ignore right clicks, double clicks, etc that might be used with the real Minesweeper program)? While a fully functional program is not needed, specific coding details are expected in your answer. Note: this question is really about inheritance!!
Explanation / Answer
Below are the answers for your questions
1. There is a method called void setText(String s). Through which we can set any specific data to JButton. To get the Text which is there for JButton, we can use getText().
2. You can get X and Y positions of your JButton by using below two methos
3. To get the click count of a particular button, we can use below method
getClickCount()
Below is the sample code of JButton which covers all your requesred parts
JButton button = new JButton();
button.setText("Click Me");
int x = button.getAlignmentX();
int y = button.getAlignmentY();
int clickCount = button.getClickCount();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.