如何更改Java程序单个部分的外观 [英] How to change the look and feel of a single part of a java program

查看:95
本文介绍了如何更改Java程序单个部分的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个程序选择工具,目前我喜欢仅用Java外观的所有外观.我唯一要更改的是Windows的JFileChooser外观.当我打电话给filechooser并告诉它改变外观时,它什么也没做.当我在程序启动时调用它时,它会使按钮显得糟糕.所以谷歌没有任何东西,我不知道如何使它工作.请帮忙!让我知道哪些代码将是相关且有用的.预先感谢!

So I'm making a program selection tool, and currently i like the way everything looks with just the java look and feel. the only thing i want to change is the JFileChooser look and feel to Windows. When i call the filechooser and tell it to change the look and feel then, it doesn't do anything. when i call it when the program starts, it makes the buttons look crappy. so google doesn't have anything, and i cant figure out how to get this to work. please help! let me know what code would be relevant and useful. Thanks in advance!

因此,这是有关JFileChooser及其启动方式的一些代码:

So here's some code relevent to the JFileChooser and how it is started:

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();
    }
}
}

推荐答案

所需要做的就是在创建JFileChooser对象时更改UIManager,然后将其设置回原来的状态,或者您可以捕获Exception,但是这是不好的做法.

All that is needed is to change the UIManager while creating the JFileChooser Object, then setting it back to what it was previously, alternatively you could just catch Exception, but that is bad practice.

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);
}

这篇关于如何更改Java程序单个部分的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆