Java:如何让对象侦听器在不同的类中工作?

时间:2014-10-14 08:11:57

标签: java class actionlistener

所以我试图让DriverPC类知道Frame Object中按下按钮的时间,以便它可以使用DoThis()方法,并且不确定如何处理这种情况。

DriverPC类:

public class DriverPC {
    private static void DoThis(){
        //Send information through already set up datastream when button in Frame clicked
    }

    public static void main(String args[]){
    //set up bluetooth datastream
    communications();

    //create gui
    new Frame(200,200,200,200);
    }
}

框架类:

public class Frame extends JFrame implements ActionListener{
    JPanel pane = new JPanel();
    JButton butt = new JButton("Button");   

    Frame(int a,int b,int c, int d){
        super("My Simple Frame");
        setBounds(a,b,c,d);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container con = this.getContentPane();
        butt.addActionListener(this);
        con.add(pane);
        pane.add(butt);
        setVisible(true);       
    }   

    public void  actionPerformed(ActionEvent event){
        Object source = event.getSource();
        if (source == butt){
        }
    }
}

只是想知道是否可能,如果不可以,我应该怎么做呢?

3 个答案:

答案 0 :(得分:0)

首先,您需要将DoThis()方法公开

public static void DoThis(){
    System.out.println("Awesome");
}

然后只需在actionPerformed方法中调用该方法:

public void  actionPerformed(ActionEvent event){
    Object source = event.getSource();
    if (source == butt){
        DriverPC.DoThis();
    }
}

这对我有用! :)

答案 1 :(得分:0)

使用

butt.addActionListener(this);

Frame构造函数中,您将按下按钮的事件委托给方法Frame.actionPerformed

当您的DoThis方法被声明为静态时,您只需在该方法中调用它:

public void  actionPerformed(ActionEvent event){
    Object source = event.getSource();
    if (source == butt) {
        DriverPC.DoThis();
    }
}

为了使这项工作,DoThis方法也必须公开(或者至少包私有,我猜):

public static void DoThis() {
    System.out.println("Awesome");
}

现在,只要按下按钮,输出就会在控制台中打印出来。

顺便说一句:请遵守Java代码约定,并使用小写字母开始方法名称:

public static void doThis() { ... }

答案 2 :(得分:0)

您可以在按钮中添加另一个ActionListener,如下所示:

<强> DriverPC

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DriverPC implements ActionListener {

    public static void main(final String args[]) {
        final Frame frame = new Frame(200, 200, 200, 200);

        frame.addActionListenerToButton(new DriverPC());
        frame.setVisible(true);

    }

    private static void DoThis() {
        System.out.println("Awesome");
    }

    @Override
    public void actionPerformed(final ActionEvent e) {
        DoThis();
    }

}

<强>帧

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Frame extends JFrame implements ActionListener {

    private static final long serialVersionUID = 6585300413498042106L;

    JPanel pane = new JPanel();
    JButton butt = new JButton("Button");

    Frame(final int a, final int b, final int c, final int d) {
        super("My Simple Frame");

        setBounds(a, b, c, d);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Container con = this.getContentPane();

        butt.addActionListener(this);
        con.add(pane);
        pane.add(butt);
    }

    public void addActionListenerToButton(final ActionListener listener) {
        butt.addActionListener(listener);
    }

    @Override
    public void actionPerformed(final ActionEvent event) {
        // ...
    }

}