2016-08-19 65 views
0

这里的色彩是我在我试图改变从雨云默认的黑色垂直JSeparator的颜色为红色已经试过基础上,答案How to change the color of a JSeparator?如何改变一个JSeparator对象的使用Nimbus的L&F

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但不是我希望的方式(只是改变了背景色VS分隔线颜色)

+1

的可能的复制[如何改变一个JSeparator对象的颜色?(http://stackoverflow.com/questions/13083876/how-to-改变颜色的jseparator) –

+0

它不是重复的@JonnyHenly,因为答案只适用于金属L&F不适用于Nimbus –

+0

@SammyGuergachi该帖子不适用于用户界面,而是直接与组件直接相关。它应该工作,不管已安装的外观和感觉。 – Mordechai

回答

1

我通过改变雨云使用导出nimbusBlueGrey颜色的解决了这个其他颜色。将分隔符设置为不透明只会帮助更改背景颜色,但JSeperator's有2种颜色,前景和背景,因此设置为不透明并更改背景色可修复一半问题。 nimbusBlueGrey似乎处理前景色,这似乎不可覆盖setForegroundcolor()Separator.foreground属性。

问题是,更改nimbusBlueGrey会影响许多其他组件的颜色。我不知道如何将颜色更改包含在JSeperator中。

0
/** 
* @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,它对我来说工作得很好。

这里是我的快照的用户界面:

Here is Snapshot Of My UI

相关问题