[英]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.