我想实现:选择图片然后在swing中显示出来,显示大小随图片大小改变而改变。

解决方案 »

  1.   

    在MenuItem里设置图片,显示大小随图片大小改变而改变
    我试了,好使,其他控件里不知道行不行
      

  2.   

    给个参考吧
    import   java.awt.Graphics;   
      import   java.awt.Image;   
      import   java.awt.Insets;   
      import   java.awt.image.BufferedImage;   
        
      import   javax.imageio.ImageIO;   
      import   javax.swing.*;   
      import   javax.swing.plaf.MenuItemUI;   
      import   javax.swing.plaf.basic.BasicPopupMenuUI;   
        
      public   class   BackgroundImgMenu   extends   JMenu   
      {   
      private   Image   bgImg;   
        
      public   BackgroundImgMenu(String   text,   Image   bgImg)   
      {   
      super(text);   
      this.bgImg   =   bgImg;   
      }   
        
      @Override   
      public   void   updateUI()   
      {   
              setUI((MenuItemUI)UIManager.getUI(this));   
        
              if   (getPopupMenu()   !=   null   )   
              {   
              getPopupMenu().setUI(new   PopupMenuUI());   
              }   
      }   
        
      @Override   
      public   JMenuItem   add(JMenuItem   menuItem)   
      {   
      menuItem.setOpaque(false);   
      return   super.add(menuItem);   
      }   
        
      private   class   PopupMenuUI   extends   BasicPopupMenuUI   
      {   
      @Override   
      public   void   paint(Graphics   g,   JComponent   c)   
      {   
      super.paint(g,   c);   
      Insets   insets   =   c.getInsets();   
        
      g.drawImage(bgImg,   insets.left,   insets.top,     
      c.getWidth()   -   insets.left   -   insets.right,   
      c.getHeight()   -   insets.top   -   insets.bottom,   c);   
      }   
      }   
        
      public   static   void   main(String[]   args)   throws   Exception   
      {   
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());   
        
      BufferedImage   img   =   ImageIO.read(BackgroundImgMenu.class.getResource("menubg.png"));   
        
      JMenuBar   menubar   =   new   JMenuBar();   
      JMenu   fileMenu   =   new   BackgroundImgMenu("File",   img);   
      JMenu   editMenu   =   new   BackgroundImgMenu("Edit",   img);   
      JMenu   editSubMenu   =   new   BackgroundImgMenu("SubMenu",   img);   
        
      editSubMenu.add(new   JMenuItem("SubMenuItem-1"));   
      editSubMenu.add(new   JMenuItem("SubMenuItem-2"));   
      editSubMenu.add(new   JMenuItem("SubMenuItem-3"));   
        
      fileMenu.add(new   JMenuItem("Open"));   
      fileMenu.add(new   JMenuItem("Save"));   
      fileMenu.add(new   JCheckBoxMenuItem("CheckboxMenuItem"));   
      fileMenu.add(new   JRadioButtonMenuItem("RadioButtonMenuItem"));   
      fileMenu.addSeparator();   
      fileMenu.add(new   JMenuItem("Exit"));   
        
      editMenu.add(new   JMenuItem("Copy"));   
      editMenu.add(new   JMenuItem("Paste"));   
      editMenu.add(new   JMenuItem("Cut"));   
      editMenu.add(editSubMenu);   
        
      menubar.add(fileMenu);   
      menubar.add(editMenu);   
        
      JFrame   f   =   new   JFrame("带背景图片的菜单");   
      f.setJMenuBar(menubar);   
      f.setSize(400,   400);   
      f.setLocationRelativeTo(null);   
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
      f.setVisible(true); }   
      }   
      
      

  3.   

    import java.util.Locale;
    import java.util.ResourceBundle;import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;import com.nec.zw.excise.common.IconFactory;
    import com.nec.zw.excise.common.ProgrammeConstant;public class CopyOfIconMenuExample extends JMenuBar {    /**
         * serialVersionUID
         */
        private static final long serialVersionUID = 6104121891993446467L;    private static final String DFAULT_RESOURCE_BASE_NAME = "com/nec/zw/resource";    private static final String DEFAULT_LANGUAGE = "";
        
        /**
         * IconMenuExampleのプロセスアイコンパス
         */
        public static final String PROGRAMMECONSTANT_PROCESS_ICON_PATH = "PROCESS_ICON_PATH";    /**
         * IconMenuExampleの成果物アイコンパス
         */
        public static final String PROGRAMMECONSTANT_FUNCTION_ICON_PATH = "FUNCTION_ICON_PATH";    /**
         * IconMenuExampleのリソースアイコンパス
         */
        public static final String PROGRAMMECONSTANT_RESOURCE_ICON_PATH = "RESOURCE_ICON_PATH";    /**
         * IconMenuExampleのプロセス項目名
         */
        public static final String PROGRAMMECONSTANT_ITEM_NAME_PROCESS = "ITEM_NAME_PROCESS";    /**
         * IconMenuExampleのプロセス項目名
         */
        public static final String PROGRAMMECONSTANT_ITEM_NAME_FUNCTION = "ITEM_NAME_FUNCTION";    /**
         * IconMenuExampleのプロセス項目名
         */
        public static final String PROGRAMMECONSTANT_ITEM_NAME_RESOURCE = "ITEM_NAME_RESOURCE";    public CopyOfIconMenuExample() {
            this.add(this.buildMenu());
        }    private JMenu buildMenu() {
            String displayName = this.getResourceContent(ProgrammeConstant.PROGRAMMECONSTANT_MENU_NAME);
            JMenu menu = new JMenu(displayName);        this.buildMenuItem(
                CopyOfIconMenuExample.PROGRAMMECONSTANT_ITEM_NAME_PROCESS,
                CopyOfIconMenuExample.PROGRAMMECONSTANT_PROCESS_ICON_PATH,
                menu);
            this.buildMenuItem(
                CopyOfIconMenuExample.PROGRAMMECONSTANT_ITEM_NAME_FUNCTION,
                CopyOfIconMenuExample.PROGRAMMECONSTANT_FUNCTION_ICON_PATH,
                menu);
            this.buildMenuItem(
                CopyOfIconMenuExample.PROGRAMMECONSTANT_ITEM_NAME_RESOURCE,
                CopyOfIconMenuExample.PROGRAMMECONSTANT_RESOURCE_ICON_PATH,
                menu);        return menu;
        }    private void buildMenuItem(String nameKey, String iconKey, JMenu menu) {
            IconFactory iconFactory = IconFactory.getInstance();
            String name = this.getResourceContent(nameKey);
            String iconpath = this.getResourceContent(iconKey);
            menu.add(new JMenuItem(name, iconFactory.getIcon(iconpath)));
        }    private String getResourceContent(String iconKey) {
            return this.getResourceBundle().getString(iconKey);
        }    private ResourceBundle getResourceBundle() {
            return ResourceBundle.getBundle(
                CopyOfIconMenuExample.DFAULT_RESOURCE_BASE_NAME,
                new Locale(CopyOfIconMenuExample.DEFAULT_LANGUAGE));
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            JFrame frame = new JFrame("IntroIconMenu");
            frame.setJMenuBar(new CopyOfIconMenuExample());
            frame.setVisible(true);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    资源文件:
    <!--IconMenuExampleResource-->
    IconMenuExample.frame.title=IconMenuExampleMENU_NAME=IconMenu
    ITEM_NAME_PROCESS=Process
    ITEM_NAME_FUNCTION=Function
    ITEM_NAME_RESOURCE=ResourcePROCESS_ICON_PATH=/icons/process.gif
    FUNCTION_ICON_PATH=/icons/function.gif
    RESOURCE_ICON_PATH=/icons/resource.gif
    再建个Icons包,放几个icon
      

  4.   

    好像代码没有摘干净
    import java.util.HashMap;
    import java.util.Map;import javax.swing.Icon;
    import javax.swing.ImageIcon;public class IconFactory {    /**
         * アイコンのMap
         */
        private final Map<String, Icon> iconMap = new HashMap<String, Icon>();    private static IconFactory factory = null;    public static IconFactory getInstance() {
            if (IconFactory.factory != null) {
                return IconFactory.factory;
            }
            return new IconFactory();
        }    public Icon getIcon(String iconPath) {
            Icon result = this.iconMap.get(iconPath);
            if (result == null) {
                try {
                    result = new ImageIcon(this.getClass().getResource(iconPath));
                } catch (java.lang.NullPointerException e) {
                    e.printStackTrace();
                    return null;
                }
                this.iconMap.put(iconPath, result);
            }
            return result;
        }
    }import java.util.Locale;
    import java.util.ResourceBundle;import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;import com.nec.zw.excise.common.IconFactory;public class CopyOfIconMenuExample extends JMenuBar {    /**
         * serialVersionUID
         */
        private static final long serialVersionUID = 6104121891993446467L;    private static final String DFAULT_RESOURCE_BASE_NAME = "com/nec/zw/resource";    private static final String DEFAULT_LANGUAGE = "";    /**
         * IconMenuExampleのプロセスアイコンパス
         */
        public static final String PROGRAMMECONSTANT_PROCESS_ICON_PATH = "PROCESS_ICON_PATH";    /**
         * IconMenuExampleの成果物アイコンパス
         */
        public static final String PROGRAMMECONSTANT_FUNCTION_ICON_PATH = "FUNCTION_ICON_PATH";    /**
         * IconMenuExampleのリソースアイコンパス
         */
        public static final String PROGRAMMECONSTANT_RESOURCE_ICON_PATH = "RESOURCE_ICON_PATH";    /**
         * IconMenuExampleのプロセス項目名
         */
        public static final String PROGRAMMECONSTANT_ITEM_NAME_PROCESS = "ITEM_NAME_PROCESS";    /**
         * IconMenuExampleのプロセス項目名
         */
        public static final String PROGRAMMECONSTANT_ITEM_NAME_FUNCTION = "ITEM_NAME_FUNCTION";    /**
         * IconMenuExampleのプロセス項目名
         */
        public static final String PROGRAMMECONSTANT_ITEM_NAME_RESOURCE = "ITEM_NAME_RESOURCE";    /**
         * IconMenuExampleのメニュー名
         */
        public static final String PROGRAMMECONSTANT_MENU_NAME = "MENU_NAME";    public CopyOfIconMenuExample() {
            this.add(this.buildMenu());
        }    private JMenu buildMenu() {
            String displayName =
                    this.getResourceContent(CopyOfIconMenuExample.PROGRAMMECONSTANT_MENU_NAME);
            JMenu menu = new JMenu(displayName);        this.buildMenuItem(
                CopyOfIconMenuExample.PROGRAMMECONSTANT_ITEM_NAME_PROCESS,
                CopyOfIconMenuExample.PROGRAMMECONSTANT_PROCESS_ICON_PATH,
                menu);
            this.buildMenuItem(
                CopyOfIconMenuExample.PROGRAMMECONSTANT_ITEM_NAME_FUNCTION,
                CopyOfIconMenuExample.PROGRAMMECONSTANT_FUNCTION_ICON_PATH,
                menu);
            this.buildMenuItem(
                CopyOfIconMenuExample.PROGRAMMECONSTANT_ITEM_NAME_RESOURCE,
                CopyOfIconMenuExample.PROGRAMMECONSTANT_RESOURCE_ICON_PATH,
                menu);        return menu;
        }    private void buildMenuItem(String nameKey, String iconKey, JMenu menu) {
            IconFactory iconFactory = IconFactory.getInstance();
            String name = this.getResourceContent(nameKey);
            String iconpath = this.getResourceContent(iconKey);
            menu.add(new JMenuItem(name, iconFactory.getIcon(iconpath)));
        }    private String getResourceContent(String iconKey) {
            return this.getResourceBundle().getString(iconKey);
        }    private ResourceBundle getResourceBundle() {
            return ResourceBundle.getBundle(
                CopyOfIconMenuExample.DFAULT_RESOURCE_BASE_NAME,
                new Locale(CopyOfIconMenuExample.DEFAULT_LANGUAGE));
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            JFrame frame = new JFrame("IntroIconMenu");
            frame.setJMenuBar(new CopyOfIconMenuExample());
            frame.setVisible(true);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    不摘代码类太多了 呵呵