使用Nimbus和Java 8U20时JList颜色错误

时间:2021-04-12 18:17:11

My application uses wrong colors for JList since I updated to the latest Java 8 version (U20). E.g. instead of dark blue for selected items a light gray is actually used.

我的应用程序在JList中使用了错误的颜色,因为我更新了最新的Java 8版本(U20)。例如,用浅灰色代替深蓝色。

Simple test application:

简单的测试应用程序:

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;



public class Test {

    public Test() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());

            JList<String> l = new JList<>();
            DefaultListModel<String> model = new DefaultListModel<>();
            model.add(0, "sssssssss");
            model.add(1, "sssssssss");
            model.add(2, "sssssssss");
            model.add(3, "sssssssss");
            l.setModel(model);

            JFrame f = new JFrame();
            f.setSize(500, 500);
            f.setLocationRelativeTo(null);
            f.add(l);
            f.pack();
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setVisible(true);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Test();
            }
        });
    }    
}

Java 7, Java 8

Java 7中,Java 8

使用Nimbus和Java 8U20时JList颜色错误

Java 8 U20:

Java 8 U20:

使用Nimbus和Java 8U20时JList颜色错误

JList.getSelectionBackground() returns

JList.getSelectionBackground()返回

DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138

but actually it is not RGB(57,105,138) but the above mentioned light gray.

但实际上它不是RGB(57,105,138),而是上面提到的浅灰色。

2 个解决方案

#1


3  

You can restore the exact behavior of versions before 1.8.0_20 with the following initialization code:

您可以使用以下初始化代码在1.8.0_20之前恢复版本的确切行为:

final NimbusLookAndFeel laf = new NimbusLookAndFeel();
UIManager.setLookAndFeel(laf);
UIDefaults defaults = laf.getDefaults();
defaults.put("List[Selected].textForeground",
    laf.getDerivedColor("nimbusLightBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Selected].textBackground",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Disabled+Selected].textBackground",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Disabled].textForeground",
    laf.getDerivedColor("nimbusDisabledText", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List:\"List.cellRenderer\"[Disabled].background",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));

This reverts what have changed between 1.8.0_05 and 1.8.0_20 in the class NimbusDefaults. The parameter false has been removed (which turns it effectively to true via the overloaded method). This change turns the Colors into UIResources what may be formally correct, but for whatever reason it causes the problem you have encountered. So re-inserting the false restores the old behavior.

这将返回在类nimbus8.0_05和1.8.0_20之间发生的更改。参数false已经被删除(通过重载方法将其有效地转换为true)。此更改将颜色转换为uiresource,这可能在形式上是正确的,但无论出于什么原因,它会导致您遇到的问题。所以重新插入错误恢复旧行为。

#2


2  

I had the same issue. Here is how I solved it for my application:

我也有同样的问题。下面是我如何为我的应用程序解决它:

 ...

 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
  ...

  UIManager.getLookAndFeelDefaults().put("List[Selected].textBackground", new Color(57, 105, 138));
  UIManager.getLookAndFeelDefaults().put("List[Selected].textForeground", Color.WHITE);

Here is a list of the UIManager's (Color) Keys:

下面是UIManager(颜色)键的列表:

http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html

http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html

Hope this helps you,

希望这能帮助你,

Edit: Tested with your provided code. It works.

编辑:使用您提供的代码进行测试。它的工作原理。

Best regards

致以最亲切的问候

#1


3  

You can restore the exact behavior of versions before 1.8.0_20 with the following initialization code:

您可以使用以下初始化代码在1.8.0_20之前恢复版本的确切行为:

final NimbusLookAndFeel laf = new NimbusLookAndFeel();
UIManager.setLookAndFeel(laf);
UIDefaults defaults = laf.getDefaults();
defaults.put("List[Selected].textForeground",
    laf.getDerivedColor("nimbusLightBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Selected].textBackground",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Disabled+Selected].textBackground",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List[Disabled].textForeground",
    laf.getDerivedColor("nimbusDisabledText", 0.0f, 0.0f, 0.0f, 0, false));
defaults.put("List:\"List.cellRenderer\"[Disabled].background",
    laf.getDerivedColor("nimbusSelectionBackground", 0.0f, 0.0f, 0.0f, 0, false));

This reverts what have changed between 1.8.0_05 and 1.8.0_20 in the class NimbusDefaults. The parameter false has been removed (which turns it effectively to true via the overloaded method). This change turns the Colors into UIResources what may be formally correct, but for whatever reason it causes the problem you have encountered. So re-inserting the false restores the old behavior.

这将返回在类nimbus8.0_05和1.8.0_20之间发生的更改。参数false已经被删除(通过重载方法将其有效地转换为true)。此更改将颜色转换为uiresource,这可能在形式上是正确的,但无论出于什么原因,它会导致您遇到的问题。所以重新插入错误恢复旧行为。

#2


2  

I had the same issue. Here is how I solved it for my application:

我也有同样的问题。下面是我如何为我的应用程序解决它:

 ...

 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
  ...

  UIManager.getLookAndFeelDefaults().put("List[Selected].textBackground", new Color(57, 105, 138));
  UIManager.getLookAndFeelDefaults().put("List[Selected].textForeground", Color.WHITE);

Here is a list of the UIManager's (Color) Keys:

下面是UIManager(颜色)键的列表:

http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html

http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html

Hope this helps you,

希望这能帮助你,

Edit: Tested with your provided code. It works.

编辑:使用您提供的代码进行测试。它的工作原理。

Best regards

致以最亲切的问候