1

我无法更改 Java Swing JScrollBar 的颜色。我已经搜索并阅读了很多关于此的内容。我正在尝试使用 XML 文件来使用 Swing 的 Synth 外观定义颜色。这对于按钮来说效果很好,但对于滚动条根本不起作用。

以下是 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

}
4

1 回答 1

1

尝试这个

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

不过,它仅适用于 Motif Look & Feel。如果您使用金属外观,它不会做任何事情。

于 2011-12-06T04:43:42.490 回答