I want to add ActionEvent to 81 JButtons how do I do this using less code as possible? I've tried using loops to get the job done but I'm having a problem on the program itself. Here is a part of the code that I think is enough to get answers from.
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tiles[0]) {
n=0;
x=1;
y=1;
detectMines();
setProperties(n,x,y);
}
if(e.getSource()==tiles[1]) {
n=1;
x=1;
y=2;
detectMines();
setProperties(n,x,y);
}
if(e.getSource()==tiles[2]) {
n=2;
x=1;
y=3;
detectMines();
setProperties(n,x,y);
}
if(e.getSource()==tiles[3]) {
n=3;
x=1;
y=4;
detectMines();
setProperties(n,x,y);
}
if(e.getSource()==tiles[4]) {
n=4;
x=1;
y=5;
detectMines();
setProperties(n,x,y);
}
if(e.getSource()==tiles[5]) {
n=5;
x=1;
y=6;
detectMines();
setProperties(n,x,y);
}
You know the rest and here's the function that I use in the code above maybe this will help solve my problems here.
public void setProperties(int n, int x, int y){
if(grid[x][y]=="1") {
slives--;
Icon phpicon = new ImageIcon(getClass().getResource( "resources/"+p1hp[slives] ) );
shp.setIcon(phpicon);
tiles[n].setIcon(icon);
tiles[n].setDisabledIcon(icon);
tiles[n].setEnabled(false);
mines=0;
if(slives==0){
message = "lose";
sendData( message );
JOptionPane.showMessageDialog(null,"You Have No Life Left! You Lose!");
System.exit(0);
}
else{
message = "mines";
sendData( message );
JOptionPane.showMessageDialog(null,"BOOOOOOOOOOM!");
}
}
else if(grid[x][y]=="2") {
clives--;
Icon phpicon = new ImageIcon(getClass().getResource( "resources/"+p2hp[clives] ) );
chp.setIcon(phpicon);
tiles[n].setIcon(powerups);
tiles[n].setDisabledIcon(powerups);
tiles[n].setEnabled(false);
mines=0;
turn--;
message = "powerups";
sendData( message );
JOptionPane.showMessageDialog(null,"Powerups -HP To Enemy!");
if(turn==0){
message = "win";
sendData( message );
JOptionPane.showMessageDialog(null,"You Cleared All The Mines You Win!");
System.exit(0);
}
}
else {
str=Integer.toString(mines);
tiles[n].setText(str);
UIManager.getDefaults().put("Button.disabledText",Color.BLUE);
tiles[n].setEnabled(false);
mines=0;
turn--;
if(turn==0){
message = "win";
sendData( message );
JOptionPane.showMessageDialog(null,"You Cleared All The Mines You Win!");
System.exit(0);
}
}
}
Sorry for the wall of codes.
I hope this will help you:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Minesweeper extends JFrame{
JPanel panel = new JPanel();
JButton [] button = new JButton[81];
public Minesweeper(){
panel.setLayout(new GridLayout(9,9));
for(int i=0;i<81;i++){
button[i] = new JButton(""+i);
button[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if( e.getSource() instanceof JButton) {
((JButton)e.getSource()).setBackground(Color.red);
}
}
});
panel.add(button[i]);
}
add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Minesweeper m = new Minesweeper();
m.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
m.pack();
m.setVisible(true);
}
});
}
}
How about something like this:
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < ??; i++) {
if(e.getSource() == tiles[i]) {
n=i;
x=1;
y=i+1;
detectMines();
setProperties(n,x,y);
break;
}
}
}
where ??
is replaced with your total number of tiles. Or as suggested by @brano88, consider creating the buttons in a loop instead using similar code to that shown above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.