Swing 带标题栏的右键菜单(三)增强版
来源:互联网 发布:edius8软件下载 编辑:程序博客网 时间:2024/05/18 03:32
这里主要通过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创建带图标的右键弹出菜单
- 使用SystemMenu(右键点标题栏出来的菜单)
- Java Swing右键菜单
- swing右键菜单
- Swing探索:创建带图像的菜单
- 带右键菜单的树形控件(1)
- 带右键菜单的树形控件(1)
- Ext 带右键菜单的树
- 带滚动条的右键菜单
- swing 菜单+右键菜单+二级菜单实现
- [经验分享]DBGridEH的标题栏,右键菜单实现方法
- APACHE配置文件中文详解
- Tomcat(免安装版)的安装与配置
- 【Unity3D】【NGUI】UIScrollBar
- 分析av_open_input_file的主要流程
- 包含《三目运算、BT交换、数组查询》的超级练习!
- Swing 带标题栏的右键菜单(三)增强版
- C指针原理(31)-win32汇编及.NET调试
- 八大Linux/Unix服务器内存转储工具
- HTML5自学手册--HTML5简介(1)
- Wine的中文显示与字体设置
- ORACLE ORA-01722 错误解决
- C++写的与http交互的类CHttpClient
- java 泛型 extends 多个
- cocos2dx初接触之创建工程