日期:2014-05-20  浏览次数:20685 次

添加了一个JPanel面板,但是却不能立刻显示出来
在我的MyPanel类中加了一个图片,当我运行程序的时候,这个面板并不显示出来,只有我最大化或者最小化一下之后,才能显示出来,哪位好心人能帮忙解决一下。我是菜鸟~初学者
  代码如下:
  import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Welcome extends JFrame 
{ Image img1;
  ImageIcon img2,img3,img4;
  Toolkit tool;
  MyPanel p1;
  JLabel l1;
  JButton b1,b2;
public static void main(String args[])
{
Welcome wel=new Welcome();

}
Welcome()
{  
p1=new MyPanel();
img3=new ImageIcon("bb1.jpg");
b1=new JButton("进入");
b2=new JButton("退出");
tool=getToolkit();
Dimension dim=tool.getScreenSize();
int a=(int)dim.getHeight();
int b=(int)dim.getWidth();
img1=tool.getImage("4.jpg");
img2=new ImageIcon("cat.jpg");
this.setTitle("招财猫彩票系统");
this.setIconImage(img1);
l1=new JLabel(img2);
l1.setBounds(0,0,img2.getIconWidth(), img2.getIconHeight());
Container con=this.getContentPane();
con.setLayout(null);
((JPanel)con).setOpaque(false);
this.getLayeredPane().setLayout(null);
this.getLayeredPane().add(l1,new Integer(Integer.MIN_VALUE));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(b/4, a/4, img2.getIconWidth(),img2.getIconHeight());
b1.setBounds(100,350,70,40);
b2.setBounds(400,350,70,40);
p1.setLocation(150,80);
p1.setVisible(true);
p1.validate();
System.out.println(this.getWidth());
System.out.println(this.getHeight());
con.add(b1);
con.add(b2);
con.add(p1);
this.setVisible(true);
this.validate();
}
  
}
class MyPanel extends JPanel
{
Toolkit tool;
Image img1;
MyPanel()
{
tool=getToolkit();
img1=tool.getImage("15.png");
this.setSize(280, 220);  
}
public void paintComponent(Graphics g)
{
g.drawImage(img1, 0, 0, null);
}
}


------解决方案--------------------
你加我QQ 519582737 我本机没有你引用的那些图片,你用QQ给我传过来
探讨

不成,你屏的那两条代码是问题已经发生了以后我加上去的,有没有那两条代码一开始的面板都是显示不出来的~