java label的图片无法显示
packageui;importjava.awt.Dimension;importjava.awt.Toolkit;importjavax.swing.ImageIcon...
package ui;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FrameGame extends JFrame{
JPanel PanelStart=new JPanel();
public FrameGame(){
this.setTitle("坑啊");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setResizable(false);
//居中
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();
int x = (screen.width-this.getWidth())/2;
int y = (screen.height-this.getHeight())/2;
this.setLocation(x, y);//
this.add( PanelStart);
}
public void PanelStart(){
JLabel bg01=new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("picture/background/bg01.jpg"))));
bg01.setLocation(0, 0);
bg01.setSize(800,600);
PanelStart.add(bg01);
}
} 展开
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FrameGame extends JFrame{
JPanel PanelStart=new JPanel();
public FrameGame(){
this.setTitle("坑啊");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setResizable(false);
//居中
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screen = toolkit.getScreenSize();
int x = (screen.width-this.getWidth())/2;
int y = (screen.height-this.getHeight())/2;
this.setLocation(x, y);//
this.add( PanelStart);
}
public void PanelStart(){
JLabel bg01=new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("picture/background/bg01.jpg"))));
bg01.setLocation(0, 0);
bg01.setSize(800,600);
PanelStart.add(bg01);
}
} 展开
2个回答
展开全部
JLabel bg01=new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("picture/background/bg01.jpg"))));更改为:
JLabel bg01 =new JLabel();
ImageIcon icon= new ImageIcon("picture/background/bg01.jpg"); //路径一定要写对
bg01.setIcon(icon);
getImage(getClass().getResource("picture/background/bg01.jpg"))));更改为:
JLabel bg01 =new JLabel();
ImageIcon icon= new ImageIcon("picture/background/bg01.jpg"); //路径一定要写对
bg01.setIcon(icon);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询