`
xichao1929
  • 浏览: 40843 次
  • 性别: 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-->

分享到:
评论

相关推荐

    Java Look and Feel.doc

    在Java中,Look and Feel(L&F)提供了图形用户界面的视觉样式和交互行为,包括按钮、文本框、菜单等组件的外观和感觉。 ** Overview of Look and Feel ** Java L&F的主要目标是提供跨平台的一致性,同时允许应用...

    java swing 图形界面实例

    7. **外观与主题**: Swing 支持 Look and Feel(L&F),允许开发者更改应用程序的视觉样式,使其与操作系统或其他风格相匹配。默认的 L&F 称为 Metal,还有 Windows L&F 和 Nimbus L&F 等。 8. **Swing 组件的嵌套*...

    捉猪游戏java源代码

    - **平台独立性**:Swing 组件可以在任何安装了 Java 虚拟机 (JVM) 的平台上运行。 - **高度可定制**:Swing 提供了大量的自定义选项,允许开发者更改组件的外观和行为。 - **丰富的功能**:包括按钮、文本框、菜单...

    swing开发的源代码

    10. ** Swing Look and Feel (L&F)**:Swing允许开发者更改应用的外观和感觉,通过LookAndFeel类可以切换系统默认的L&F或安装第三方的L&F库,以达到不同的视觉效果。 Swing源代码的学习可以帮助开发者深入理解其...

    swing图形用户界面程序设计.ppt

    这些类继承自`java.awt.Component`,并提供了自己的特性和功能,如JComponent类就支持边框设置、双缓冲、提示信息、键盘导航和可插入的L&F。 JComponent是Swing中大多数组件的基类,它提供了许多高级功能。例如,...

    编译原理复习课件

    //按钮,查询、取消、修改 JLabel label,L; //标签 //定义文本框 JTable table;//用来接收数据库中返回的信息 Object columnName[]={"图书名","图书号","单价","作者","出版社","入库时间"}; Object ar[][] =new ...

    Swing Examples

    6. **可定制的外观(LookAndFeel)**:Swing允许更改应用程序的外观,可以设置为系统默认的LookAndFeel,也可以使用Java提供的其他LookAndFeel,甚至自定义LookAndFeel。 7. **对话框(Dialogs)**:如JOptionPane...

    04747 Java语言程序设计(一) 2013年10月

    - **解析**: 抽象类不一定必须包含抽象方法,它也可以仅包含具体方法。抽象方法确实只能存在于抽象类中,而且抽象方法没有具体的实现体,这些选项A、C、D均正确。 4. **字符串拼接** - **题目**: 执行下列程序...

Global site tag (gtag.js) - Google Analytics