加入行动听众在Java中的回路形成,并从另一个类称为新的JButton(adding action

2019-10-19 19:30发布

我想一个添加ActionListenerJButton中的回路形成,然后调用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);
    }
}

这里是调用按钮控制器类ActionListener ,并创建一个ActionEvent的按钮

public class BrowseController {

    private Browse b;

    public BrowseController(Browse b) {
        this.b = b;
        b.addPlayListener(new PlayListener());
    }

    private class PlayListener implements ActionListener {

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

似乎没有任何工作。 print语句从未露面。 请帮助,因为我想实现MVC设计模式。

Answer 1:

尝试这个,

将你的内部类中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);
}
}

BrowseController.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class BrowseController {

private Browse b;

public BrowseController(Browse b) {
    this.b = b;
    b.addPlayListener(new PlayListener());
}

    private class PlayListener implements ActionListener {

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

}


Answer 2:

在代码Browse不加入一个ActionListener每个JButton创建。

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


文章来源: adding action listeners to a new jbutton created in a loop in java and called from another class