所以我正在制作一个程序选择工具,目前我喜欢所有看起来只有java外观的方式。我唯一想要改变的是Windows的JFileChooser外观。当我调用filechooser并告诉它改变外观时,它什么也没做。当我在程序启动时调用它时,它会使按钮看起来很糟糕。所以谷歌没有任何东西,我无法弄清楚如何使这个工作。请帮忙!让我知道哪些代码是相关和有用的。提前谢谢!
编辑: 所以这里有一些与JFileChooser相关的代码以及它是如何启动的:
public class Start(){
public static JButton assignButton = new JButton(new AbstractAction(
"Assign") {
public void actionPerformed(ActionEvent e) {
AssignWindow.run();
}
});
}
public class AssignmentWindow(){
public static void run() {
Dialogs.assignmentInfo();
bgImage = aw.getImage("files/background.png");
//aw is the object of this class
aw.makeFrame(); //makes the jframe for all the buttons to sit.
aw.setGraphics(); //assigns a different graphics variable
aw.fileChooser();
}
public void fileChooser() {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
// here is where i want to set the look and feel....
if (jfc.showDialog(null, "Select") == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
fileDir = file.getPath();
} else {
Dialogs.msg("You cancled selecting a file. Returning to file frame...");
AssignWindow.destroy();
}
}
}
答案 0 :(得分:15)
所需要的只是在创建JFileChooser对象时更改UIManager,然后将其设置回原来的状态,或者你可以捕获Exception,但这是不好的做法。
public void stuff(){
JFileChooser chooser = null;
LookAndFeel previousLF = UIManager.getLookAndFeel();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
chooser = new JFileChooser();
UIManager.setLookAndFeel(previousLF);
} catch (IllegalAccessException | UnsupportedLookAndFeelException | InstantiationException | ClassNotFoundException e) {}
//Add whatever other settings you want to the method
chooser.showOpenDialog(frame);
}
答案 1 :(得分:6)
是的,这是可能的。您可以设置每手的UI:
JFileChooser jfc = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(jfc);
wui.installUI(jfc);
jfc.showOpenDialog(parentComponent);
这将为filechooser设置Windows UI,但保留所有其他组件的外观。
答案 2 :(得分:3)
推送Cancel JButton
,从Metal
到System
和Nimbus Look and Feel
必须通过代码行
调用已经可见容器的所有更新SwingUtilities.updateComponentTreeUI(Top-Level Container);
代码
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
class ChooserFilterTest {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"};
for (String property : properties) {
System.out.println(property + ": " + System.getProperty(property));
}
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
jfc.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
}
@Override
public String getDescription() {
return "Wavefront OBJ (*.obj)";
}
@Override
public String toString() {
return getDescription();
}
});
int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(jfc);
} catch (Exception e) {
e.printStackTrace();
}
jfc.showOpenDialog(null);
result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
try {
for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
SwingUtilities.updateComponentTreeUI(jfc);
break;
}
}
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
}
jfc.showOpenDialog(null);
result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
}
};
SwingUtilities.invokeLater(r);
}
private ChooserFilterTest() {
}
}
答案 3 :(得分:2)
您应该阅读Look and Feel 此外,我认为你不能为每个组件提供不同的L& F.至少我从未见过使用非均匀L& F
的应用程序