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

在JF中怎么才可以控制icon的对象的移动?
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
public   class   BackgroundPanel   extends   JComponent
{
public   BackgroundPanel()
{
JFrame   JF   =   new   JFrame( "加载背景图片   ");
Container   cp   =   JF.getContentPane();
NewPanel   p   =   new   NewPanel();
cp.setLayout(new   BorderLayout());
cp.add(p,BorderLayout.CENTER);
JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JF.setBounds(0,0,800,600);
JF.setVisible(true);
;
}
public   static   void   main(String[]   args)
{
new   BackgroundPanel();
}
}
class   NewPanel   extends   JPanel  
{
int   x   =   0;
int   y   =   0;


java.net.URL   imgURL   =   getClass().getResource( "stile.gif ");//此处换成你的图片路径
ImageIcon   icon   =   new   ImageIcon(imgURL);

public   NewPanel()
{
}
public   void   paintComponent(Graphics   g)
{

//g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
while(true)
{
g.drawImage(icon.getImage(),x,y,this);
if(x   >   getSize().width   &&   y   >   getSize().height)
{
break;
}  
if(x   >   getSize().width)
{
y   +=   getSize().height;
x   =   0;
}
else
{
x   +=   getSize().width;
}
}
}//paintComponent()

}//class   NewPanel

------解决方案--------------------
修改成绝对定位的模式 修改点的位置就可以了