Java Swing Ribbon(Flamingo)的使用05:小组件

这篇文章将简单介绍一下Ribbon中常用的控件空间小组件的使用,包括:设置左上角圆形菜单图标、设置左上角快捷工具条、设置右上角帮助按钮等。


在看这篇文章之前建议先看如下的一些文章。当然如果对Flamino-Ribbon已经有一些基本的了解的则不用看了。

01.关于Flamingo-Ribbon的安装与使用:Flamingo-Ribbon的使用01:安装与初始使用

02.关于Fiamingo-Ribbon界面风格设置:Flamingo-Ribbon的使用02:更改界面风格

03.关于Flamingo-Ribbon按钮样式更改:Flamingo-Ribbon的使用03:按钮中添加图片和更改样式

04.关于Flamingo-Ribbon按钮模式更改: Flamingo-Ribbon的使用04:按钮(JCommandButton)模式更改


1、设置左上角圆形菜单图标

Office 2007中在软件的左上角可以添加圆形按钮菜单,在Flamingo-Ribbon中也可以添加同样的圆形菜单按钮。

代码如下:

// 设置左上角圆图标
private void setApplicationMenu(JRibbon ribbon) {
	RibbonApplicationMenu ram = new RibbonApplicationMenu();
	ribbon.setApplicationMenu(ram); // 左上角的圆图标
}

2、设置左上角快捷工具条

Office 2007中在软件的左上角可以设置常用的按钮的快捷方式,比如复制、回退、格式刷等,在Flamingo-Ribbon中也可以添加同样的快捷按钮。

代码如下:

//左上角的工具条
private void setTaskbarComponent(JRibbon ribbon) {
	JCommandButton button1 = new JCommandButton("打开");
	JCommandButton button2 = new JCommandButton("复制");
	JCommandButton button3 = new JCommandButton("格式化");
	
//		button1.addActionListener(actionListener1);
//		button2.addActionListener(actionListener2);
//		button3.addActionListener(actionListener3);
	
	attachImage(button1, "resources/AddFile.png");
	attachImage(button2, "resources/SetEnvironment.png");
	attachImage(button3, "resources/RunAgent.png");
	
	ribbon.addTaskbarComponent(button1);
	ribbon.addTaskbarComponent(button2);
	ribbon.addTaskbarComponent(button3);
}

3、设置右上角帮助按钮

在很多桌面软件中,会在右上角放置一个帮组按钮,点击该按钮就可以弹出对该软件的帮助说明。在 Flamingo-Ribbon中也可以添加该按钮。其代码如下:

//配置帮助按钮
private void configureHelp(JRibbon ribbon) {
    ribbon.configureHelp(getResizableIconFromResource(
            "resources/Help.png"), new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "Help");
        }
    });
}


上述所有代码的完整形式如下:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
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.JRibbon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenu;
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);
                
                JRibbonBand band = new JRibbonBand("文件", null); //新建一个Band
                
                band.setResizePolicies((List) Arrays.asList(
                		new CoreRibbonResizePolicies.None(band.getControlPanel()),
                		new IconRibbonBandResizePolicy(band.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"));
                band.addCommandButton(button1, RibbonElementPriority.TOP);
                band.addCommandButton(button2, RibbonElementPriority.MEDIUM);
                band.addCommandButton(button3, RibbonElementPriority.MEDIUM);
                band.addCommandButton(button4, RibbonElementPriority.MEDIUM);
                
                //新建一个Task,并将Band添加到该Task中去
                RibbonTask task1 = new RibbonTask("One", band);
                
                JRibbon ribbon = frame.getRibbon();
                ribbon.addTask(task1);

                frame.setApplicationMenu(ribbon); //左上角圆图标
                frame.setTaskbarComponent(ribbon); //左上角的工具条
                frame.configureHelp(ribbon); //右上角的帮助按钮
                
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
    
	//左上角的工具条
    private void setTaskbarComponent(JRibbon ribbon) {
		JCommandButton button1 = new JCommandButton("打开");
		JCommandButton button2 = new JCommandButton("复制");
		JCommandButton button3 = new JCommandButton("格式化");
		
//		button1.addActionListener(actionListener1);
//		button2.addActionListener(actionListener2);
//		button3.addActionListener(actionListener3);
		
		attachImage(button1, "cn/edu/njnu/resources/AddFile.png");
		attachImage(button2, "cn/edu/njnu/resources/SetEnvironment.png");
		attachImage(button3, "cn/edu/njnu/resources/RunAgent.png");
		
		ribbon.addTaskbarComponent(button1);
		ribbon.addTaskbarComponent(button2);
		ribbon.addTaskbarComponent(button3);
	}
    
    private void attachImage(JCommandButton btn, String key) {
		btn.setIcon(getResizableIconFromResource(key));
	}
    
	//配置帮助按钮
	private void configureHelp(JRibbon ribbon) {
		ribbon.configureHelp(getResizableIconFromResource(
				"resources/Help.png"), new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Help");
			}
		});
	}
    
	// 设置左上角圆图标
 	private void setApplicationMenu(JRibbon ribbon) {
 		RibbonApplicationMenu ram = new RibbonApplicationMenu();
 		ribbon.setApplicationMenu(ram); // 左上角的圆图标
 	}
}

运行上述代码结果如下:


从上图可以看到:

1、在左上角多了一个圆形菜单图标,在这个菜单中可以自行添加自己想要的功能;

2、在圆形菜单的旁边多了三个快捷按钮,可以自行添加按钮的个数和按钮的触发事件;

3、在右上角脱了一个帮助按钮。


全文完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值