【问题标题】:Access buttons that were created in a loop访问在循环中创建的按钮
【发布时间】:2017-07-31 19:18:52
【问题描述】:

我创建了一个带有许多按钮的板,例如900+。由于这是很多工作,我想我会循环进行。但是现在我想为每个按钮添加一个 ActionListener,这些按钮将为第 i 行和第 j 列中的按钮提供 arrary number[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){

                            }
            });
       }
    };
}

}

【问题讨论】:

  • 在字段中声明JButton[][] button而不是构造函数

标签: java swing loops button actionlistener


【解决方案1】:

看看这段代码:

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

你有你自己的按钮类来保存它的位置变量和一个单一的动作监听器,它向事件源(你的按钮)询问他的位置并将它保存在数字数组中。

您还可以完全删除数字数组,并为您的按钮提供一个“已选择”变量。

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多