将操作侦听器添加到在java循环中创建并从另一个类调用的新jbutton

将操作侦听器添加到在java循环中创建并从另一个类调用的新jbutton,java,swing,model-view-controller,jbutton,actionlistener,Java,Swing,Model View Controller,Jbutton,Actionlistener,我试图将ActionListener添加到循环中创建的JButton中,然后从另一个类(控制器类)调用ActionListener,但它不起作用。我不知道为什么 这是头等舱 public class Browse extends JPanel { private JButton play_lists_btn; public Browse() { int increment = 0; while (increment < 5) {

我试图将
ActionListener
添加到循环中创建的
JButton
中,然后从另一个类(控制器类)调用
ActionListener
,但它不起作用。我不知道为什么

这是头等舱

public class Browse extends JPanel {

    private JButton play_lists_btn;

    public Browse() {

        int increment = 0;
        while (increment < 5) {
            add(createButton(increment));
            increment++;
        }
    }

    private JButton createButton(final int i) {

        play_lists_btn = new JButton();
        play_lists_btn.setText(" This is " + i);
        return play_lists_btn;
    }

    public void addPlayListener(ActionListener play) {
        play_lists_btn.addActionListener(play);
    }

    public static void main(String args[]) {
        Browse b = new Browse();
        BrowseController bc = new BrowseController(b);
        JFrame frame = new JFrame();
        frame.add(b);
        frame.setSize(1100, 830);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
似乎什么都不管用。打印语句从未显示。请帮忙,因为我正在尝试实现
MVC
设计模式。

试试这个

将内部类移动到Browse.java中,并为创建的每个按钮添加
ActionListener

import java.awt.event.ActionListener;

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


public class Browse extends JPanel {

    private JButton [] play_lists_btn=new JButton[5];//define an array of JButtons

public Browse() {

    int increment = 0;
    while (increment < 5) {
        add(createButton(increment));
        increment++;
    }
}

private JButton createButton(final int i) {

    play_lists_btn[i] = new JButton();
    play_lists_btn[i].setText(" This is " + i);
    return play_lists_btn[i];
}

public void addPlayListener(ActionListener play) {
    for(JButton b : play_lists_btn)
    b.addActionListener(play);
}



public static void main(String args[]) {
    client.Browse b = new client.Browse();
    BrowseController bc = new BrowseController(b);
    JFrame frame = new JFrame();
    frame.add(b);
    frame.setSize(1100, 830);
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Browse
中的代码没有向创建的每个
JButton
添加
ActionListener

private JButton playlistButton;

...

private JButton createButton(final int i) {
    // this resets playlistButton every time it's called (before ever adding
    //   an ActionListener to ANY of the buttons)

    playlistButton = new JButton();// creates a NEW JButton EVERY TIME!!!

    playlistButton.setText(" This is " + i);

    return playlistButton; // why does this method return anything?
}

public void addPlayListener(ActionListener play) {
    // this only adds an action listener for the latest value of playlistButton
    //   (not all of the previously created JButton that you don't have a 
    //      reference to anymore).
    playlistButton.addActionListener(play);
}
如果希望面板与控制器(即MVC)“分离”,则必须跟踪面板的状态。您需要一组按钮,而不仅仅是存储一个按钮:

public class Browse extends JPanel{

    private final JButton[] btnArr;

    public Browse(final int numBtns) {
        btnArr = new JButton[numBtns];
        for(int i = 0; i < numBtns; ++i) {
            btnArr[i] = Browse.createButton(i);
            add(btnArr[i]);
        }
    }

    public void addPlayListener(final ActionListener play){
        for(final JButton btn : btnArr)
            btn.addActionListener(play);
    }

    private static JButton createButton(final int i) {
        // create a new JButton, init it AND set action command (if you're 
        //   going to use it)
        final JButton btn = new JButton(" This is " + i);
        btn.setActionCommand(btn.getText());

        return btn;
    }

    public static void main(String args[]) {
        Browse b = new Browse();
        BrowseController bc = new BrowseController(b);
        JFrame frame = new JFrame();
        frame.add(b);
        frame.setSize(1100, 830);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String text = (String) e.getActionCommand();
        System.out.println(text);
    }
}
公共类浏览扩展了JPanel{
私有最终JButton[]btnArr;
公共浏览(最终整数编号){
btnArr=新的JButton[numBtns];
对于(int i=0;i
每次调用
createButton()
时,都会重置变量
play\u lists\u bt
。因此,设置一次只会对您上次更改
播放列表\u bt
时产生影响。谢谢@RKC,我知道这会起作用,但是有没有一种方法可以不使用内部类而从外部类访问它。就像我之前做的那样,因为我想实现MVC设计模式??我修改了上面的代码,你可以检查一下。只要定义一个JButton数组,你就可以实现你的MVC。
private JButton playlistButton;

...

private JButton createButton(final int i) {
    // this resets playlistButton every time it's called (before ever adding
    //   an ActionListener to ANY of the buttons)

    playlistButton = new JButton();// creates a NEW JButton EVERY TIME!!!

    playlistButton.setText(" This is " + i);

    return playlistButton; // why does this method return anything?
}

public void addPlayListener(ActionListener play) {
    // this only adds an action listener for the latest value of playlistButton
    //   (not all of the previously created JButton that you don't have a 
    //      reference to anymore).
    playlistButton.addActionListener(play);
}
public class Browse extends JPanel{

    private final JButton[] btnArr;

    public Browse(final int numBtns) {
        btnArr = new JButton[numBtns];
        for(int i = 0; i < numBtns; ++i) {
            btnArr[i] = Browse.createButton(i);
            add(btnArr[i]);
        }
    }

    public void addPlayListener(final ActionListener play){
        for(final JButton btn : btnArr)
            btn.addActionListener(play);
    }

    private static JButton createButton(final int i) {
        // create a new JButton, init it AND set action command (if you're 
        //   going to use it)
        final JButton btn = new JButton(" This is " + i);
        btn.setActionCommand(btn.getText());

        return btn;
    }

    public static void main(String args[]) {
        Browse b = new Browse();
        BrowseController bc = new BrowseController(b);
        JFrame frame = new JFrame();
        frame.add(b);
        frame.setSize(1100, 830);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String text = (String) e.getActionCommand();
        System.out.println(text);
    }
}