Swing 带标题栏的右键菜单 增强版
来源:互联网 发布:mac如何接受共享日历 编辑:程序博客网 时间:2024/05/17 23:53
这里主要通过PopupFactory来实现对菜单或右键弹出菜单添加标题,而不用从写JPopupMenu 或JMenu 。
效果如下图:
右键菜单:
JMenu:
代码如下:
import java.awt.*;import java.awt.event.MouseEvent;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import javax.swing.*;public class TitlePopupMenuFactory extends PopupFactory { private static final int POPUP_TITLE_WIDTH = 30; private Font font = new Font("Dialog", Font.BOLD, 13); @Override public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException { if (contents instanceof JPopupMenu || contents instanceof JMenu) { Dimension dim = contents.getPreferredSize(); dim.width += POPUP_TITLE_WIDTH; ((JComponent) contents).setBorder(null); JComponent comp = new JPanel(); comp.setBorder(UIManager.getBorder("PopupMenu.border")); comp.setPreferredSize(dim); comp.setLayout(new BorderLayout()); comp.add(contents, BorderLayout.CENTER); comp.add(new JLabel(createImage("Menu Title", dim.height)), BorderLayout.WEST); return super.getPopup(owner, comp, x, y); } return super.getPopup(owner, contents, x, y); } private ImageIcon createImage(String text, int height) { BufferedImage bi = new BufferedImage(POPUP_TITLE_WIDTH, height, BufferedImage.TYPE_INT_ARGB); ImageIcon image = new ImageIcon(bi); Graphics2D g2d = bi.createGraphics(); GradientPaint paint = new GradientPaint(0, 0, Color.yellow, POPUP_TITLE_WIDTH, 10, Color.red, true); g2d.setPaint(paint); g2d.fillRect(0, 0, bi.getWidth(), bi.getHeight()); AffineTransform at = new AffineTransform(); at.rotate(-Math.PI / 2); g2d.setTransform(at); g2d.setColor(Color.black); g2d.setFont(font); int stringWidth = g2d.getFontMetrics().stringWidth(text); g2d.drawString(text, -(height + stringWidth) / 2, bi.getWidth() / 2); return image; } //test public static void main(String[] args) { PopupFactory.setSharedInstance(new TitlePopupMenuFactory()); final JFrame frame = new JFrame(); frame.setSize(600, 500); frame.setTitle("ImageMenu"); JMenu jmenu = new JMenu("Menu"); jmenu.add(new JMenuItem("MenuItem1")); jmenu.add(new JMenuItem("MenuItem2")); jmenu.add(new JMenuItem("MenuItem3")); jmenu.add(new JMenuItem("MenuItem4")); JMenuBar menuBar = new JMenuBar(); menuBar.add(jmenu); frame.setJMenuBar(menuBar); final JPopupMenu menu = new JPopupMenu("Windows XP Perfessional"); menu.add(new JMenuItem("Winzip 8.0")); menu.addSeparator(); menu.add(new JMenuItem("Programs")); menu.addSeparator(); menu.add(new JMenuItem("Shut Down...")); JLabel label = new JLabel("Right click me to show image popup menu."); label.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { menu.show(frame, e.getPoint().x, e.getPoint().y); } } }); frame.getContentPane().add(label, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }}
- Swing 带标题栏的右键菜单 增强版
- Swing 带标题栏的右键菜单(三)增强版
- Swing 带标题栏的右键菜单
- Swing 带标题栏的右键菜单
- Swing 带标题栏的右键菜单
- Swing 带标题栏的右键菜单(一)
- Swing 带标题栏的右键菜单(二)
- Swing创建带图标的右键弹出菜单
- Java Swing右键菜单
- swing右键菜单
- Swing探索:创建带图像的菜单
- 带右键菜单的树形控件(1)
- 带右键菜单的树形控件(1)
- Ext 带右键菜单的树
- 带滚动条的右键菜单
- swing 菜单+右键菜单+二级菜单实现
- [经验分享]DBGridEH的标题栏,右键菜单实现方法
- 使用SystemMenu(右键点标题栏出来的菜单)
- XAF笔记01
- a,b比较和交换问题
- Android ApiDemo 笔记(二)Graphics和View
- 嵌入式Linux+ARM开发环境搭建
- findbugs
- Swing 带标题栏的右键菜单 增强版
- ubuntu12.10 安装hadoop1.0.4伪分布式
- 黑马程序员_java入门_位运算
- 国家测绘地理信息局黑龙江基础地理信息中心----网站访问情况
- 排序算法的比较与选择
- ShellExecute详解
- 几个带双下划线的宏
- 常见的C/C++ 编译器和两个环境
- findbugs ant 代码审核工具