2017-10-25 15 views
0

誰かがJFrameをどのようにして外部のウィンドウを閉じることができるのか分かっていたのですが、たとえば、.batファイルです。私のGUIウィンドウは次のようになります (https://cdn.discordapp.com/attachments/339245512647770112/372569903070183425/unknown.png) 「実行」ボタンで開いたファイルを「閉じる」と言う場所にしたいと思います。JFrameを外部ウィンドウで閉じることができますか?

 btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20)); 
    btnNewButton.setBounds(375, 190, 120, 40); 
    frame.getContentPane().add(btnNewButton); 

    JButton btnStart = new JButton("Run"); 
    btnStart.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     } 
    }); 
    btnStart.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      try { 
       Desktop.getDesktop().open(new File("C:\\Users\\User\\Desktop\\Discord-Selfbot-master\\self-bot.bat")); 
      } catch (IOException ex) { 
       System.out.println(ex.getMessage()); 
      } 
     } 
    }); 

同じコードまたは類似のコードを使用してファイルを閉じることはできますか?これについての情報があれば、私はそれを聞いてうれしいです。

ありがとうございます。 -Brand0n

+0

のInvokeウィンドウがJavaで '' tasklist'とtaskkill'コマンドの完全な作業コードのですか? 'Desktop.open()'を使うと別のプロセスをフォークし、OSコマンドで強制終了することしかできません。 – Alex

+0

これを使って別のプログラム/ファイルを終了することができますか? –

+0

確かに、これは_Task Manager_で_end process_をクリックしたのとまったく同じです。 – Alex

答えて

2

ProcessBuilderを使用してプログラムを開始することができます。返されたプロセスを使用して、プログラムを終了することができます。ここで

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.concurrent.TimeUnit; 

import javax.swing.*; 

public class TestFrame { 

public static void testWithoutUI(String s[]) { 
    ProgramRunner programRunner = new ProgramRunner("notepad.exe"); 
    programRunner.start(); 

    System.out.println("waiting"); 
    try { 
     TimeUnit.SECONDS.sleep(10); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 

    programRunner.getProcess().destroyForcibly(); 

    System.out.println("done"); 

} 

public static void main(String s[]) { 

    JFrame frame = new JFrame("JFrame Example"); 

    JPanel panel = new JPanel(); 
    panel.setLayout(new FlowLayout()); 

    JLabel label = new JLabel("Self-Bot"); 


    ProgramRunner programRunner = new ProgramRunner("notepad.exe");//C:\\Users\\User\\Desktop\\Discord-Selfbot-master\\self-bot.bat 



    JButton btnStart = new JButton("Run"); 
    btnStart.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     } 
    }); 
    btnStart.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      programRunner.start(); 
     } 
    }); 

    JButton buttonClose = new JButton(); 
    buttonClose.setText("close"); 

    buttonClose.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent e) { 
      programRunner.getProcess().destroyForcibly(); 
     } 
    }); 

    panel.add(label); 
    panel.add(btnStart); 
    panel.add(buttonClose); 

    frame.add(panel); 
    frame.setSize(300, 300); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 
} 



class ProgramRunner extends Thread{ 
private String pathToFile = null; 
private Process process = null; 

public ProgramRunner(String pathToFile) { 
    this.pathToFile = pathToFile; 
} 

@Override 
public void run() { 
    try { 
     startProgram(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void startProgram() throws IOException { 
    process = new ProcessBuilder(pathToFile).start(); 
    InputStream is = process.getInputStream(); 
    InputStreamReader isr = new InputStreamReader(is); 
    BufferedReader br = new BufferedReader(isr); 
    String line; 
    while ((line = br.readLine()) != null) { 
     System.out.println(line); 
    } 
} 

public Process getProcess() { 
    return process; 
    } 
} 
関連する問題