62,620
社区成员
发帖
与我相关
我的任务
分享import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
class m
{ public JPanel jp;
protected ImageIcon icon ;
public int width,height;
public m(String photoPath)
{
jp=new JPanel();
icon=new ImageIcon(photoPath);
width=icon.getIconWidth();
height=icon.getIconHeight();
jp.setSize(width,height);
}
JPanel lovd(){
return this.jp;
}
protected void paintComponent(Graphics g)
{
this.jp.paintComponents(g);
Image img=icon.getImage();
g.drawImage(img, 0, 0, this.jp);
}
}
public class test
{
private JFrame jf;
test(String Title)
{
jf=new JFrame(Title);
}
void set(){
m lo=new m("d://亲爱的1.jpg");
Dimension ScrSize=Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((ScrSize.width-500)/2);
int y=(int)((ScrSize.height-300)/2);
this.jf.setLayout(null);
this.jf.setBounds(0, 0, ScrSize.width, ScrSize.height);
this.jf.setResizable(false);
this.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.jf.setVisible(true);
this.jf.add(lo.lovd());
}
public static void main(String [] args)
{
new test("test").set();
}
}
jp = new JPanel() {
public void paintComponent(Graphics g) {
paintComponents(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0, this);
}
};