简体   繁体   English

动态按钮上的事件侦听器

[英]Event Listeners on Dynamic Buttons

I've been struggling with this for a while, I'm trying to build a program that makes use of the MVC pattern, that dynamically creates a grid of buttons (nxn) based on user input. 我已经为此苦苦挣扎了一段时间,我试图构建一个利用MVC模式的程序,该程序根据用户输入动态创建一个按钮网格(nxn)。 I can't however get it right to attach listeners to them. 但是,我无法正确地将侦听器附加到它们。

EDIT: I meant that I wanted to handle the event inside of the Controller class to conform to the MVC pattern 编辑:我的意思是我想处理Controller类内的事件以符合MVC模式

View 视图

public class AIGameView extends javax.swing.JFrame {

private AIGameModel model;
private JButton[][] btn_arr;

public AIGameView(AIGameModel m) {
    model = m;
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {...}                                 

private void startBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         
    options.setVisible(false);
    int size = Integer.parseInt(size_field.getText());
    model.setSize(size);
    btn_arr = new JButton[size][size];
    GameGUI.setLayout(new java.awt.GridLayout(size, size));
    for(int y = 0; y < size; y++)  {
        for(int x = 0; x < size; x++)  {
            btn_arr[y][x] = new JButton();
            btn_arr[y][x].setBackground(Color.white);
            GameGUI.add(btn_arr[y][x]);
            btn_arr[y][x].setVisible(true);

        }
    }
    GameGUI.setVisible(true);
    GameGUI.revalidate();
    GameGUI.repaint();
}        

Controller 调节器

public class AIGameController {

private AIGameView view;
private AIGameModel model;
private JButton[][] buttons;

public AIGameController(AIGameModel m, AIGameView v)    {
    view = v;
    model = m;
}

I've tried several things, but nothing seems to work for this and I mostly just end up with a null pointer exception. 我已经尝试了几件事,但是似乎没有任何效果,而且我最终只会遇到空指针异常。 Any advice on this ? 有什么建议吗?

From the code that you posted so far, it seems that you could add a call 从到目前为止发布的代码来看,您似乎可以添加呼叫

btn_arr[y][x] = new JButton();
btn_arr[y][x].addActionListener(createActionListener(y, x));
...

With a method like 用类似的方法

private ActionListener createActionListener(final int y, final int x)
{
    return new ActionListener()
    {
        @Override 
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("Clicked "+y+" "+x);

            // Some method that should be called in 
            // response to the button click:
            clickedButton(y,x);
        }
    };
}

private void clickedButton(int y, int x)
{
    // Do whatever has to be done now...
}

When you're going through your initialisation loop : 当您经历初始化循环时:

for(int x = 0; x < size; x++)  {
  btn_arr[y][x] = new JButton();
  btn_arr[y][x].setBackground(Color.white);
  GameGUI.add(btn_arr[y][x]);
  btn_arr[y][x].setVisible(true);
  btn_arr[y][x].addActionListener( new YourListener() );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM