代码如下
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class look extends JFrame
{
ImageIcon icon = new ImageIcon("D:/images/pro/jp.png");
look()
{
setDefaultCloseOperation(2);
Container con=this.getContentPane();
JPanel jp1= new JPanel()
{
protected void paintComponent(Graphics g)
{
g.drawImage(icon.getImage(), 0, 0, this);
super.paintComponents(g);
}
};
JPanel jp2
=new JPanel();
JScrollPane jsp=new JScrollPane(jp2);
setSize(300,200);
setVisible(true);
jp2.setBackground(Color.red);
jp2.setPreferredSize(new Dimension(200, 100));
jsp.setLocation(00, 100);
jsp.setSize(200, 50);
jp1.setLocation(0, 0);
jp1.setLayout(null);
jp1.setSize(getSize().width,getSize().height);
con.setLayout(null);
con.add(jp1);
jp1.add(jsp);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new look();
}
}
运行后,最上面有一块红色(不应该有的),这块红色是jp2的背景色,是设置在JScrollPane里的,为什么多出来了呢?