[英]Windows look and feel for JFileChooser
我正在嘗試生成具有Windows外觀的JFileChooser
。 我找不到一個方法來改變它,所以我創建了一個擴展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()); }
然后,在另一節課中,我打電話
FileChooser chooser = new FileChooser(fileName);
int val = chooser.showOpenDialog(null);
但是出現的對話框具有Java外觀。 有關如何改變這一點的任何想法? 是否有一個JFileChooser類的方法,我可以使用它而不是這個擴展類?
謝謝!
我知道你可以設置整個應用程序的外觀和感覺,但是如果你喜歡跨平台的外觀和感覺但是想要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;
}
}
如果您不需要更改外觀,可以嘗試將UIManager.setLookAndFeel(..)行放在入門類的main方法中嗎?
這似乎對我有用,雖然我不知道為什么它不能像你設置它那樣工作。
首先,嘗試從命令行運行代碼並指定其外觀以查看它是否可以應用。
java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel YourApp
如果它確實應用了正確的外觀,那么您可以在創建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
}
問題是當你調用super(path)
時已經為你選擇了Look&Feel。
來自Java Tutorial for Look and Feel :
注意 :如果您要設置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,等等。
要解決此問題,您應該執行此操作( 此處的說明 ) - 使用以下代碼替換try / catch塊:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
this.pack();
在main方法的開頭嘗試這個。 或者如果您使用netbeans或eclipse windowbuilder生成的代碼,請將其放在生成的代碼之前。
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch (UnsupportedLookAndFeelException e) {}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
使用UIManager.setLookAndFeel(...);
在您的主要方法的早期應該是如前所述的最干凈的方法,但是在沒有進行大量測試的情況下將其添加到現有應用程序時要非常謹慎。
例如,我嘗試將LAF更改為WindowsLookAndFeel (以便JFileChooser知道“我的文檔”實際上引用了名為“Documents”的文件夾)並且由於以下行而在另一個模塊中遇到NullPointerException :
int separatorWidth = (new JToolBar.Separator()).getSeparatorSize().width;
使用可以使用以下代碼(也)!
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);
首先:
String path = null;
FileChooser fc=new FileChooser(path);
fc.showOpenDialog(null);
然后在另一個班級:
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){
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.