JAVA 编程小问题
package cn.edu.ctgu.imagetest;
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class ImageShow {
public static void main(String[] args)
{
ImageFrame frame=new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("ImageShow");
setSize(WIDTH,HEIGHT);
ImagePanel panel=new ImagePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
class ImagePanel extends JPanel
{
public ImagePanel()
{
URL url=new URL("http://img1.fadmy.net/star/1/1513/151314151513.jpg");
image=Toolkit.getDefaultToolkit().getImage(url);
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image, 1);
try
{
tracker.waitForID(1);
}
catch(InterruptedException exception)
{}
}
public void paintCompont(Graphics g)
{
super.paintComponent(g);
int imageWidth=image.getWidth(this);
int imageHeigth=image.getHeight(this);
int FrameWidth=getWidth();
int FrameHeight=getHeight();
int x=(FrameWidth-imageWidth)/2;
int y=(FrameHeight-imageHeigth)/2;
g.drawImage(image, x, y, null);
}
private Image image;
}
在本地上显示网络上的图片?
URL url=new URL("http://img1.fadmy.net/star/1/1513/151314151513.jpg");
image=Toolkit.getDefaultToolkit().getImage(url);
这两句有什么错~?
------解决方案--------------------http://topic.csdn.net/u/20071012/13/298279f1-8247-4714-87c2-8cf1e779fb15.html
这个你还没结呢。
Image image=Toolkit.getDefaultToolkit().getImage(url);
------解决方案--------------------Java code
String url="http://img.asiaec.com/le365/laugh/qwgx/images/piccyyk0ufy.jpg";
try {
image = getImage(new URL(url));
} catch(Exception e){}
}
Graphics g=new Graphics();
g.drawImage(image, 0, 0,this);
}
------解决方案--------------------
帮顶
不会GUI编程
一般做WEB开发的