从另一个打开java应用程序

时间:2013-11-15 01:04:51

标签: java swing netbeans call

我需要你的帮助。我有2个Java应用程序。我有一个要从另一个调用。即应用程序“A”按下打开我的应用程序“B”的按钮 有人知道你怎么做到这一点?

PS:这两个应用程序都是用Java编写的,用netbeans开发。

3 个答案:

答案 0 :(得分:0)

您可以让一个包含另一个并将其中包含的一个设置为false,visibilty,然后更改可见性。像这样的东西

public class AFrame extends JFrame {
     private JButton jbt = new JButton("Open Window");
     private BFrame jfrm = new BFrame();

    public class AFrame(){
        add(jbt);
        jfrm.setVisibile(false);
        add(jfrm);

        jbt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                jfrm.setVisibile(true);
            }
        });
    }
}


public class BFrame extends JFrame {

    public BFrame(){

   }

}

按下“打开窗口”按钮时,BFrame设置为可见,然后出现。 AFrame是起始程序,其中包含BFrame

答案 1 :(得分:0)

尝试使用Desktop.getDesktop().open()功能

try {
    Desktop.getDesktop().open(new File("PATH-TO-YOUR-APPLICATION-JAR\\yourapplication.jar"));
} catch (IOException ex) {
    System.out.println(ex.getMessage());
}

或者将jar文件放在类路径中并调用其main()方法。

参考:

  1. Launch External Jar through Java Application
  2. Running an external jar file by pressing a JButton

答案 2 :(得分:0)

你可以使用 dispose()方法和程序A 的框架对象来关闭程序A和 setDefaultCloseOperation 到HIDE_ON_CLOSE,因为如果你将它设置为exit_on_close它将终止,你还需要在dispose()之前调用程序B的构造函数或方法来启动B程序

//in the method were you want to stop the first program and start second program
AFrame.setDefaultCloseOperation(AFrame.HIDE_ON_CLOSE);
new ProgramB(); //calling constructor of class B
AFrame.dispose();
相关问题