swing图片显示 我想实现:选择图片然后在swing中显示出来,显示大小随图片大小改变而改变。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在MenuItem里设置图片,显示大小随图片大小改变而改变我试了,好使,其他控件里不知道行不行 给个参考吧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); } } 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=IconMenuITEM_NAME_PROCESS=ProcessITEM_NAME_FUNCTION=FunctionITEM_NAME_RESOURCE=ResourcePROCESS_ICON_PATH=/icons/process.gifFUNCTION_ICON_PATH=/icons/function.gifRESOURCE_ICON_PATH=/icons/resource.gif再建个Icons包,放几个icon 好像代码没有摘干净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); }}不摘代码类太多了 呵呵 java中的文件复制问题,求高手解答 关于抽象类和接口的疑问 关于DJ Native Swing - SWT的疑问 j2se中实现与网页通讯 如何读取文件中对象的个数 请问如何做一个像金山快译那样的浮动工具条,而不显示Frame! 在线等待,大家帮帮我啊,高分奉送! 关于gui的小问题,请帮忙解决以下!!谢谢! java 可以和MySQL连接吗? 有人说学FLASH对学面向对像有用,真的吗? java socket inputstream 的阻塞问题 我想建一个.properties的属性文件,怎么创建啊
我试了,好使,其他控件里不知道行不行
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); }
}
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
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);
}
}
不摘代码类太多了 呵呵