将动作侦听器添加到在Java循环中创建并从另一个类调用的新jbutton中 [英] adding action listeners to a new jbutton created in a loop in java and called from another class
问题描述
我试图在循环中创建的JButton
中添加ActionListener
,然后从另一个类(控制器类)调用ActionListener
,但是它不起作用.我不知道为什么.
I am trying to add an ActionListener
to a JButton
created in a loop, then call the ActionListener
from another class (the controller class), but its not working. I don't know why.
这是头等舱
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
的控制器类
Here is the controller class that calls the button ActionListener
, and creates an ActionEvent
for the button
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);
}
}
}
似乎没有任何作用.打印语句永远不会出现.请帮忙,因为我正在尝试实现MVC
设计模式.
Nothing seems to work. the print statement never show up. please help, because I am trying to achieve the MVC
design pattern.
推荐答案
尝试一下,
将内部类移动到Browse.java中,并为每个创建的每个按钮添加ActionListener
Move your inner class inside Browse.java and add ActionListener
for each and every button created
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
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);
}
}
}
这篇关于将动作侦听器添加到在Java循环中创建并从另一个类调用的新jbutton中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!