- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在使用 Nimbus L&F 时覆盖 JTable
中标题的背景颜色。我正在有效地“主题化”Nimbus L&F,即对其进行小幅调整。
无论我尝试什么,它似乎都没有效果。
这是一个 SSCCS :
public class MyTest {
public static void main(String[] args) {
new MyTest();
}
public MyTest() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(MyTest.class.getName()).log(Level.SEVERE, null, ex);
}
UIManager.put("TableHeader.background", Color.RED);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
DefaultTableModel model = new DefaultTableModel(
new Object[][]{
{"hhvt ", "er sdf", "sfdg"},
{"hyshg ", "dh sdf", "jer"}
},
new Object[]{"Col A", "Col B", "Col C"}
);
JTable table = new JTable(model);
setLayout(new BorderLayout());
add(new JScrollPane(table));
}
}
}
结果如下:
我很清楚 Nimbus 是合成器 L&F,所以它使用 Painter几乎任何东西。我打赌我可以覆盖 UIManager
中的一些 Painter
,但我不想从头开始重做 Painter。 Nimbus 中的 Painters 非常先进,他们使用渐变和你有什么。我想利用这一点。这只是我想更改的颜色。
最佳答案
这是一个可能的 - 但非常丑陋的 - 解决方案。
Nimbus 严重依赖于 Painter
。 Nimbus 之所以好看,是因为它使用了渐变、阴影等等。这是 Painter
的工作。我们真的,真的不想做我们自己的画家。 Nimbus Painters 非常复杂,可以产生漂亮的效果。所以我们想利用它们。不要自己做!
Nimbus 有很多自动生成的源代码。所有源代码都是从 skin.laf
XML 文件(位于 JDK 源代码中)生成的,但在运行时不使用 XML 文件。大多数自动生成的源文件实际上是特定于类型的 Painter
。例如,TableHeaderRendererPainter
有一个 painter 类(负责绘制表头的 painter)等等。问题是所有自动生成的源代码都是包私有(private)的。
Painters 在 NimbusLookAndFeel 实例初始化时设置。此后它们不会改变。
从 skin.laf
文件我们可以看到什么颜色用于什么。在我们的例子中,我们可以看到控制表格标题背景颜色的实际上是 nimbusBlueGrey
颜色。我们不能只更改 nimbusBlueGrey
的值,因为这会影响 Nimbus 中使用此颜色的一切。所以我们需要想出别的办法。这就是它变得丑陋的地方。
在特定情况下,我们对默认情况下的表格标题感兴趣(即,当鼠标不在表格上方时,表格未被禁用,列标题未被按下等)。这就是我们将在下面集中讨论的内容。但是对于某人想要做的任何其他类型的特殊装饰,该技术都是相同的。
技术是首先启动 NimbusLookAndFeel
的临时实例。我们这样做只是为了能够“窃取”它生成的其中一个 Painters。我们可以安全地保留此 Painter,然后真正启动 NimbusLookAndFeel
。现在我们可以替换特定的 Painter,以便我们换入之前保存的 Painter。
public class MyTest {
public static void main(String[] args) throws UnsupportedLookAndFeelException {
new MyTest();
}
public MyTest() throws UnsupportedLookAndFeelException {
// Start dummy instance of L&F
NimbusLookAndFeel nimbusTmp = new NimbusLookAndFeel();
Object nimbusBlueGreyOrg = UIManager.get("nimbusBlueGrey"); // original value
UIManager.put("nimbusBlueGrey", Color.RED); // the color we want
try {
UIManager.setLookAndFeel(nimbusTmp);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(MyTest.class.getName()).log(Level.SEVERE, null, ex);
}
Object painter = UIManager.get("TableHeader:\"TableHeader.renderer\"[Enabled].backgroundPainter");
// We've got what we came for. Now unload the dummy.
UIManager.getLookAndFeel().uninitialize(); // important to avoid UIDefaults change listeners firing
UIManager.put("nimbusBlueGrey", nimbusBlueGreyOrg); // revert
// Load the L&F for real.
UIManager.setLookAndFeel(new NimbusLookAndFeel());
// Swap in the value we saved previously
UIManager.put("TableHeader:\"TableHeader.renderer\"[Enabled].backgroundPainter", painter);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
DefaultTableModel model = new DefaultTableModel(
new Object[][]{
{"hhvt ", "er sdf", "sfdg"},
{"hyshg ", "dh sdf", "jer"}},
new Object[]{"Col A", "Col B", "Col C"}
);
JTable table = new JTable(model);
setLayout(new BorderLayout());
add(new JScrollPane(table));
}
}
}
对此并不感到自豪,但它确实有效。谁有更好的想法?
关于java - Nimbus - 覆盖 TableHeader 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840096/
我是 Java Nimbus 外观和感觉 的新手。我正在尝试使用 Nimbus 的功能来自定义单个组件实例,使用 putClientProperty("Nimbus.Overrides", overr
我正在使用 java 7 update 17 和 netBeans IDE 7.3 我想将 nimbus 设置为每个 GUI 项目的默认值,但 Oracle 网站指令不起作用。 how to set
我正在制作一个程序,该程序应该尽可能独立于平台。我正在使用 Nimbus LAF。 我想知道,我应该担心字体吗?它的默认字体是包含在 LAF 本身中还是从平台获取? 最佳答案 这是默认的非系统字体。
如何自定义 Nimbus 外观中组件的图像?我想在 Photoshop 中创建图像并放置在一些 Nimbus 外观和感觉组件上,这些是我想要更改的组件: 滚动条按钮旋钮滚动条拇指滚动条滚动条轨道 谢谢
在 Nimbus 的网络照片应用程序示例中,当我返回表格(从 Facebook 或 Dribbble 中选择要下载照片的位置)时,我之前看到的和缓存中的照片都会从其中清除。有办法防止这种情况吗? 例如
我想更改选项卡标题的颜色。 我尝试设置不同的属性,但没有任何变化。 在我的 main 中,我将属性 text 设置为白色,因此所有文本都是白色的。 现在我想更改一些组件文本,例如选项卡 Pane ,但
我已经创建了一个文本字段组件,里面有一个“x”按钮,可以清除该字段的文本。它在使用 Window 的系统外观和感觉(对于 Windows)时效果很好,但是我在使用 Nimbus 时尝试获得正确的外观时
引用这个错误: 4848910 : UIManager only updates colors once 我的 JFrame 上有两个按钮。从第一个按钮开始,我将 LAF 颜色更改为 UIManage
所以,我的程序中有一个使用 Nimbus LAF 的 JTextArea。由于一些功能问题,我需要将其替换为 JTextPane。 但是,JTextArea 默认情况下有一个绘制的边框。 JTextP
我正在使用 NIPagingScrollView在 iPhone 上显示多个页面。 每次我翻到一个页面,下一页也被预先加载,这很好。 当我旋转 iPhone从纵向模式到横向模式,我让layoutSub
我正在使用 Nimbus显示带有擦洗器和可缩放 ImageView 的相册。我使用网络图像,并在加载最终图像之前显示缩略图。 NIPhotoAlbumScrollView提供方法didLoadPhot
我正在尝试覆盖 JLabel 的一些 nimbus 属性。 UIDefaults labelDefault = new UIDefaults(); labelDefault.put("Labe
刚接触java,有一个问题,无论我做什么似乎都无法解决,所以任何我们将非常感谢您的帮助。 我有一个Java小应用程序,并集成了一个弹出日历(WWW)使用 Nimbus 外观和感觉的可用。我的问题是我无
我正在尝试全局更改工具提示的大小。 我正在使用: UIManager.put("ToolTip.font", new Font("SansSerif",Font.PLAIN,25)); 总体来说效果很
我们正在使用 Nimbus LaF 开发一个 Swing 应用程序。我们更改了许多 Nimbus 默认设置(控件、文本、NimbusLightBackground 等)以拥有深色主题。 现在我们在渲染
我想验证表格单元格中的用户输入,并且我使用了 Nimbus 外观。这是验证整数输入的单元格编辑器的代码:WholeNumberField它扩展了 JTextField 并添加了输入验证。 当我为列设置
我想知道为什么 Nimbus会以某种方式与 Virtual keys 发生冲突.查看我在下面制作的示例: public class buttontest implements ActionLi
只是寻找一些文档。 最佳答案 Nimbus 基于 Synth,因此您应该从 Synth 的角度考虑对其进行自定义,这一点已有详细记录。 关于java - 有人有关于 Nimbus 外观和感觉定制的信息
我想更改选项卡选项卡的高度,为此我尝试了多种方法,但没有成功。首先,我尝试使用间隔图标,高度很好,但选项卡的文本不再居中。然后我尝试更改 Nimbus L&F 属性,TabbedPane.tabIns
我有例如 UIDefaults defaults = UIManager.getLookAndFeelDefaults(); defaults.put("text",Color.GREEN);` 文本
我是一名优秀的程序员,十分优秀!