cha*_*ama 9 java jfilechooser look-and-feel
我正在尝试生成JFileChooser具有Windows外观的内容.我找不到一个方法来改变它,所以我创建了一个扩展的基类,JFileChooser用以下代码更改UI:
public FileChooser(){
this(null);
}
public FileChooser(String path){
super(path);
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Run Code Online (Sandbox Code Playgroud)
然后,在另一节课中,我打电话
FileChooser chooser = new FileChooser(fileName);
int val = chooser.showOpenDialog(null);
Run Code Online (Sandbox Code Playgroud)
但是出现的对话框具有Java外观.有关如何改变这一点的任何想法?是否有一个JFileChooser类的方法,我可以使用它而不是这个扩展类?
谢谢!
小智 13
我知道你可以设置整个应用程序的外观和感觉,但是如果你喜欢跨平台的外观和感觉但是想要JFileChoosers的系统外观,你会怎么做.特别是因为跨平台甚至没有正确的文件图标(看起来非常俗气).
这就是我做的.这绝对是一个黑客......
public class JSystemFileChooser extends JFileChooser{
public void updateUI(){
LookAndFeel old = UIManager.getLookAndFeel();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Throwable ex) {
old = null;
}
super.updateUI();
if(old != null){
FilePane filePane = findFilePane(this);
filePane.setViewType(FilePane.VIEWTYPE_DETAILS);
filePane.setViewType(FilePane.VIEWTYPE_LIST);
Color background = UIManager.getColor("Label.background");
setBackground(background);
setOpaque(true);
try {
UIManager.setLookAndFeel(old);
}
catch (UnsupportedLookAndFeelException ignored) {} // shouldn't get here
}
}
private static FilePane findFilePane(Container parent){
for(Component comp: parent.getComponents()){
if(FilePane.class.isInstance(comp)){
return (FilePane)comp;
}
if(comp instanceof Container){
Container cont = (Container)comp;
if(cont.getComponentCount() > 0){
FilePane found = findFilePane(cont);
if (found != null) {
return found;
}
}
}
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不需要更改外观,可以尝试将UIManager.setLookAndFeel(..)行放在入门类的main方法中吗?
这似乎对我有用,虽然我不知道为什么它不能像你设置它那样工作.