我想将ActionEvent添加到81个JButton中,如何使用更少的代码来做到这一点?我尝试使用循环来完成工作,但是程序本身存在问题。这是我认为足以从中获得答案的部分代码。
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);
}
您知道其余的内容,这是我在上面的代码中使用的功能,也许这将有助于解决我的问题。
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);
}
}
}
对不起,代码墙。
最佳答案
我希望这能帮到您:
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);
}
});
}
}
关于java - 将ActionEvent添加到许多JButton中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13310851/