Java Swing Ribbon(Flamingo)的使用03:按钮样式

关于Flamingo-Ribbon的介绍和安装参见: Flamingo-Ribbon

下面主要介绍如何Ribbon中按钮(JCommandButton)的一些操作,包括添加图片和更改按钮样式等。


1 添加按钮(JCommandButton)图片

import java.awt.Dimension;
import java.util.Arrays;
import java.util.List;
import javax.swing.SwingUtilities;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.RibbonTask;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
import org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy;

public class MainFrame extends JRibbonFrame {
    static {
        //获取系统样式
        try {
            javax.swing.UIManager.setLookAndFeel(
                  "org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //根据图片的地址获取该图片,返回ResizableIcon
    public static ResizableIcon getResizableIconFromResource(String resource) {
        return ImageWrapperResizableIcon.getIcon(
                MainFrame.class.getClassLoader().getResource(resource), 
                new Dimension(48, 48));
    }
    public static void main(String[] args) {
         JFrame.setDefaultLookAndFeelDecorated(true); //windows功能失效
         JDialog.setDefaultLookAndFeelDecorated(true); //Dialog功能失效
         SwingUtilities.invokeLater(new Runnable() {
            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public void run() {
                MainFrame frame = new MainFrame();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                JRibbonBand band1 = new JRibbonBand("Hello", null); //新建一个Band
                band1.setResizePolicies((List) Arrays.asList(
                        new CoreRibbonResizePolicies.None(band1.getControlPanel()),
                        new IconRibbonBandResizePolicy(band1.getControlPanel())));
                JCommandButton button1 = 
                        new JCommandButton("Square", 
                        getResizableIconFromResource("resources/Project.png"));
                JCommandButton button2 = 
                        new JCommandButton("Circle", 
                        getResizableIconFromResource("resources/Clear.png"));
                JCommandButton button3 = 
                        new JCommandButton("Triangle", 
                        getResizableIconFromResource("resources/ZoomOut.png"));
                JCommandButton button4 = 
                        new JCommandButton("Star", 
                        getResizableIconFromResource("resources/ZoomIn.png"));
                band1.addCommandButton(button1, RibbonElementPriority.TOP);
                band1.addCommandButton(button2, RibbonElementPriority.MEDIUM);
                band1.addCommandButton(button3, RibbonElementPriority.MEDIUM);
                band1.addCommandButton(button4, RibbonElementPriority.MEDIUM);
                //新建一个Task,并将Band添加到该Task中去
                RibbonTask task1 = new RibbonTask("One", band1);
                //先获取Ribbon,然后在Ribbon上添加一个Task
                frame.getRibbon().addTask(task1);
            }
        });
    }
}

运行上述代码,其结果如下:


其中新建了一个方法:getResizableIconFromResource(String),这个方法可以根据传进来的图片地址获取该图片并返回一个ResizableIcon对象。在新建JCommandButton时将ResizableIcon对象作为其中一个参数就可以将图片添加到按钮中。


2 更改按钮(JCommandButton)样式

2.1 Mirror

设置Mirror样式时,在设置Band时如下设置:

band1.setResizePolicies((List) Arrays.asList(
            new CoreRibbonResizePolicies.Mirror(band1.getControlPanel()),
		new IconRibbonBandResizePolicy(band1.getControlPanel())));
运行结果如下:


2.2 High2Low

设置High2Low样式时,在设置Band时如下设置:

band1.setResizePolicies((List) Arrays.asList(
            new CoreRibbonResizePolicies.<span style="color:red;background:lime;">High2Low</span>(band1.getControlPanel()),
		new IconRibbonBandResizePolicy(band1.getControlPanel())));
运行结果如下:



2.2 High2Mid

设置High2Low样式时,在设置Band时如下设置:

band1.setResizePolicies((List) Arrays.asList(
            new CoreRibbonResizePolicies.<span style="color:red;background:lime;">High2Low</span>(band1.getControlPanel()),
		new IconRibbonBandResizePolicy(band1.getControlPanel())));
运行结果如下:


此外,当界面变小时,按钮也会跟着隐藏,如下图:


当界面变小时,就会变成这样:

全文完。


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值