Open up windows explorer in java

*爱你&永不变心* 提交于 2019-12-10 04:30:24

问题


I have been looking for an answer to this on Stack Overflow, but I couldn't find an answer that worked for me.

Using Java, how do I create a button that will launch an Explorer Window to a specified directory? If this is possible, how do I make it work for OSX and Linux?


回答1:


I am not sure how it works in other OS but in Windows you can use something like this

Desktop.getDesktop().open(new File("c:\\"));

Edit

Found another way (check link to FileExplorer class from that answer). Also you can use System.getProperty("os.name") to determine operation system.




回答2:


javax.swing.JButton myButton = new javax.swing.JButton("BUTTON TEXT");
myButton.addActionListener(new java.awt.event.ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
    java.awt.Desktop.getDesktop().open(new java.io.File("MY PATH NAME HERE"));
  }
});


来源:https://stackoverflow.com/questions/11174055/open-up-windows-explorer-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!