java背景图片显示不出来,必须要先缩放窗体才能正常显示图片
packagegame;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassCe...
package game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ceshi extends JFrame{
public Ceshi()
{
super("此为测试画面!");
Backgound backg1=new Backgound();
Container contentpane=getContentPane();
contentpane.add(backg1);
backg1.setBorder(BorderFactory.createEtchedBorder());
setBounds(100,20,800,800);
//setOpaque(true);
setVisible(true);
}
}
class Backgound extends JPanel{
public Image backg;
public Backgound()
{
super();
setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
backg= Toolkit.getDefaultToolkit().getImage("image/背景.jpg");
g.drawImage(backg,30,30,300,400,null);
}
}
主函数在这里
package game;
public class start {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ceshi c=new Ceshi();
c.setBounds(50,60,600,700);
c.setVisible(false);
c.repaint();
c.repaint();
c.setVisible(true);
//c.setVisible(false);
//c.setVisible(true);
}
} 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ceshi extends JFrame{
public Ceshi()
{
super("此为测试画面!");
Backgound backg1=new Backgound();
Container contentpane=getContentPane();
contentpane.add(backg1);
backg1.setBorder(BorderFactory.createEtchedBorder());
setBounds(100,20,800,800);
//setOpaque(true);
setVisible(true);
}
}
class Backgound extends JPanel{
public Image backg;
public Backgound()
{
super();
setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
backg= Toolkit.getDefaultToolkit().getImage("image/背景.jpg");
g.drawImage(backg,30,30,300,400,null);
}
}
主函数在这里
package game;
public class start {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ceshi c=new Ceshi();
c.setBounds(50,60,600,700);
c.setVisible(false);
c.repaint();
c.repaint();
c.setVisible(true);
//c.setVisible(false);
//c.setVisible(true);
}
} 展开
2019-07-18
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-01-21
展开全部
backg= Toolkit.getDefaultToolkit().getImage("image/背景.jpg");
放到构造、、、、、或更早加载的方法中。。。。。。。要确保图片已经加载完成
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
放到构造、、、、、或更早加载的方法中。。。。。。。要确保图片已经加载完成
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询