简体   繁体   English

Windows外观中的Nimbus进度栏

[英]Nimbus progress bar in a windows look and feel

I saw this post: how to make Indeterminate progressbar look nicely? 我看到了这篇文章: 如何使Indeterminate progressbar看起来更好?

That's just the opposite direction of what I want, I want to use the System (windows) look and feel, but have the nimbus progressbar. 那只是我想要的相反方向,我想使用系统(windows)的外观和感觉,但是要有灵巧的进度条。

My code so far: 到目前为止,我的代码:

private void setLookAndFeel(){

    // set nimbus look&feel
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            try {
                UIManager.setLookAndFeel(info.getClassName());
            } catch (ClassNotFoundException | InstantiationException
                    | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }

    // copy progress bar defaults
    HashMap<Object, Object> progressDefaults = new HashMap<>();
    for(Map.Entry<Object, Object> entry : UIManager.getDefaults().entrySet()){
        if(entry.getKey().getClass() == String.class && ((String)entry.getKey()).startsWith("ProgressBar")){
            progressDefaults.put(entry.getKey(), entry.getValue());
        }
    }

    // set system look&feel
    try {
        UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.updateComponentTreeUI(this);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Couldn't use system look and feel: " + e);
    } 

    // copy back progress bar defaults
    for(Map.Entry<Object, Object> entry : progressDefaults.entrySet()){
        System.out.println(entry.getKey().toString() + ": " + entry.getValue());
        UIManager.put(entry.getKey(), entry.getValue()); //getDefaults().
    }
    System.out.println("lookAndFeel " + UIManager.getLookAndFeel());
}

But if I compile I get the error message: 但是,如果我进行编译,则会收到错误消息:

com.sun.java.swing.plaf.windows.WindowsLookAndFeel cannot be cast to > javax.swing.plaf.nimbus.NimbusLookAndFeel com.sun.java.swing.plaf.windows.WindowsLookAndFeel无法转换为> javax.swing.plaf.nimbus.NimbusLookAndFeel

where I initialize the progress bar. 在这里我初始化进度条。

If someone could explain to me what's going on and/or how to get a nimbus-looking progressbar on a system (windows) look and feel I would be so happy. 如果有人可以向我解释正在发生的事情和/或如何在系统(Windows)上获得看起来像灵气的进度条,那么我会很高兴。 I'm mainly interested in the glass-effekt. 我主要对玻璃效果感兴趣。

edit: If I don't copy the progressbar defaults back, the error is gone. 编辑:如果我不复制进度条默认值,错误就消失了。

I have no idea what the problem was, but the solution is the following: Divide the method in 2 parts (directly before "//copy progress bar defaults"). 我不知道问题出在哪里,但是解决方法如下:将方法分为两部分(紧接在“ //复制进度栏默认值”之前)。 Then call the method that sets the nimbus look&feel, then the method to initialize the class-variables (and with that the progressbar). 然后调用设置nimbus外观的方法,然后调用用于初始化类变量(并带有进度条)的方法。 Third, call the new method with everything beginning from "//copy progress bar defaults". 第三,以“ //复制进度条默认值”开头的所有内容调用新方法。 (the method will copy the progressbar defaults from the nimbus look&feel, set the system look and feel and then paste the progressbar defaults). (该方法将从nimbus外观复制进度条默认值,设置系统外观,然后粘贴进度条默认值)。

This line is just added as a workaround to that AWEFUL captcha bullshit thing. 此行只是作为解决方法添加到该AWEFUL captcha胡扯的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM