[英]How to change the color of a JSeparator using the Nimbus L&F
Here is what I've tried in my attempt to change a vertical JSeparator
's color from Nimbus default black to red based on the answer in How to change the color of a JSeparator? 这是我根据如何更改JSeparator颜色的尝试将垂直
JSeparator
的颜色从Nimbus默认黑色更改为红色的尝试。 : :
public class TestFrame extends JFrame {
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setSize(200, 200);
frame.setLayout(new GridBagLayout());
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
}
UIManager.put("Separator.background", Color.red);
UIManager.put("Separator.foreground", Color.red);
JSeparator separator = new JSeparator(JSeparator.VERTICAL);
separator.setPreferredSize(new Dimension(2, 100));
separator.setForeground(Color.red);
separator.setBackground(Color.red);
frame.add(separator, new GridBagConstraints());
frame.setVisible(true);
}
}
Yet the vertical separator remains black. 但是垂直分隔符仍为黑色。 What should I be doing?
我该怎么办?
Note: I know Nimbus is the issue, because I tried without setting the L&F to Nimbus and this worked fine. 注意:我知道Nimbus是问题所在,因为我没有将L&F设置为Nimbus便尝试了,这很好。 Also to note that setting the
Separator[Enabled].backgroundPainter
property seems to have affected the JSeperator
but not in the way I intended (just changed the background color vs the separating line color) 还要注意,设置
Separator[Enabled].backgroundPainter
属性似乎已经影响了JSeperator
但并没有达到我的预期效果(只是更改了背景色与分隔线色)
I resolved this by changing the nimbusBlueGrey
color that Nimbus uses to derive other colors. 我通过更改Nimbus用于导出其他颜色的
nimbusBlueGrey
颜色来解决此问题。 Setting the separator to opaque will only help change the background color, but JSeperator's
have 2 colors, a foreground and a background, so setting to opaque and changing the background color fixed half the problem. 将分隔符设置为不透明只会帮助更改背景颜色,但是
JSeperator's
具有两种颜色,即前景和背景色,因此设置为不透明并更改背景颜色可以解决一半的问题。 nimbusBlueGrey
seems to handle the foreground color, which doesn't seem to be overridable with setForegroundcolor()
or the Separator.foreground
property. nimbusBlueGrey
似乎可以处理前景色,而setForegroundcolor()
或Separator.foreground
属性似乎无法nimbusBlueGrey
前景色。
The problem is that changing nimbusBlueGrey
will affect the color of many other components. 问题在于更改
nimbusBlueGrey
会影响许多其他组件的颜色。 I'm not sure how to contain the color change to just the JSeperator. 我不确定如何仅将颜色更改包含在JSeperator中。
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Windows look and feel instead of NIMBUS*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
/*Change This Line To Make Your TextField Transparent */ if ("WINDOWS".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Furious().setVisible(true);
}
});
}
Just Change Your Look and feel from NIMBUS to WINDOWS,it worked fine for me. 只需将您的外观从NIMBUS更改为WINDOWS,对我来说效果很好。
Here is Snapshot Of My UI: 这是我的UI快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.