另一个类中的动作监听器 – java

它可以有两个类,在一个类似的东西

arrayButtons[i][j].addActionListener(actionListner); 

在另一个

 ActionListener actionListner = new ActionListener() { public void actionPerformed(ActionEvent e) { for (int j = 0; j < arrayButtons.length; j++) { for (int i = 0; i < arrayButtons[j].length; i++) { if (arrayButtons[j][i] == e.getSource()) { if ((gameNumber == 2) && (playHand.getNumberOfCards() == 0)) { if (player[j].getCard(i).getSuit() == Suit.HEARTS.toString() && player[j].hasSuitBesideHearts()) //second game messageOnTable("xxx"); else{ arrayButtons[j][i].setVisible(false); test[j].setIcon(player[j].getCard(i).getImage()); pnCardNumber[j].setText(Integer.toString(player[j].getCard(i).getNumber())); pnCardName[j].setText(player[j].getCard(i).toString()); pnCardSuit[j].setText(player[j].getCard(i).getSuit()); playHand.addCard(player[j].getCard(i), j); player[j].removeCard(i); } } } 

//更多的原因是因为我需要将按钮(swing)与动作监听器分开

我该怎么办?

谢谢

不仅可以将这两者分开,还建议(参见MVC模式 – 它非常关于分离屏幕控件,如按钮和程序的逻辑)

我想到的最简单的方法是编写一个实现ActionListener接口的命名类,如下所示:

 public class SomeActionListener implements ActionListener{ private JTextField textField1; private JComboBox combo1; private JTextField textField2; //... public SomeActionListener(JTextField textField1, JComboBox combo1, JTextField textField2){ this.textField1=textField1; this.combo1=combo1; this.textField2=textField2; //... } public void actionPerformed(ActionEvent e) { //cmd } } 

然后将其添加到按钮:

 ActionListener actionListener = new SomeActionListener(textField1, combo1, textField2); someButton.addActionListener(actionListener); 

要回答:“我的问题是动作监听器有很多像摇摆一样的变量,例如,当我换到另一个class级时,我遇到了问题”

您的动作侦听器类可以有一个构造函数,该构造函数接受视图类类型的参数:

 public class Listener implements ActionListener { private final MyViewClass mView; public Listener(MyViewClass pView) { mView = pView; } public void actionPerformed(ActionEvent e) { // can use mView to get access to your components. mView.get...().doStuff... } } 

然后在你看来:

 Listener l = new Listener(this); button.addActionListener(l); 

您可以通过使用嵌套类轻松地完成它,但我认为最好的方法是将父对象作为参数传递给对象的构造并将其用作动作处理程序;

 //**parent class - Calculator **// public class Calculator extends JFrame implements ActionListener{ private DPanel dPanel; private JTextField resultText; public Calculator(){ // set calc layout this.setLayout(new BorderLayout(1,1)); dPanel = new DPanel(this); // here is the trick ;) } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); resultText.setText(command); // **** your code ****/ } } //**inner class - DPanel**// public class DPanel extends JPanel{ private JButton digitsButton[]; private JButton dotButton,eqButton; public DPanel(Calculator parent){ //layout this.setLayout(new GridLayout(4,3,1,1)); // digits buttons digitsButton = new JButton[10]; for (int i=9;i>=0;i--){ digitsButton[i] = new JButton(i+""); digitsButton[i].addActionListener(parent); // using parent as action handler ;) this.add(digitsButton[i]); } } } 

是的,可以做到。 这很简单; 在一个类中你有你的按钮,在另一个类中你只需要实现一个ActionListener,然后让你的// cmd来分离那个按钮的function。 为此,您需要使用e.getActionCommand()。equals(buttonActionCommand)。 示例代码:

 public class Click implements ActionListener{ public Click( //input params if needed ){ } public void actionPerformed(ActionEvent e) { if( e.getActionCommand().equals(buttonActionCommand){ //cmd } } } 

要在按钮上添加该侦听器,请执行以下操作:

 buttonTest.addActionListener(new Click()); 

这有点偏离主题,但你绝对不应该使用==运算符来比较你在这一行上做的String

 if (player[j].getCard(i).getSuit() == Suit.HEARTS.toString() 

这是因为String是指针,而不是实际值,并且您可能会使用==运算符获得意外行为。 请改用someString.equals(otherString)方法。 并且

 "String to compare".equals(stringVariable) 

比其他方式好多了

 stringVariable.equals("String to compare to") 

因为在第一个示例中,如果stringVariable为null,则避免获取NullPointerException。 它只是返回false。