JPanel 背景图片不能显示 请大虾帮忙看下
packagecrow;importjavax.swing.*;importjava.awt.*;importjava.net.*;publicclassrt{JFram...
package crow;
import javax.swing.*;
import java.awt.*;
import java.net.*;
public class rt {
JFrame f;
Container c;
public rt(){
f=new JFrame("背景");
f.setSize(400,300);
c=f.getContentPane();
c.setLayout(new BorderLayout());
newjpanl p=new newjpanl();
c.add(p,BorderLayout.SOUTH);
p.add(new JTextField(10));
f.show();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new rt();
}
}
class newjpanl extends JPanel{
public void paintComponent(Graphics g){
int x=0,y=0;
java.net.URL url=getClass().getResource("/image/123.gif");
ImageIcon icon=new ImageIcon(url);
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){x=0;y+=icon.getIconHeight();}
else x+=icon.getIconHeight();
}
}
} 展开
import javax.swing.*;
import java.awt.*;
import java.net.*;
public class rt {
JFrame f;
Container c;
public rt(){
f=new JFrame("背景");
f.setSize(400,300);
c=f.getContentPane();
c.setLayout(new BorderLayout());
newjpanl p=new newjpanl();
c.add(p,BorderLayout.SOUTH);
p.add(new JTextField(10));
f.show();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new rt();
}
}
class newjpanl extends JPanel{
public void paintComponent(Graphics g){
int x=0,y=0;
java.net.URL url=getClass().getResource("/image/123.gif");
ImageIcon icon=new ImageIcon(url);
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){x=0;y+=icon.getIconHeight();}
else x+=icon.getIconHeight();
}
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询