为什么JPanel的背景图片显示不出来???
classJStarPanelextendsJPanel{privateImagebackground=Toolkit.getDefaultToolkit().getIm...
class JStarPanel extends JPanel {
private Image background = Toolkit.getDefaultToolkit().getImage(
"Images/test.jpg");
private int width;
private int height;
public JStarPanel() {
setSize(Toolkit.getDefaultToolkit().getScreenSize());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();
width = d.width;// 宽
height = d.height;// 高
}
public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, width, height, this);
}
} 展开
private Image background = Toolkit.getDefaultToolkit().getImage(
"Images/test.jpg");
private int width;
private int height;
public JStarPanel() {
setSize(Toolkit.getDefaultToolkit().getScreenSize());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();
width = d.width;// 宽
height = d.height;// 高
}
public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, width, height, this);
}
} 展开
1个回答
展开全部
你的图片没有加载进来。
backgroundImage_URL=BackgroundPanel.class.getResource("image/background.jpg");
Image backgroundImage=Toolkit.getDefaultToolkit().getImage(backgroundImage_URL);
public void paintComponent(Graphics g) {
g.drawImage(backgroundImage, 0, 0, width, height, this);
}
背景图片要放在存放class文件的相对目录下。
backgroundImage_URL=BackgroundPanel.class.getResource("image/background.jpg");
Image backgroundImage=Toolkit.getDefaultToolkit().getImage(backgroundImage_URL);
public void paintComponent(Graphics g) {
g.drawImage(backgroundImage, 0, 0, width, height, this);
}
背景图片要放在存放class文件的相对目录下。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询