使用 JFileChooser 时显示 windows UI
Having the windows UI display when using JFileChooser
目前正在使用 JFileChooser
将文件保存到用户系统。问题是它用于 select 文件目标的用户界面是丑陋的 Swing UI 而不是 Windows 文件浏览器 UI。 JFileChooser
是否有我可以轻松更改的属性?
下面是我的代码:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(this);
if (userSelection == JFileChooser.APPROVE_OPTION) {
fileToSave = fileChooser.getSelectedFile();
String filePath = fileToSave.getPath();
if(!filePath.toLowerCase().endsWith(".csv"))
{
fileToSave = new File(filePath + ".csv");
}
}
我之前也定义了 fileToSave
,代码一切正常,这纯粹是一个表面问题。
正如 Andrew Thompson 提到的,UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
是您正在寻找的方法。
试试这个:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //set Look and Feel to Windows
JFileChooser fileChooser = new JFileChooser(); //Create a new GUI that will use the current(windows) Look and Feel
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //revert the Look and Feel back to the ugly Swing
// rest of the code...
目前正在使用 JFileChooser
将文件保存到用户系统。问题是它用于 select 文件目标的用户界面是丑陋的 Swing UI 而不是 Windows 文件浏览器 UI。 JFileChooser
是否有我可以轻松更改的属性?
下面是我的代码:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(this);
if (userSelection == JFileChooser.APPROVE_OPTION) {
fileToSave = fileChooser.getSelectedFile();
String filePath = fileToSave.getPath();
if(!filePath.toLowerCase().endsWith(".csv"))
{
fileToSave = new File(filePath + ".csv");
}
}
我之前也定义了 fileToSave
,代码一切正常,这纯粹是一个表面问题。
正如 Andrew Thompson 提到的,UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
是您正在寻找的方法。
试试这个:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //set Look and Feel to Windows
JFileChooser fileChooser = new JFileChooser(); //Create a new GUI that will use the current(windows) Look and Feel
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //revert the Look and Feel back to the ugly Swing
// rest of the code...