【问题标题】:Cannot change colors of a ScrollBar using Synth无法使用 Synth 更改 ScrollBar 的颜色
【发布时间】:2011-12-06 01:04:58
【问题描述】:

我无法更改 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

}

【问题讨论】:

    标签: java swing scrollbar look-and-feel synth


    【解决方案1】:

    试试这个

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

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

    【讨论】:

    • 约翰尼,为什么这对金属 LAF 不起作用?谢谢,艾伦
    • 据我所见,我似乎没有与那个 LAF 合作。试一试。
    猜你喜欢
    • 2015-08-15
    • 2022-01-05
    • 2012-02-16
    • 2021-05-18
    • 1970-01-01
    • 2012-06-19
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多