swing中用什么控件放图比较好啊(.bmp格式图) 另外还想实现对该控件点击右键,可以选择要插入的图片,并可以实现放大,缩小,旋转等功能,不知道用什么控件比较好,求助各位前辈指点,也让我少走些弯路,多谢了先! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 随便都行,比如JPanel,JLabel等等,右键菜单需要编程 JComponent,想实现放图,java没有现成的组件,学要自己写,不如用JComponent JPanel,右键事件: public void mousePressed(MouseEvent event) { int selRow = tree.getRowForLocation(event.getX(), event.getY()); if(event.getButton() == MouseEvent.BUTTON3 && (selRow != -1)) { TreePath selPath = tree.getPathForLocation(event.getX(), event.getY()); if(!tree.isPathSelected(selPath)) { tree.setSelectionPath(selPath); } } } public void mouseClicked(MouseEvent event) { PopupMenu popupMenu = new PopupMenu(); int selRow = tree.getRowForLocation(event.getX(), event.getY()); TreePath selPath = tree.getPathForLocation(event.getX(), event.getY()); if(event.getButton() == MouseEvent.BUTTON3 && (selRow != -1)) { if(tree.isPathSelected(selPath)) { popupMenu.showMenu(event.getX(),event.getY()); }else { tree.setSelectionPath(selPath); popupMenu.showMenu(event.getX(),event.getY()); } } } 谢谢楼上的几位热心人哦,我开始使用的是swt的Canvas控件来实现的,但是现在又要在swing上实现,很是郁闷。再次谢过楼上的哦 建议自己写一个自定义控件,这样似乎更方便,可参考java.awt.Graphics中与image相关函数 简单,呵呵import java.awt.Graphics;import java.awt.Image;import javax.swing.JPanel;/** * 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。 * 未设置背景图片的情况下,同JPanel。 * * @author 003 */public class JImagePane extends JPanel{ private static final long serialVersionUID = -8251916094895167058L; /** * 居中 */ public static final String CENTRE = "Centre"; /** * 平铺 */ public static final String TILED = "Tiled"; /** * 拉伸 */ public static final String SCALED = "Scaled"; /** * 背景图片 */ private Image backgroundImage; /** * 背景图片显示模式 */ private String imageDisplayMode; /** * 背景图片显示模式索引(引入此属性有助于必要时扩展) */ private int modeIndex; /** * 构造一个没有背景图片的JImagePane */ public JImagePane() { this(null, CENTRE); } /** * 构造一个具有指定背景图片和指定显示模式的JImagePane * @param image 背景图片 * @param modeName 背景图片显示模式 */ public JImagePane(Image image, String modeName) { super(); setBackgroundImage(image); setImageDisplayMode(modeName); } /** * 设置背景图片 * @param image 背景图片 */ public void setBackgroundImage(Image image) { this.backgroundImage = image; this.repaint(); } /** * 获取背景图片 * @return 背景图片 */ public Image getBackgroundImage() { return backgroundImage; } /** * 设置背景图片显示模式 * @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE */ public void setImageDisplayMode(String modeName) { if(modeName != null) { modeName = modeName.trim(); //居中 if(modeName.equalsIgnoreCase(CENTRE)) { this.imageDisplayMode = CENTRE; modeIndex = 0; } //平铺 else if(modeName.equalsIgnoreCase(TILED)) { this.imageDisplayMode = TILED; modeIndex = 1; } //拉伸 else if(modeName.equalsIgnoreCase(SCALED)) { this.imageDisplayMode = SCALED; modeIndex = 2; } this.repaint(); } } /** * 获取背景图片显示模式 * @return 显示模式 */ public String getImageDisplayMode() { return imageDisplayMode; } /** * 绘制组件 * @see javax.swing.JComponent#paintComponent(Graphics) */ @Override protected void paintComponent(Graphics g) { super.paintComponent(g); //如果设置了背景图片则显示 if(backgroundImage != null) { int width = this.getWidth(); int height = this.getHeight(); int imageWidth = backgroundImage.getWidth(this); int imageHeight = backgroundImage.getHeight(this); switch(modeIndex) { //居中 case 0: { int x = (width - imageWidth) / 2; int y = (height - imageHeight) / 2; g.drawImage(backgroundImage, x, y, this); break; } //平铺 case 1: { for(int ix = 0; ix < width; ix += imageWidth) { for(int iy = 0; iy < height; iy += imageHeight) { g.drawImage(backgroundImage, ix, iy, this); } } break; } //拉伸 case 2: { g.drawImage(backgroundImage, 0, 0, width, height, this); break; } } } }} 干嘛用bmp图呢,转成png吧…… 刚学servlet,快疯掉了 为什么java输出菱形时出现死循环了 已经知道某个链接在网页上的位置,怎样用js自动打开这个链接呢? jdk1.4中怎么得到打印机的分辨率 Socket FTP 流的同步问题,本地流用FTPClient向服务端写文件后,服务端的文件不完整 读取数据库问题,很简单的。 hibernate的配置问题。 字符串操作的简单问题 好奇怪啊,大家帮忙啊 applet能否直接连数据库? 使用javah报错问题。 包内文件路径问题
public void mousePressed(MouseEvent event) {
int selRow = tree.getRowForLocation(event.getX(), event.getY());
if(event.getButton() == MouseEvent.BUTTON3 && (selRow != -1)) {
TreePath selPath = tree.getPathForLocation(event.getX(), event.getY());
if(!tree.isPathSelected(selPath)) {
tree.setSelectionPath(selPath);
}
}
}
public void mouseClicked(MouseEvent event) {
PopupMenu popupMenu = new PopupMenu();
int selRow = tree.getRowForLocation(event.getX(), event.getY());
TreePath selPath = tree.getPathForLocation(event.getX(), event.getY());
if(event.getButton() == MouseEvent.BUTTON3 && (selRow != -1)) {
if(tree.isPathSelected(selPath)) {
popupMenu.showMenu(event.getX(),event.getY());
}else {
tree.setSelectionPath(selPath);
popupMenu.showMenu(event.getX(),event.getY());
}
}
}
再次谢过楼上的哦
import java.awt.Image;import javax.swing.JPanel;/**
* 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。
* 未设置背景图片的情况下,同JPanel。
*
* @author 003
*/
public class JImagePane extends JPanel
{
private static final long serialVersionUID = -8251916094895167058L;
/**
* 居中
*/
public static final String CENTRE = "Centre";
/**
* 平铺
*/
public static final String TILED = "Tiled"; /**
* 拉伸
*/
public static final String SCALED = "Scaled"; /**
* 背景图片
*/
private Image backgroundImage;
/**
* 背景图片显示模式
*/
private String imageDisplayMode; /**
* 背景图片显示模式索引(引入此属性有助于必要时扩展)
*/
private int modeIndex; /**
* 构造一个没有背景图片的JImagePane
*/
public JImagePane()
{
this(null, CENTRE);
}
/**
* 构造一个具有指定背景图片和指定显示模式的JImagePane
* @param image 背景图片
* @param modeName 背景图片显示模式
*/
public JImagePane(Image image, String modeName)
{
super();
setBackgroundImage(image);
setImageDisplayMode(modeName);
}
/**
* 设置背景图片
* @param image 背景图片
*/
public void setBackgroundImage(Image image)
{
this.backgroundImage = image;
this.repaint();
} /**
* 获取背景图片
* @return 背景图片
*/
public Image getBackgroundImage()
{
return backgroundImage;
} /**
* 设置背景图片显示模式
* @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE
*/
public void setImageDisplayMode(String modeName)
{
if(modeName != null)
{
modeName = modeName.trim();
//居中
if(modeName.equalsIgnoreCase(CENTRE))
{
this.imageDisplayMode = CENTRE;
modeIndex = 0;
}
//平铺
else if(modeName.equalsIgnoreCase(TILED))
{
this.imageDisplayMode = TILED;
modeIndex = 1;
}
//拉伸
else if(modeName.equalsIgnoreCase(SCALED))
{
this.imageDisplayMode = SCALED;
modeIndex = 2;
}
this.repaint();
}
} /**
* 获取背景图片显示模式
* @return 显示模式
*/
public String getImageDisplayMode()
{
return imageDisplayMode;
} /**
* 绘制组件
* @see javax.swing.JComponent#paintComponent(Graphics)
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//如果设置了背景图片则显示
if(backgroundImage != null)
{
int width = this.getWidth();
int height = this.getHeight();
int imageWidth = backgroundImage.getWidth(this);
int imageHeight = backgroundImage.getHeight(this); switch(modeIndex)
{
//居中
case 0:
{
int x = (width - imageWidth) / 2;
int y = (height - imageHeight) / 2;
g.drawImage(backgroundImage, x, y, this);
break;
}
//平铺
case 1:
{
for(int ix = 0; ix < width; ix += imageWidth)
{
for(int iy = 0; iy < height; iy += imageHeight)
{
g.drawImage(backgroundImage, ix, iy, this);
}
}
break;
}
//拉伸
case 2:
{
g.drawImage(backgroundImage, 0, 0, width, height, this);
break;
}
}
}
}
}