如何使用 Nimbus L&F 更改 JSeparator 的颜色
How to change the color of a JSeparator using the Nimbus L&F
这是我尝试根据 How to change the color of a 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);
}
}
但是垂直分隔符仍然是黑色。我应该做什么?
注意:我知道 Nimbus 是问题所在,因为我尝试在没有将 L&F 设置为 Nimbus 的情况下工作正常。还要注意设置 Separator[Enabled].backgroundPainter
属性 似乎影响了 JSeperator
但不是我想要的方式(只是改变了背景颜色与分隔线颜色)
我通过更改 Nimbus 用来派生其他颜色的 nimbusBlueGrey
颜色解决了这个问题。将分隔符设置为不透明只会帮助更改背景颜色,但 JSeperator's
有 2 种颜色,前景和背景,因此设置为不透明并更改背景颜色解决了一半问题。 nimbusBlueGrey
似乎处理前景色,这似乎不能被 setForegroundcolor()
或 Separator.foreground
属性.
覆盖
问题是更改 nimbusBlueGrey
会影响许多其他组件的颜色。我不确定如何将颜色更改仅包含在 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);
}
});
}
只需将您的外观从 NIMBUS 更改为 WINDOWS,它对我来说效果很好。
这是我的快照 UI:
这是我尝试根据 How to change the color of a 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);
}
}
但是垂直分隔符仍然是黑色。我应该做什么?
注意:我知道 Nimbus 是问题所在,因为我尝试在没有将 L&F 设置为 Nimbus 的情况下工作正常。还要注意设置 Separator[Enabled].backgroundPainter
属性 似乎影响了 JSeperator
但不是我想要的方式(只是改变了背景颜色与分隔线颜色)
我通过更改 Nimbus 用来派生其他颜色的 nimbusBlueGrey
颜色解决了这个问题。将分隔符设置为不透明只会帮助更改背景颜色,但 JSeperator's
有 2 种颜色,前景和背景,因此设置为不透明并更改背景颜色解决了一半问题。 nimbusBlueGrey
似乎处理前景色,这似乎不能被 setForegroundcolor()
或 Separator.foreground
属性.
问题是更改 nimbusBlueGrey
会影响许多其他组件的颜色。我不确定如何将颜色更改仅包含在 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);
}
});
}
只需将您的外观从 NIMBUS 更改为 WINDOWS,它对我来说效果很好。
这是我的快照 UI: