gpt4 book ai didi

java - Nimbus - 覆盖 TableHeader 的颜色

转载 作者:搜寻专家 更新时间:2023-11-01 03:22:26 25 4
gpt4 key购买 nike

我想在使用 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));
}

}

}

结果如下:

enter image description here

我很清楚 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com