[英]In Java, how do I find the proper path to open a txt file that's name is provided as a command line arguement?
[英]How do I pass file path to command line arguements when using “open with” in Windows?
我是新來的,所以不確定自己做錯了什么,所以請告訴我我是否在。
我正在用Java創建一個簡單的圖像/ gif查看器。 我希望能夠使用提取的應用程序(該應用程序將與launch4j包裝在.exe中)來打開圖像文件。 選擇要打開的圖像時,我希望它獲取圖像的文件路徑並將其傳遞給args [0],以便應用程序可以使用它。
我感覺到這是需要在Java代碼之外完成的事情。 也許在我正在使用的安裝程序或包裝程序中,但我不知道該怎么做。
我一直在搜索過去3小時無濟於事。 任何幫助表示贊賞。 到目前為止,我的代碼:
public class MainWindow {
private JFrame frame1;
public static String imageLocation = null;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
e.printStackTrace();
}
try {
imageLocation = args[0];
} catch (Exception e1) {
e1.printStackTrace();
System.exit(0);
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame1.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainWindow() {
initialize();
}
private void initialize() {
BufferedImage img = null;
try{
img = ImageIO.read(new File(imageLocation));
}catch (IOException e) {
e.printStackTrace();
}
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();
frame1 = new JFrame();
frame1.getContentPane().setBackground(Color.WHITE);
frame1.setResizable(false);
frame1.setTitle(imageLocation);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.getContentPane().setLayout(new BorderLayout(0, 0));
frame1.setBounds(200, 200, imgWidth+18, imgHeight+40);
JLabel imgLabel = new JLabel("");
imgLabel.setToolTipText(imageLocation);
imgLabel.setHorizontalAlignment(SwingConstants.CENTER);
imgLabel.setIcon(new ImageIcon(imageLocation));
imgLabel.setBounds(0, 0, frame1.getWidth(), frame1.getWidth());
frame1.getContentPane().add(imgLabel);
}
}
提前致謝。
我看不到任何要上傳文件的表單元素。 不是JForms的專家,但我相信您需要一個文件上傳控件,該控件會將其值傳遞給代碼中的args數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.