[英]adding action listeners to a new jbutton created in a loop in java and called from another class
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. 我试图将
ActionListener
添加到在循环中创建的JButton
中,然后从另一个类(控制器类)调用ActionListener
,但是它不起作用。 I don't know why. 我不知道为什么
Here is the first class 这是头等舱
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);
}
}
Here is the controller class that calls the button ActionListener
, and creates an ActionEvent
for the button 这是调用按钮
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);
}
}
}
Nothing seems to work. 似乎没有任何作用。 the print statement never show up.
打印声明永远不会出现。 please help, because I am trying to achieve the
MVC
design pattern. 请帮忙,因为我正在尝试实现
MVC
设计模式。
try this, 尝试这个,
Move your inner class inside Browse.java and add ActionListener
for each and every button created 将内部类移动到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 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);
}
}
}
The code in Browse
isn't adding an ActionListener
to every JButton
created. 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);
}
If you want the Panel to be "separate" from the controller (ie MVC), then you'll have to keep track of the state of the Panel. 如果要使面板与控制器(即MVC)“分离”,则必须跟踪面板的状态。 Instead of just storing a single button--you need an array of buttons:
您不仅需要存储一个按钮,还需要一个按钮数组:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.