如何获取 JList 未选定单元格的背景颜色?
我试图找到未选择的单元格使用的颜色。这通常是白色的,但是当我调用 UIManager.getColor("List.background") 时,它似乎与 JPanel 所用的灰色相同。当我调用 new JList().getBackground() 时,我得到了同样可怕的灰色,但当我实际使用该列表时,它是白色的。如何从 JList 或 UIManager 获取这种白色?我目前正在做的查找背景颜色的操作是这样的:
String[] contents = {"Foo", "Bar"};
JList list = new JList(contents);
// Prints true
System.out.println(list.getBackground().equals(new Color(237, 236, 235)));
由于 List.selectionBackground 为我提供了单击单元格时期望看到的蓝色,因此我认为 List.background 将为我提供未选定单元格的颜色。那么 List.background 实际上返回的值是什么?
相关说明,是否有列出这些键的含义?我发现了一个相关问题,但没有一个答案提供按键的描述。
编辑:看来这是执行此操作的正确方法。然而,至少在 GNOME 中,调用 setLookAndFeel 时会出现问题。
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
String[] contents = {"Foo", "Bar"};
JList list = new JList(contents);
// Prints true
System.out.println(list.getBackground().equals(new Color(237, 236, 235)));
// Add list to a pane and display it, and it will actually be white
看起来这可能是一个错误,抱歉各位。
I am trying to find the color used by cells that are unselected. This is usually white, however when I call UIManager.getColor("List.background"), it appears to be the same grey color used for JPanels. When I call new JList().getBackground(), I get the same horrid grey color back, but when I actually use the list, it's white. How do I get this white color from JList or UIManager? What I am currently doing to find the background color is this:
String[] contents = {"Foo", "Bar"};
JList list = new JList(contents);
// Prints true
System.out.println(list.getBackground().equals(new Color(237, 236, 235)));
Since List.selectionBackground gives me the blue color I expect to see when I click on a cell, I figured List.background would give me the color of an unselected cell. What is List.background actually returning a value for then?
On related note, is there a listing somewhere of what these keys means? I've found a related question, but none of the answers provide descriptions of the keys.
EDIT: It appears this is the correct way to do this. However, at least in GNOME the problem arises when calling setLookAndFeel.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
String[] contents = {"Foo", "Bar"};
JList list = new JList(contents);
// Prints true
System.out.println(list.getBackground().equals(new Color(237, 236, 235)));
// Add list to a pane and display it, and it will actually be white
Looks like this might be a bug, sorry guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
嗯,你能根据这个例子澄清我(我们)你的问题是什么(来自已故的 Old.Good.Forum.Sun.com,:-),我知道不是珍妮特抵抗)
注意:最好改变这个使用
getListCellRendererComponent
编辑值:请再次使用该检查 camickr UI 默认
EDIT:
hmmm, can you clarify me (us) what is your issue based on this example (from died Old.Good.Forum.Sun.com, :-) and I know that not Jeanette resist)
Note: better would be change this value by using
getListCellRendererComponent
EDIT2: please for another play with that check camickr UI Defaluts