这篇文章主要介绍在文章主要介绍在Flamingo的Ribbon中开发出类似Office Word 2007中“样式”长廊(Gallery)的组件。
在看这篇文章之前建议先看如下的一些文章。当然如果对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)模式更改
05.关于Flamingo-Ribbon小组件的添加:Flamingo-Ribbon的使用05:小组件
06.关于Flamingo-Ribbon的常规组件项:Flamingo-Ribbon的使用06:添加JComponent组件
Flamingo Ribbon的Gallery中其实就是一个一个的Button的集合(List),该Button是JCommandToggleButton对象(JCommandToggleButton与JCommandButton的区别是JCommandToggleButton按下去不会自动起来,而JCommandButton按下去会自动起来)。根据该Button的List新建一个StringValuePair对象,随后再将该StringValuePair添加到一个List中去。最后新建Map并将Gallery添加到Band中去。
此外,还可以在Gallery的下放添加一个按钮,比如“更多颜色”等。这个按钮需RibbonGalleryPopupCallback对象去完成:
其完整代码如下:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.pushingpixels.flamingo.api.common.JCommandMenuButton;
import org.pushingpixels.flamingo.api.common.JCommandToggleButton;
import org.pushingpixels.flamingo.api.common.StringValuePair;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
import org.pushingpixels.flamingo.api.common.popup.JCommandPopupMenu;
import org.pushingpixels.flamingo.api.ribbon.JRibbon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand.RibbonGalleryPopupCallback;
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 MainFrame2 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(
MainFrame2.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() {
@Override
public void run() {
MainFrame2 frame = new MainFrame2();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//新建一个Task,并将Band添加到该Task中去
RibbonTask task1 = new RibbonTask("One", frame.getGalleryBand());
JRibbon ribbon = frame.getRibbon();
ribbon.addTask(task1);
frame.pack();
frame.setVisible(true);
}
});
}
private JRibbonBand getGalleryBand() {
JRibbonBand colorBand = new JRibbonBand("颜色设置", null);
colorBand.setResizePolicies(((List) Arrays.asList(
new CoreRibbonResizePolicies.None(colorBand.getControlPanel()),
new IconRibbonBandResizePolicy(colorBand.getControlPanel()))));
JCommandToggleButton button1 = new JCommandToggleButton("blue");
JCommandToggleButton button2 = new JCommandToggleButton("black");
JCommandToggleButton button3 = new JCommandToggleButton("red");
JCommandToggleButton button4 = new JCommandToggleButton("darkGray");
JCommandToggleButton button5 = new JCommandToggleButton("gray");
JCommandToggleButton button6 = new JCommandToggleButton("green");
JCommandToggleButton button7 = new JCommandToggleButton("orange");
JCommandToggleButton button8 = new JCommandToggleButton("cyan");
JCommandToggleButton button9 = new JCommandToggleButton("pink");
JCommandToggleButton button10 = new JCommandToggleButton("yellow");
JCommandToggleButton button11 = new JCommandToggleButton("lightGray");
JCommandToggleButton button12 = new JCommandToggleButton("white");
button1.setIcon(paintSingleColorIcon(Color.blue, 55, 50));
button2.setIcon(paintSingleColorIcon(Color.black, 55, 50));
button3.setIcon(paintSingleColorIcon(Color.red, 55, 50));
button4.setIcon(paintSingleColorIcon(Color.darkGray, 55, 50));
button5.setIcon(paintSingleColorIcon(Color.gray, 55, 50));
button6.setIcon(paintSingleColorIcon(Color.green, 55, 50));
button7.setIcon(paintSingleColorIcon(Color.orange, 55, 50));
button8.setIcon(paintSingleColorIcon(Color.cyan, 55, 50));
button9.setIcon(paintSingleColorIcon(Color.pink, 55, 50));
button10.setIcon(paintSingleColorIcon(Color.yellow, 55, 50));
button11.setIcon(paintSingleColorIcon(Color.lightGray, 55, 50));
button12.setIcon(paintSingleColorIcon(Color.white, 55, 50));
List<JCommandToggleButton> buttons = new ArrayList<JCommandToggleButton>();
buttons.add(button1);
buttons.add(button2);
buttons.add(button3);
buttons.add(button4);
buttons.add(button5);
buttons.add(button6);
buttons.add(button7);
buttons.add(button8);
buttons.add(button9);
buttons.add(button10);
buttons.add(button11);
buttons.add(button12);
StringValuePair<List<JCommandToggleButton>> svp =
new StringValuePair<List<JCommandToggleButton>>("常规色彩", buttons);
List<StringValuePair<List<JCommandToggleButton>>> svps =
new ArrayList<StringValuePair<List<JCommandToggleButton>>>();
svps.add(svp);
Map<RibbonElementPriority, Integer> prefers = new HashMap<RibbonElementPriority, Integer>();
prefers.put(RibbonElementPriority.LOW, 2);
prefers.put(RibbonElementPriority.MEDIUM, 5);
prefers.put(RibbonElementPriority.TOP, 5); //一行的最大数数目
colorBand.addRibbonGallery("颜色设置", svps,
prefers, 5, 3, RibbonElementPriority.TOP);
colorBand.setRibbonGalleryPopupCallback("颜色设置",
new RibbonGalleryPopupCallback() {
@Override
public void popupToBeShown(JCommandPopupMenu menu) {
menu.addMenuButton(new JCommandMenuButton("更多...",
paintMultiColorIcon(55, 50)));
}
});
return colorBand;
}
/**
* 根据指定颜色绘制指点大小的图标(ImageIcon)
* @param color
* @param width
* @param height
* @return
*/
public final ResizableIcon paintSingleColorIcon(Color color, int width, int height) {
BufferedImage image =
new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics2d = image.createGraphics();
graphics2d.setColor(color);
graphics2d.fill3DRect(0, 0, width, height, true);
return ImageWrapperResizableIcon.getIcon(image, new Dimension(width, height));
}
/**
* 创建色彩斑斓的图标
* @param width
* @param height
* @return
*/
public final ResizableIcon paintMultiColorIcon(int width, int height) {
BufferedImage image =
new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for(int i=0; i<width; i++) {
for(int j=0; j<height; j++) {
Color c = new Color(i * 255 / width, j * 255 / height, 30);
image.setRGB(i, j, c.getRGB());
}
}
return ImageWrapperResizableIcon.getIcon(image, new Dimension(width, height));
}
}
运行上述代码后出来的界面如下:
点击下拉按钮将长廊(Gallery)拉下来后如下:
本章完。