Java 将ActionEvent添加到许多JButton

Java 将ActionEvent添加到许多JButton,java,swing,Java,Swing,我想将ActionEvent添加到81个JButtons中如何尽可能少地使用代码?我尝试过使用循环来完成任务,但是程序本身有问题。下面是代码的一部分,我认为它足以从中得到答案 public void actionPerformed(ActionEvent e) { if(e.getSource()==tiles[0]) { n=0; x=1; y=1; detectMines(); setProper

我想将ActionEvent添加到81个JButtons中如何尽可能少地使用代码?我尝试过使用循环来完成任务,但是程序本身有问题。下面是代码的一部分,我认为它足以从中得到答案

    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);
     }
     }
 }

对不起,代码墙。

像这样的东西怎么样:

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;
      }
    }
}
public void actionPerformed(ActionEvent e){
对于(int i=0;i<??;i++){
如果(e.getSource()==tiles[i]){
n=i;
x=1;
y=i+1;
detectMines();
集合属性(n,x,y);
打破
}
}
}

其中,
??
将替换为您的瓷砖总数。或者如@ BrANO88所建议的,考虑使用上面所示的类似代码来创建一个循环中的按钮。

这样的事情是怎样的:

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;
      }
    }
}
public void actionPerformed(ActionEvent e){
对于(int i=0;i<??;i++){
如果(e.getSource()==tiles[i]){
n=i;
x=1;
y=i+1;
detectMines();
集合属性(n,x,y);
打破
}
}
}

其中,
??
将替换为您的瓷砖总数。或者如@ BrANO88所建议的,考虑使用上面所示的类似代码来创建循环中的按钮。

< P>希望对您有帮助:

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.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共级扫雷舰{
JPanel面板=新的JPanel();
JButton[]按钮=新JButton[81];
公共扫雷艇(){
面板设置布局(新网格布局(9,9));

对于(int i=0;i我希望这将帮助您:

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.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共级扫雷舰{
JPanel面板=新的JPanel();
JButton[]按钮=新JButton[81];
公共扫雷艇(){
面板设置布局(新网格布局(9,9));

对于(int i=0;i哪个屏幕可以容纳81个按钮?@Nambari我从上下文中假设这些是代表扫雷舰中正方形的小按钮。如何创建按钮数组,并通过for循环添加它们,在每个步骤中,您可以为特定按钮添加动作侦听器。哪个屏幕可以容纳81个按钮?@Nambari我从t中假设他解释说,这些是代表扫雷舰中正方形的小按钮。如何创建按钮数组,并通过for循环添加它们,在每个步骤中,您可以为特定的按钮添加动作侦听器。我想我已经完成了上面的代码,但我对结果不满意。谢谢您的帮助。我想我已经完成了上面的代码,但我对结果不满意,谢谢你的帮助。我认为这是迄今为止最好的答案。我认为这是迄今为止最好的答案。