[英]Trying to use Java Swing and Filechooser but there is an error in my code
我有一個帶有 GUI 的 java 程序,如下所示。 它的作用是由一個按鈕組成,該按鈕允許用戶選擇文件,並在選擇文件時打開命令提示符並切換到該文件的目錄。
但是,此代碼不會打開命令提示符。 如何修改代碼以便更改到所選文件的目錄?
public class GUIProject {
public static void main(String[] args) {
JFrame f=new JFrame();
//final JTextField tf=new JTextField();
JTextArea File1= new JTextArea("Select the program to test.");
JTextArea File2= new JTextArea();
File1.setBounds(50,50, 150,20);
JButton b=new JButton("Select");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
File1();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
f.add(b);
f.add(File1);
f.add(File2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void File1() throws IOException{
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"selectedFile.getAbsolutePath()\" && dir");
builder.redirectErrorStream(true);
Process p = builder.start();
}
}
}
更新 -
public static void File2() throws IOException{
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
String[] commands = {"cd selectedFile.getAbsolutePath()", "mvn clean test -DskipTests -Dmaven.compiler.source=1.8 -Dmaven.compiler.target=1.8 -B -f C:\\Users\\A\\Documents\\GitHub\\ThisNew\\flacoco\\examples\\exampleFL1\\FLtest1", "java -jar target/flacoco-1.0.6-SNAPSHOT-jar-with-dependencies.jar --projectpath examples\\exampleFL1\\FLtest1 --output finalresult.csv --format CSV"};
String command = "cmd.exe /c " + String.join(" && ", commands);
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.inheritIO(); //to see the result in the console
pb.start();
}
}
使用我在下面編寫的這段代碼,您可以打開 cmd 並將其設置在所需的目錄中。
import java.io.File;
import java.io.IOException;
public class main {
public static void main( String[] args ) {
try{
String command = "ping www.google.com";
//remove "\"start;" to hide cmd
String[] cmd = new String[]{ "cmd.exe", "/C", "\"start;" + command + "\"" };
File directory = new File( "C:/Users" );
ProcessBuilder pb = new ProcessBuilder( cmd );
pb.directory( directory );
Process process = pb.start();
}catch( IOException e ){
System.out.println( "Something has gone wrong" );
e.printStackTrace();
}
}
}
讓我知道它是否有幫助。
如果要運行多個命令,可以將它們與&&
放在一起,如下所示:
cd "C:/users/" && echo hello world && echo I will be executed after
。
其輸出將是:
hello world
I will be executed after
C:\Users>
在 Java 中,這可能看起來像:
String[] commands = {"cd C:/users/", "echo Hello world", "echo I will be executed after"};
String command = "cmd.exe /c " + String.join(" && ", commands);
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.inheritIO(); //to see the result in the console
pb.start();
流程構建器不應打開新窗口。 默認情況下,它在后台執行此操作。 您可以使用參數/c start
來欺騙它打開一個 cmd 窗口。 添加另一個參數來設置新窗口的位置: cmd /c start /D "C:/users/"
。
使用流程構建器可以如下所示:
String command = "cmd /c start /D \"C:/users/\"";
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.