Java 单击按钮时是否可以调用其他类

Java 单击按钮时是否可以调用其他类,java,swing,class,jbutton,Java,Swing,Class,Jbutton,因此,我有一个名为“开始”的窗口,上面有一个JButton。我希望的是,当单击该按钮时,它将运行我已经创建的一个单独的类。这可能吗?如果可能的话,我会怎么做? 谢谢你的帮助 这是我到目前为止所拥有的 JButton start = new JButton ("Play"); frame.add(start); start.addActionListener(//not sure what goes here, i would like to call other class here) 顺便说

因此,我有一个名为“开始”的窗口,上面有一个
JButton
。我希望的是,当单击该按钮时,它将运行我已经创建的一个单独的类。这可能吗?如果可能的话,我会怎么做? 谢谢你的帮助

这是我到目前为止所拥有的

JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(//not sure what goes here, i would like to call other class here)
顺便说一句,被调用的另一个类正在下降,以防发生重要的事情

单击按钮时,您(您的类)可以实现ActionListener来处理

public class YourClass implements ActionListener {

  public YourClass() {
    JButton start = new JButton ("Play");
    frame.add(start);
    start.addActionListener(this);
  }

  public void actionPerformed(ActionEvent arg0) {
    // Call other class
  }

}
您(您的类)可以实现ActionListener,以便在单击按钮时进行处理

public class YourClass implements ActionListener {

  public YourClass() {
    JButton start = new JButton ("Play");
    frame.add(start);
    start.addActionListener(this);
  }

  public void actionPerformed(ActionEvent arg0) {
    // Call other class
  }

}

有很多方法可以做到这一点。如果您想使用另一个已经实现了
ActionListener
的类,那么您完全可以创建该类的对象,然后按如下方式插入:

//Another class that you're already created is named MyActionListener
// and implements ActionListener for this example

MyActionListener mal = new MyActionListener();
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(mal);
但是,这可能没有任何意义,因为您需要引用作为当前类实例成员的变量。在这种情况下,您希望当前类实现如下
ActionListener

class MyClass implements ActionListener {

    public MyClass() {
        JButton start = new JButton ("Play");
        frame.add(start);
        start.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //do whatever you need to do here
    }
}
this
关键字表示您正在引用此类的当前实例。因此,它将使用您在此类中实现的
actionPerformed
方法。您可以在
actionPerformed
方法中实例化另一个类的对象,然后像往常一样对该对象进行任何调用,或者如果该对象是该类的成员,则直接调用该成员的函数。此外,如果您在第二个类中引用静态方法,您也可以直接在
actionPerformed
方法中调用这些方法

还有一个选择,就是使用匿名内部类。如果希望在单个类中执行多个
actionPerformed
方法,而这些方法不是在不同组件的事件处理程序注册之间共享的,则通常使用此方法(即,仅将其用于此单个启动按钮)。下面是如何使用该方法:

JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //do whatever you want here for whenever start is clicked
    }
 });

与其他选项一样,您也可以在这里实例化
actionPerformed
方法中的第二个类,或者从另一个类调用静态方法。但是,如果您计划调用外部类成员变量上的方法(
MyClass
,在本例中),则需要使用此语法
this.MyOtherClass.method()
。原因是,在本例中,
this
关键字为您提供了对匿名内部类内部外部类的访问。

有很多方法可以做到这一点。如果您想使用另一个已经实现了
ActionListener
的类,那么您完全可以创建该类的对象,然后按如下方式插入:

//Another class that you're already created is named MyActionListener
// and implements ActionListener for this example

MyActionListener mal = new MyActionListener();
JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(mal);
但是,这可能没有任何意义,因为您需要引用作为当前类实例成员的变量。在这种情况下,您希望当前类实现如下
ActionListener

class MyClass implements ActionListener {

    public MyClass() {
        JButton start = new JButton ("Play");
        frame.add(start);
        start.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //do whatever you need to do here
    }
}
this
关键字表示您正在引用此类的当前实例。因此,它将使用您在此类中实现的
actionPerformed
方法。您可以在
actionPerformed
方法中实例化另一个类的对象,然后像往常一样对该对象进行任何调用,或者如果该对象是该类的成员,则直接调用该成员的函数。此外,如果您在第二个类中引用静态方法,您也可以直接在
actionPerformed
方法中调用这些方法

还有一个选择,就是使用匿名内部类。如果希望在单个类中执行多个
actionPerformed
方法,而这些方法不是在不同组件的事件处理程序注册之间共享的,则通常使用此方法(即,仅将其用于此单个启动按钮)。下面是如何使用该方法:

JButton start = new JButton ("Play");
frame.add(start);
start.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //do whatever you want here for whenever start is clicked
    }
 });

与其他选项一样,您也可以在这里实例化
actionPerformed
方法中的第二个类,或者从另一个类调用静态方法。但是,如果您计划调用外部类成员变量上的方法(
MyClass
,在本例中),则需要使用此语法
this.MyOtherClass.method()
。原因是本例中的
this
关键字为您提供了访问匿名内部类内部外部类的权限。

您可以发布您到目前为止拥有的内容吗?我刚刚发布了,还有很多内容,但我认为这就是您所需要的一切。基本上是的,但您的“其他”都可以类需要实现
ActionListener
,或者您需要提供一个
ActionListener
能够调用您的“其他”类。考虑一下使用<代码>动作<代码>,你能发布你目前的内容吗?我只是做了很多,但是我认为这就是你所需要的一切。基本上是的,但是你的“其他”。类需要实现
ActionListener
,或者您需要提供一个
ActionListener
能够调用您的“其他”类。请考虑使用<代码>动作< /代码>,Type:构造函数后面的括号应该是卷曲的。