繁体   English   中英

另一个类中的动作侦听器 - java

[英]action listener in another class - 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);

                            }

                        }

}

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

我该怎么办?

谢谢

不仅可以将这两者分开,而且还推荐使用(请参阅 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);

回答:“我的问题是动作监听器有很多像按钮一样的摆动变量,所以,当我换到另一个班级时,我遇到了问题”

您的操作侦听器类可以有一个构造函数,该构造函数采用视图类类型的参数:

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

这有点离题,但您绝对不应该使用==运算符来比较String s,就像您在这一行中所做的那样:

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。

是的,这是可以做到的。 这很简单; 在一个类中你有你的按钮,在另一个类中你只需要实现一个 ActionListener 并让你的 //cmd 分隔那个按钮的功能。 为此,您需要使用 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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM