访问在循环中创建的按钮

时间:2017-07-31 19:18:53

标签: java swing loops button actionlistener

我创建了一个带有许多按钮的电路板,例如900+。由于这是很多工作,我以为我会在一个循环中完成它。但是现在我想为每个按钮添加一个ActionListener,它将为行i和列j中的按钮提供arrary号[i] [j]值1.我不知道如何做到这一点没有按钮按钮。我已经期待你的回答了。我的代码在这里:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JButton;


public class MakeMotive {
    int howManyButtonsVer = 34; 
    int howManyButtonsHor = 27;
    int sideLength = 50; 
    int [][] number = new int [howManyButtonsHor+1][howManyButtonsVer+1];
    JButton[][] button = new JButton [howManyButtonsHor+1][howManyButtonsVer+1]; 
    private JFrame frame;
    public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                       MakeMotive window = new MakeMotive();
                       window.frame.setVisible(true);
                  } catch (Exception e) {
                                         e.printStackTrace();
                                         }
        }
    });
}

public MakeMotive(){
    frame = new JFrame("Menu");
    frame.getContentPane().setBackground(Color.yellow);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.getContentPane().setLayout(null);
    frame.setVisible(true);


    for(int i = 0; i<=howManyButtonsHor; i++){
       for(int j = 0; j<=howManyButtonsVer; j++){
           button[i][j] = new JButton();
           button[i][j].setBounds(sideLength*i, sideLength*j, sideLength,sideLength );
           button[i][j].setFont(new Font("Tahoma",Font.PLAIN, 10));
           button[i][j].setBackground(Color.green);
           button[i][j].setText(i+"a"+j);
           frame.getContentPane().add(button[i][j]);
           button[i][j].addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

                            }
            });
       }
    };
}

}

1 个答案:

答案 0 :(得分:0)

看一下这段代码:

import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MakeMotive {
    private static int howManyButtonsVer = 34;
    private static int howManyButtonsHor = 27;
    private static int sideLength = 50;
    private int[][] number = new int[howManyButtonsHor + 1][howManyButtonsVer + 1];
    private JButton[][] button = new JButton[howManyButtonsHor + 1][howManyButtonsVer + 1];
    private JFrame frame;

    private MakeMotive() {
        frame = new JFrame("Menu");
        frame.getContentPane().setBackground(Color.yellow);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setLayout(null);
        frame.setVisible(true);

        MyActionListener myActionListener = new MyActionListener(this); // one action listener
        for (int i = 0; i <= howManyButtonsHor; i++) {
            for (int j = 0; j <= howManyButtonsVer; j++) {
                button[i][j] = new MyButton(i, j); // create your own button, that knows its location
                button[i][j].setBounds(sideLength * i, sideLength * j, sideLength, sideLength);
                button[i][j].setFont(new Font("Tahoma", Font.PLAIN, 10));
                button[i][j].setBackground(Color.green);
                button[i][j].setText(i + "a" + j);
                frame.getContentPane().add(button[i][j]);
                button[i][j].addActionListener(myActionListener); // add the one listener to all buttons
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                MakeMotive window = new MakeMotive();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    private void markValue(int i, int j) {
        number[i][j] = 1;
    }

    private final static class MyButton extends JButton {
        private final int i;
        private final int j;

        MyButton(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }

    private class MyActionListener implements ActionListener {
        private MakeMotive makeMotive;

        MyActionListener(MakeMotive makeMotive) {
            this.makeMotive = makeMotive;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            MyButton source = (MyButton) e.getSource(); // its your own button
            makeMotive.markValue(source.i, source.j);   // get the location and mark the array position
        }
    }
}

您有自己的按钮类可以保存其位置变量,还有一个动作侦听器会向事件源(您的按钮)询问其位置并将其保存在数字数组中。

您也可以完全删除数字数组,并为您的按钮添加变量&#34;选择&#34;。