2011-12-06 46 views
1

我無法更改Java Swing JScrollBar的顏色。我已經在這方面進行了很多搜索和閱讀。我正在嘗試使用XML文件來定義使用Swing的Synth外觀的顏色。這對於一個按鈕來說工作得很好,但對於滾動條來說完全不起作用。無法使用合成器更改ScrollBar的顏色 - Java Swing

我一直希望能夠附加文件到這個問題,但它似乎沒有,我可以。我很樂意向您發送這些文件。

在此先感謝。 ---艾倫

下面是XML文件的內容:

<synth> 

     <!-- Style that all regions will use --> 
    <style id="backingStyle"> 
    <!-- Make all the regions that use this skin opaque--> 
    <opaque value="TRUE"/> 
    <font name="Dialog" size="12"/> 
    <state> 
     <!-- Provide default colors --> 
     <color value="#F5DEB3" type="BACKGROUND"/> 
     <color value="black" type="FOREGROUND"/> 
    </state> 
    </style> 
    <bind style="backingStyle" type="region" key=".*"/> 

    <!-- Scroll bar track style --> 
    <style id="scrollBarTrackStyle"> 
    <state> 
     <color value="red" type="BACKGROUND"/> 
     <color value="blue" type="FOREGROUND"/> 
    </state> 
    </style> 
    <bind style="scrollBarTrackStyle" type="REGION" key="ScrollBarTrack" /> 

    <!-- Scroll bar thumb style --> 
    <style id="scrollBarThumbStyle"> 
    <state> 
     <color value="white" type="BACKGROUND"/> 
     <color value="yellow" type="FOREGROUND"/> 
    </state> 
    </style> 
    <bind style="scrollBarThumbStyle" type="REGION" key="ScrollBarThumb" /> 

    <!-- Scroll bar style --> 
    <style id="scrollBarStyle"> 
    <state> 
     <color value="red" type="BACKGROUND"/> 
     <color value="blue" type="FOREGROUND"/> 
    </state> 
    </style> 
    <bind style="scrollBarStyle" type="REGION" key="ScrollBar" /> 

</synth> 

這裏是源代碼:

/* 
* TestSynthScrollBar.java 
* 
* Created on Dec 5, 2011, 2:52:26 PM 
*/ 

package playDisplay; 

import javax.swing.UIManager; 
import javax.swing.plaf.synth.SynthLookAndFeel; 

public class TestSynthScrollBar extends javax.swing.JFrame 
{ 

    private static String synthFile = "scrollBarSkin.xml"; 

    /** Creates new form TestSynthScrollBar */ 
    public TestSynthScrollBar() 
    { 
     initComponents(); 
    } 

    private static void initAnotherLookAndFeel() 
    { 
     SynthLookAndFeel badAssLookAndFeel = new SynthLookAndFeel(); 
     try 
     { 
      badAssLookAndFeel.load(TestSynthScrollBar.class.getResourceAsStream(synthFile), TestSynthScrollBar.class); 
      UIManager.setLookAndFeel(badAssLookAndFeel); 
     } 
     catch (Exception e) 
     { } 
    } 

    /** This method is called from within the constructor to 
    * initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is 
    * always regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 
    private void initComponents() { 

     somePanel = new javax.swing.JPanel(); 
     someScrollPane = new javax.swing.JScrollPane(); 
     someTextArea = new javax.swing.JTextArea(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     someScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
     someScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

     someTextArea.setColumns(20); 
     someTextArea.setRows(5); 
     someScrollPane.setViewportView(someTextArea); 

     javax.swing.GroupLayout somePanelLayout = new javax.swing.GroupLayout(somePanel); 
     somePanel.setLayout(somePanelLayout); 
     somePanelLayout.setHorizontalGroup(
      somePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(somePanelLayout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(someScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     somePanelLayout.setVerticalGroup(
      somePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, somePanelLayout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(someScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap()) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(somePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(somePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>//GEN-END:initComponents 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) 
    { 
     initAnotherLookAndFeel(); 

     java.awt.EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new TestSynthScrollBar().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify//GEN-BEGIN:variables 
    private javax.swing.JPanel somePanel; 
    private javax.swing.JScrollPane someScrollPane; 
    private javax.swing.JTextArea someTextArea; 
    // End of variables declaration//GEN-END:variables 

} 

回答

1

試試這個

UIManager.getLookAndFeelDefaults().put("ScrollBar.thumb", Color.blue); 

它只會與工作母題看&感覺雖然。如果你使用Metal外觀,它不會做任何事情。

+0

約翰尼,爲什麼不爲金屬LAF做這項工作?謝謝,艾倫 – Alan

+0

從我所看到的,我似乎沒有與該LAF工作。試一試。 –