我正在尝试将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);
}
}
以下是调用按钮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
设计模式。
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:0)
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);
}
如果您希望Panel与&#34;分开&#34;从控制器(即MVC)开始,您必须跟踪Panel的状态。而不是只存储一个按钮 - 您需要一组按钮:
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);
}
}