[Java] java 创建带下拉阴影的菜单 →→→→→进入此内容的聊天室

来自 , 2019-04-20, 写在 Java, 查看 149 次.
URL http://www.code666.cn/view/d7322ed7
  1. public class CustomPopupMenuUI extends BasicPopupMenuUI {
  2.         public static ComponentUI createUI(JComponent c) {        
  3.             return new CustomPopupMenuUI();
  4.         }
  5.         public Popup getPopup(JPopupMenu popup, int x, int y) {
  6.             Popup pp = super.getPopup(popup,x,y);
  7.             JPanel panel = (JPanel)popup.getParent();
  8.             panel.setBorder(new ShadowBorder(3,3));
  9.             panel.setOpaque(false);
  10.             return pp;
  11.  
  12.         }
  13.     }
  14.     class ShadowBorder extends AbstractBorder {
  15.         int xoff, yoff;
  16.         Insets insets;
  17.         public ShadowBorder(int x, int y) {
  18.             this.xoff = x;
  19.             this.yoff = y;
  20.             insets = new Insets(0,0,xoff,yoff);
  21.  
  22.         }
  23.         public Insets getBorderInsets( Component c ) {
  24.             return insets;
  25.         }
  26.  
  27.         public void paintBorder(Component comp, Graphics g,
  28.         int x, int y, int width, int height) {
  29.         g.setColor(Color.black);
  30.         g.translate(x,y);
  31.         // draw right side
  32.         g.fillRect(width-xoff, yoff, xoff, height-yoff);
  33.         // draw bottom side
  34.         g.fillRect(xoff, height-yoff, width-xoff, yoff);
  35.         g.translate(-x,-y);
  36.     }
  37. }
  38.  
  39.     public class MenuTest {
  40.         public static void main(String[] args) throws Exception {
  41.             UIManager.put("PopupMenuUI","CustomPopupMenuUI");
  42.             JFrame frame = new JFrame();
  43.             JMenuBar mb = new JMenuBar();
  44.             frame.setJMenuBar(mb);
  45.  
  46.             JMenu menu = new JMenu("File");
  47.             mb.add(menu);
  48.             menu.add(new JMenuItem("Open"));
  49.             menu.add(new JMenuItem("Save"));
  50.             menu.add(new JMenuItem("Close"));
  51.             menu.add(new JMenuItem("Exit"));
  52.             menu = new JMenu("Edit");
  53.             mb.add(menu);
  54.             menu.add(new JMenuItem("Cut"));
  55.             menu.add(new JMenuItem("Copy"));
  56.             menu.add(new JMenuItem("Paste"));
  57.             menu.add(new JMenuItem("Paste Special.."));
  58.             frame.getContentPane().setLayout(new BorderLayout());
  59.             frame.getContentPane().add("North",new JButton("Button"));
  60.             frame.getContentPane().add("Center",new JLabel("a label"));
  61.             frame.getContentPane().add("South",new JCheckBox("checkbox"));
  62.             frame.pack();
  63.             frame.setSize(200,150);
  64.             frame.show();
  65.  
  66.         }
  67.     }
  68.  
  69. //源代码片段来自云代码http://yuncode.net
  70.                        

回复 "java 创建带下拉阴影的菜单"

这儿你可以回复上面这条便签

captcha