Java 马蒂斯正确设置面板的图像
我在将映像设置为pannel时遇到NetBeans资源管理问题: 这是我的非工作代码:Java 马蒂斯正确设置面板的图像,java,image,swing,netbeans,matisse,Java,Image,Swing,Netbeans,Matisse,我在将映像设置为pannel时遇到NetBeans资源管理问题: 这是我的非工作代码: try { BufferedImage myPicture = ImageIO.read(new File("images/3D.jpg")); JLabel picLabel = new JLabel(new ImageIcon(myPicture)); pnlMain.add(picLabel); //the main and only pannel made by matisse
try {
BufferedImage myPicture = ImageIO.read(new File("images/3D.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
pnlMain.add(picLabel); //the main and only pannel made by matisse is called pnlMain
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Cannot set image");
}
名为“图像”的文件夹位于主项目文件夹中。有几个文件夹:build、nbproject、src和“images”。我的问题是,程序运行,但它没有设置图像 有人建议我使用以下代码在不同的包中创建另一个类:
public class PanelImage extends JPanel{
private Image imag;
public PanelImage(Image img){
if(imagen != null){
this.imagen = img;
}
}
@Override
public void paint(Graphics g){
g.drawImage(img, 0,0, getWidth(), getHeight(), this);
setOpaque(false);
super.paint(g);
}
}
但是我找不到一个合适的方法来实现它 用于您的
图像面板
类
super.在所有其他东西之前先绘制[组件]
李>
paint
,而是覆盖paintComponent
paintComponent
方法ieset不透明()
中设置属性。此外,JPanel
默认为不透明getPreferredSize()
ProjectRoot
src
images
3D.jpg
ImageIO
确保路径正确。如果无效,将引发异常
URL url = getClass().getResource("/images/3D.jpg");
Image image = ImageIO.read(url);
图标
看到和