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();
}
}
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();
}
}
解决方案 »
- 循环赛日程表,高手告诉我哪里出现逻辑错误总有Exception抛出!
- 多线程的好处....?
- 面试编程题
- 怎样用java反射机制获得父类private 属性的值
- jtable中如何根据cell的值判断此cell是否可以修改呢?
- 急!请问为什么使用定时器时,java.util.TimerTask中有获得数据库连接的任务不执行且无异常?
- 为什么我的语句在前面,反而后执行?
- 求解题思路:2013蓝桥杯Java高职高专组第10题
- 请指点,谢谢
- new Inner(){ }这个匿名类是不是就是指Inner类的一个匿名子类?
- 用java编写外语电子词典
- 给一个中文字符串 utf8编码后的 byte[] ,使用io流转成gbk编码的byte[],怎么弄
this.jf.add(lo.lovd());
这个反了吧?
1 把一张图片放到JPanel上的话,不需要用drawImage呀,paint的,Graphics。。
2 如果你是想画一张图片放到JPanel上,那就画Graphics,也不需要你的d://亲爱的1.jpg
仅仅是想把一个image显示在Jpanel上吗?
很多例子的。
jp = new JPanel() {
public void paintComponent(Graphics g) {
paintComponents(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0, this);
}
};另外,m lo = new m("d://1.jpg");改成m lo = new m("d:\\1.jpg");
原来的那个paintComponent方法是没用的。去掉吧。