`
xichao1929
  • 浏览: 38862 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

substance 控件颜色迁移(变色龙)用法

阅读更多

    在进行这个之前必须引入第三包,substance.jar ,   附件里面有。在网上搜的这个包,很多事不全的。我  也遇到了这个问题,不过这个包目前还可以。

   我们可以设置控件的颜色,但是可选的颜色不是很多,不过还好我们可以自己配置相关的颜色。不过对于控制颜色的浅淡还是不太容易的。现在我们来看看substance是怎么做到的。Substance中有一个属性,colorization _factor (中文叫做:变色龙)。使用这个方法进行改变按钮颜色的深浅:

<!--EndFragment-->

 JButton button = new JButton("sample");
    button.setBackground(Color.yellow);
    button.setForeground(Color.red);
  button.putClientProperty(SubstanceLookAndFeel.COLORIZATION_FACTOR, new Double(0.8));

 

   有一个地方我说一下,就是substanceLookAndFeel.COLORIZATION_FACTOR 这个是和字符"substancelaf.colorizationFactor"对应的。换句话说上面的一行代码可以完全改为button.putClientProperty("substancelaf.colorizationFactor", new Double(0.8));

 

 

   顺便解释一下这两个方法: setBackground();这个是设置控件的颜色,不包括控件上的字体,即背景颜色。setForeground();一般就是控件上的字体颜色,即前景颜色。

 

 

 

 

以下是完整代码:

 

package color;

import java.awt.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;

/**
 * Test application that shows the use of the {@link
 * SubstanceLookAndFeel#COLORIZATION_FACTOR} client property.
 * 
 * @author Kirill Grouchnikov
 * @see SubstanceLookAndFeel#COLORIZATION_FACTOR
 */
public class ColorizationFactor extends JFrame {
  /**
   * Creates the main frame for <code>this</code> sample.
   */
  public ColorizationFactor() {
    super("Colorization factor");

    this.setLayout(new BorderLayout());

    final JPanel panel = new JPanel(new FlowLayout());
    JButton button = new JButton("sample");
    button.setBackground(Color.yellow);
    button.setForeground(Color.red);
 //   button.putClientProperty(SubstanceLookAndFeel.COLORIZATION_FACTOR, new Double(0.8));
    panel.add(button);
    JCheckBox checkbox = new JCheckBox("sample");
    checkbox.setSelected(true);
    checkbox.setBackground(Color.green.brighter());
    checkbox.setForeground(Color.blue.darker());
    panel.add(checkbox);
    JRadioButton radiobutton = new JRadioButton("sample");
    radiobutton.setSelected(true);
    radiobutton.setBackground(Color.yellow);
    radiobutton.setForeground(Color.green.darker());
    panel.add(radiobutton);

    this.add(panel, BorderLayout.CENTER);

    JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    final JSlider colorizationSlider = new JSlider(0, 100, 50);
    colorizationSlider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        float val = (float) (colorizationSlider.getValue() / 100.0);
      //一下两行代码,功能一样,可以互换。阿
      //  panel.putClientProperty(SubstanceLookAndFeel.COLORIZATION_FACTOR, new Double(
       // val));
        panel.putClientProperty(
        		"substancelaf.colorizationFactor", new Double(
                val));
        panel.repaint();
      }
    });
    controls.add(colorizationSlider);

    this.add(controls, BorderLayout.SOUTH);

    this.setSize(400, 200);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  /**
   * The main method for <code>this</code> sample. The arguments are ignored.
   * 
   * @param args
   *     Ignored.
   * @throws Exception
   *     If some exception occured. Note that there is no special treatment
   *     of exception conditions in <code>this</code> sample code.
   */
  public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
    JFrame.setDefaultLookAndFeelDecorated(true);
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        new ColorizationFactor().setVisible(true);
      }
    });
  }
}

 

<!--EndFragment-->

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics