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

修改JButton的L&F,Java也可以

阅读更多

   Substance里面可以实现对按钮的一些修改,而且比较简单。这次修改是对Button的边框的修改。Substance显示的按钮式不是矩形的,角是弧形的。这是默认的。然后我们可以改成矩形,或者只让某两个角成为直角(这个还是通过修改边实现的)。或者让某一边去掉。下面是图片:

        

 

 buttonA.putClientProperty(SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY,
	        SubstanceConstants.Side.LEFT);
	     //这是button里里面的方法,参数很好理解,第一个是隐藏button的边,第二个是隐藏哪一个边。
	    //如果同时隐藏的边数超过1的话,就需要用到容器EnumSet。
	   // 自然就是把第二个参数改为EnumSet类型。
	    //EnumSet自然就是存放所有的要隐藏或者是修改的边。

EnumSet<Side> leftTopSides = EnumSet.of(SubstanceConstants.Side.LEFT,
	        SubstanceConstants.Side.TOP);
	    buttonC.putClientProperty(SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY,
	        leftTopSides);
	    buttonC.putClientProperty(
	        SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY, leftTopSides);

 

  buttonA.putClientProperty(
	        SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY,
	        SubstanceConstants.Side.LEFT);
//这段代码是将边修改为直线型的

 

以下是完整代码:

package button;

import java.awt.FlowLayout;
import java.util.EnumSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
import org.jvnet.substance.utils.SubstanceConstants;
import org.jvnet.substance.utils.SubstanceConstants.Side;

public class Buttontest extends JFrame {
	  /**
	   * Creates the main frame for <code>this</code> sample.
	   */
	  public Buttontest() {
	    super("Buttons with open sides");

	    this.setLayout(new FlowLayout());

	    JButton buttonA = new JButton("left only");
	    // mark button to have open and straight left side
	    // using side constant
	    buttonA.putClientProperty(SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY,
	        SubstanceConstants.Side.LEFT);
	     //这是button里里面的方法,参数很好理解,第一个是隐藏button的边,第二个是隐藏哪一个边。
	    //如果同时隐藏的边数超过1的话,就需要用到容器EnumSet。
	   // 自然就是把第二个参数改为EnumSet类型。
	    //EnumSet自然就是存放所有的要隐藏或者是修改的边。
	    buttonA.putClientProperty(
	        SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY,
	        SubstanceConstants.Side.LEFT);

	    JButton buttonB = new JButton("right only");
	    // mark button to have open and straight right side using side constant
	    buttonB.putClientProperty(SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY,
	        SubstanceConstants.Side.RIGHT);
	    buttonB.putClientProperty(
	        SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY,
	        SubstanceConstants.Side.RIGHT);

	    JButton buttonC = new JButton("left+top");
	    // mark button to have open and straight left and top sides
	    // using set of side constants
	    EnumSet<Side> leftTopSides = EnumSet.of(SubstanceConstants.Side.LEFT,
	        SubstanceConstants.Side.TOP);
	    buttonC.putClientProperty(SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY,
	        leftTopSides);
	    buttonC.putClientProperty(
	        SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY, leftTopSides);

	    JButton buttonD = new JButton("right+bottom");
	    // mark button to have open and straight right and bottom sides
	    // using set of side constants
	    EnumSet<Side> rightBottomSides = EnumSet.of(SubstanceConstants.Side.RIGHT,
	        SubstanceConstants.Side.BOTTOM);
	    buttonD.putClientProperty(SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY,
	        rightBottomSides);
	    buttonD.putClientProperty(
	        SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY, rightBottomSides);

	    this.add(buttonA);
	    this.add(buttonB);
	    this.add(buttonC);
	    this.add(buttonD);

	    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 Buttontest().setVisible(true);
	      }
	    });
	  }
	}

 

最后一点实现这个必须引入第三方包,不过这个包网上下载的很多是错的。很让人恼火,不过幸好找到一个差不多的,让我我就放到附件里面,有需要的话,各位可以下载。

<!--EndFragment-->

分享到:
评论
Global site tag (gtag.js) - Google Analytics