java显示图片,为什么图片显示不出来?求大神
importjava.awt.Container;importjava.awt.Graphics;importjava.awt.Image;importjava.awt....
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class InternetAccessDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);;
}
}
class ImageFrame extends JFrame {
public static final int WIDTH = 1024;
public static final int HEIGHT = 676;
public ImageFrame() {
setTitle("从网络中下载图片");
setSize(WIDTH, HEIGHT);
ImagePanel panel = new ImagePanel();
Container container = getContentPane();
container.add(panel);
}
}
class ImagePanel extends JPanel {
private Image image;
URL url;
public ImagePanel() {
try {
url = new URL(
"http://pica.nipic.com/2008-03-19/2008319183523380_2.jpg");
image = Toolkit.getDefaultToolkit().getImage(url);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void painComponent(Graphics g) {
super.paintComponents(g);
g.drawImage(image, 0, 0, this);
g.drawString("正在下载....", 1030, 700);
}
} 展开
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class InternetAccessDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);;
}
}
class ImageFrame extends JFrame {
public static final int WIDTH = 1024;
public static final int HEIGHT = 676;
public ImageFrame() {
setTitle("从网络中下载图片");
setSize(WIDTH, HEIGHT);
ImagePanel panel = new ImagePanel();
Container container = getContentPane();
container.add(panel);
}
}
class ImagePanel extends JPanel {
private Image image;
URL url;
public ImagePanel() {
try {
url = new URL(
"http://pica.nipic.com/2008-03-19/2008319183523380_2.jpg");
image = Toolkit.getDefaultToolkit().getImage(url);
MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void painComponent(Graphics g) {
super.paintComponents(g);
g.drawImage(image, 0, 0, this);
g.drawString("正在下载....", 1030, 700);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询