Bring control from application to java frame

2019-07-25 02:23发布

I use a java program for communication between a arduino board and a scratch file. The communication happen well. I use a user interface to start the communication where i have buttons called

connect close and minimize

When the user clicks the connect button code will check the value in combo box and accordingly it opens the scratch file.

Once the connect button is clicked the control moves to the scratch application. After completing my work when i tried closing the scratch. My scratch application closes as expected but the control does not return to the user interface because of which i am not able to close the application and i close it in net beans forcefully. In the output screen i don't see build successful and instead i get build stopped. That is my process works perfectly until i give connect but once the button is pressed it is hanged up some where.

I tried making it as a jar file and running it in a different machine at that time i use end task in task manager to close the application.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

if("Disconnect".equals(jButton1.getText()))
{
    System.exit(0);
}

if(jComboBox2.getSelectedItem()==null)
{
    System.out.println("Select one port");
}
else
{           
    Runtime r = Runtime.getRuntime();

    try {
        //this.hide();
        //p = r.exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe     C:\\Users\\Admin\\Desktop\\fwdbckpwm12.sb2");

        p = Runtime.getRuntime().exec("C:\\Program Files\\Scratch 2\\Scratch   2.exe C:\\Users\\Admin\\Desktop\\scratch files new.sb2");

        //Runtime.getRuntime().exec("taskkill /F /IM <p>.exe");            
        //p.destroy();
        //r.exec("C:\\Windows\\notepad.exe C:\\Windows\\ss.txt");
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        A4S a4sObj = new A4S(new String[] {jComboBox2.getSelectedItem().toString()});  //defaultline

        //A4S a4sObj = new A4S(new String[]{"COM16"});  //addedline
        //r.gc();
        //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    } catch (IOException ex) {
        Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
    } 
    finally{
        //p.destroy();
        //System.gc(); 
   // }

}            

Here is the code i tried. But none seems to work.

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-25 02:42
  1. Move all Process related work into separate Thread.
  2. Use waitFor method to recognise Process end - then you are free to exit your app.
查看更多
Lonely孤独者°
3楼-- · 2019-07-25 02:55

As I can understood you used SWING for creating UI. You can set

 yourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

for your frame. This must help.

查看更多
登录 后发表回答