Java JFileChooser 的 Windows 外观
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2282211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows look and feel for JFileChooser
提问by chama
I'm trying to generate a JFileChooser
that has the Windows look-and-feel. I couldn't find a method to change it, so I created a base class that extends JFileChooser
that changes the UI with the following code:
我正在尝试生成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()); }
Then, in another class, I call
然后,在另一个班级,我打电话
FileChooser chooser = new FileChooser(fileName);
int val = chooser.showOpenDialog(null);
but the dialog box that comes up has the Java look and feel. Any thoughts on how to change this? Is there a method of the JFileChooser class that I can use instead of this extended class?
但是出现的对话框具有 Java 外观。关于如何改变这种情况的任何想法?我可以使用 JFileChooser 类的方法来代替这个扩展类吗?
Thank you!
谢谢!
采纳答案by Luhar
If you don't need to change the Look and Feel, could you try putting the UIManager.setLookAndFeel(..) line in the main method of your entry class?
如果您不需要更改外观,您可以尝试将 UIManager.setLookAndFeel(..) 行放在您的入口类的 main 方法中吗?
That seems to work for me, though I am at a loss as to why it won't work the way you have set it upt.
这似乎对我有用,尽管我不知道为什么它不能像您设置的那样工作。
回答by Vincent Ramdhanie
First, try running the code from the command line and specify the look and feel there to see that it can be applied.
首先,尝试从命令行运行代码并在那里指定外观以查看它是否可以应用。
java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel YourApp
If it does apply the correct look and feel then you can add the look and feel code to the program before you create the JFileChooser dialog. Lets say a simple program would look like this:
如果它确实应用了正确的外观,那么您可以在创建 JFileChooser 对话框之前将外观代码添加到程序中。假设一个简单的程序如下所示:
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception e) {
// handle exception
}
JFileChooser chooser = new JFileChooser();
//etc
}
回答by Timothy
The problem is that the Look & Feel was already selected for you when you called super(path)
.
问题是当您调用super(path)
.
From the Java Tutorial for Look and Feel:
来自Java 观感教程:
Note: If you are going to set the L&F, you should do it as the very first step in your application. Otherwise you run the risk of initializing the Java L&F regardless of what L&F you've requested. This can happen inadvertently when a static field references a Swing class, which causes the L&F to be loaded. If no L&F has yet been specified, the default L&F for the JRE is loaded. For Sun's JRE the default is the Java L&F, for Apple's JRE the Apple L&F, and so forth.
注意:如果您要设置 L&F,您应该将其作为应用程序的第一步。否则,无论您请求什么 L&F,您都会冒初始化 Java L&F 的风险。当静态字段引用 Swing 类时,这可能会无意中发生,这会导致 L&F 被加载。如果尚未指定 L&F,则加载 JRE 的默认 L&F。对于 Sun 的 JRE,默认值为 Java L&F,对于 Apple 的 JRE,默认值为 Apple L&F,依此类推。
To remedy, you should do this (explanation located here) - replace your try/catch block with this code:
为了补救,您应该这样做(位于此处的解释) - 用以下代码替换您的 try/catch 块:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
this.pack();
回答by Riquochet
I know you can set the look and feel for the whole application, but what do you do if you like the cross platform look and feel but want the System Look and Feel for the JFileChoosers. Especially since the cross platform doesn't even have the right file icons (and looks completely cheesy.)
我知道您可以为整个应用程序设置外观,但是如果您喜欢跨平台的外观,但想要 JFileChoosers 的系统外观和感觉,您会怎么做。特别是因为跨平台甚至没有正确的文件图标(而且看起来很俗气。)
Here is what I did. It is definitely a hack...
这是我所做的。这绝对是一个黑客......
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;
}
}
回答by jithin
To start off:
开始:
String path = null;
FileChooser fc=new FileChooser(path);
fc.showOpenDialog(null);
Then in another class:
然后在另一个班级:
public FileChooser(){
this(null);
}
public FileChooser(String path) {
super(path);
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
this.pack();
} catch (Exception ex) {
Logger.getLogger(FileChooser.class.getName()).log(Level.SEVERE, null, ex);
}
JFileChooser chooser = new JFileChooser();
}
private void pack() {
try{
}catch(UnsupportedOperationException eu){
};
}
回答by Jacob
Try this at the beginning of your main method. Or if you are using netbeans or eclipse windowbuilder generated code, put this before the generated code.
在 main 方法的开头试试这个。或者,如果您使用的是 netbeans 或 eclipse windowbuilder 生成的代码,请将其放在生成的代码之前。
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch (UnsupportedLookAndFeelException e) {}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
回答by VirtualBlade
If you need this -> Windows Look And Feel Sample
如果您需要此 -> Windows 外观和感觉示例
Use can use the below code (too)!
使用可以使用下面的代码(太)!
Have fun!
玩得开心!
JFrame w = new FileExplorerJFrame();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException ex) {
Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(ExcelTest.class.getName()).log(Level.SEVERE, null, ex);
}
SwingUtilities.updateComponentTreeUI(w);
w.pack();
w.setVisible(true);
回答by RobB
Using the UIManager.setLookAndFeel(...);
early in your main method should be the cleanest approach as explained previously but be very cautious about adding it to an existingapplication without extensive testing.
UIManager.setLookAndFeel(...);
如前所述,在主要方法中使用早期应该是最干净的方法,但在将其添加到现有应用程序时要非常谨慎,而无需进行大量测试。
For instance, I tried changing the LAFto WindowsLookAndFeel(so that JFileChooser knows that "My Documents" actually refers to a folder named "Documents") and hit a NullPointerExceptionin a different module due to the following line:
例如,我尝试将LAF更改为WindowsLookAndFeel(以便 JFileChooser 知道“我的文档”实际上是指名为“文档”的文件夹)并由于以下行在不同的模块中遇到NullPointerException:
int separatorWidth = (new JToolBar.Separator()).getSeparatorSize().width;