如何更改java程序的单个部分的外观

Pul*_*nda 5 java user-interface swing jfilechooser look-and-feel

所以我正在制作一个程序选择工具,目前我喜欢所有看起来只有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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Pul*_*nda 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);
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*han 6

是的,这是可能的.您可以设置每手的UI:

JFileChooser jfc = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(jfc);
wui.installUI(jfc);
jfc.showOpenDialog(parentComponent);
Run Code Online (Sandbox Code Playgroud)

这将为filechooser设置Windows UI,但保留所有其他组件的外观.