Java 在JPanel 上画图片小问题,不知哪里写错了
packagecom.tank;importjava.awt.Graphics;importjava.awt.Image;importjava.io.File;impor...
package com.tank;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test422
{
public static void main(String [] args)
{
Windows w1=new Windows();
}
}
class Windows extends JFrame
{
MyPanel mp=null;
Windows()
{
//窗口属性设置
setSize(600, 500);
setTitle("测试");
setVisible(true);
setLocation(20, 20);
setDefaultCloseOperation(EXIT_ON_CLOSE);
mp=new MyPanel();
add(mp);
}
}
class MyPanel extends JPanel
{
//定义三张图片
Image image1=null;
Image image2=null;
Image image3=null;
MyPanel()
{
//初始化三张图片
try {
image1=ImageIO.read(new File("bomb_1.gif"));
image2=ImageIO.read(new File("bomb_2.gif"));
image3=ImageIO.read(new File("bomb_3.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("测试");
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(20, 20, 20, 20);
System.out.println("paint 函数执行测试");
}
}
自从我定义图片 Image 初始化 Image 后 paint()内的内容就不能执行 不知道哪里写错了? 展开
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test422
{
public static void main(String [] args)
{
Windows w1=new Windows();
}
}
class Windows extends JFrame
{
MyPanel mp=null;
Windows()
{
//窗口属性设置
setSize(600, 500);
setTitle("测试");
setVisible(true);
setLocation(20, 20);
setDefaultCloseOperation(EXIT_ON_CLOSE);
mp=new MyPanel();
add(mp);
}
}
class MyPanel extends JPanel
{
//定义三张图片
Image image1=null;
Image image2=null;
Image image3=null;
MyPanel()
{
//初始化三张图片
try {
image1=ImageIO.read(new File("bomb_1.gif"));
image2=ImageIO.read(new File("bomb_2.gif"));
image3=ImageIO.read(new File("bomb_3.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("测试");
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(20, 20, 20, 20);
System.out.println("paint 函数执行测试");
}
}
自从我定义图片 Image 初始化 Image 后 paint()内的内容就不能执行 不知道哪里写错了? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询