简体   繁体   English

如何在ActionListener中调用另一个Java文件?

[英]How to call another Java file in ActionListener?

I am creating a graphical interface for my program. 我正在为程序创建图形界面。 When I press the start button, I want it to call another program. 当我按下开始按钮时,我希望它调用另一个程序。

Below is my listerner code: 以下是我的侦听器代码:

private class ButtonHandler implements ActionListener 
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==startButton)
            ?????
    }
}

What should I do? 我该怎么办? I intend to call another Java file (Start.java). 我打算调用另一个Java文件(Start.java)。

If you want to create a new Start instance there, you could do that: 如果要在此处创建新的Start实例,则可以执行以下操作:

public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==startButton) {
       Start myStart = new Start();
       myStart.foo();
    }
}

Or if you already have a reference to an existing Start object, then simply call its methods. 或者,如果您已有对现有Start对象的引用,则只需调用其方法。 A caveat, if any of Start's methods take a long time to complete or are resource hogs, you'll want to do them in a background thread such as supplied by a SwingWorker object. 需要警告的是,如果Start的任何方法花费很长时间才能完成,或者是资源消耗大的话,则需要在后台线程(如SwingWorker对象提供的线程)中进行操作。

Note that my answer is quite general and perhaps a bit vague on the details, but I cannot supply any finer grained details until you tell us a lot more about the structure of your program, your classes, and your specific problem here. 请注意,我的回答是相当笼统的,也许在细节上有些含糊,但是我无法提供任何更细粒度的细节,除非您在这里向我们详细介绍了程序的结构,类以及特定的问题。

You can call the required method of Start class if this belongs to your project and is already included in class path. 如果Start类属于您的项目并且已经包含在类路径中,则可以调用Start类的必需方法。

If you want to launch another process alltogether you have to use http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String ) 如果您想一起启动另一个进程,则必须使用http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String

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

相关问题 如何在另一个类中调用ActionListener中的Java类 - How to call Java class in ActionListener in another class Java NetBeans用另一个调用ActionListener - Java NetBeans call ActionListener with another 如何从一个actionListener返回一个数组列表,然后将其调用到另一个actionListener? - how to return an array-list from a actionListener and then call it to another actionListener? 另一个类中的Java ActionListener - Java ActionListener in another Class Java:使用actionlistener在该类的对象上调用另一个类中的函数 - Java: Using an actionlistener to call a function in another class on an object from that class 如何将另一个类中的变量调用到 ActionListener 方法中? - How to call variables from another class into an ActionListener method? 如何将一个ActionPerformed从一个ActionListener发送到另一个ActionListener? - How to send an ActionPerformed from an ActionListener to another ActionListener? 如何在使用Java2D的GUI中使用ActionListener调用方法 - How to call a method using ActionListener in GUI using Java2D 如何在Java中编写ActionListener - How to write actionlistener in java 如何保存来自actionlistener的输入,然后与Java中的另一个输入进行比较? - How to save input from actionlistener, and then compare to another in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM