import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GetImage extends JFrame{
Container theContainer;
JTextField theTextField;
JButton theButton;
Toolkit theToolkit;
Image theImage;
URL theURL;
public GetImage() {
super("從網路取得圖片");
theContainer = super.getContentPane();
theTextField = new JTextField(18);
theContainer.add(theTextField);
theButton = new JButton("輸入網址");
theContainer.add(theButton);
theToolkit = getToolkit();
theButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
theURL = new URL(theTextField.getText());
theImage = theToolkit.getImage(theURL);
repaint();
}catch(MalformedURLException malformedEx) {
JOptionPane.showMessageDialog(null,"不合標準的URL格式!");
}catch(IOException ioEx) {
JOptionPane.showMessageDialog(null,"發生IO例外!");
}catch(Exception Ex) {
JOptionPane.showMessageDialog(null,"發生例外!");
}
}
});
pack();
setLayout(new FlowLayout(FlowLayout.CENTER));
setVisible(true);
setBounds(100,100,400,300);
}
public void paint(Graphics g) {
super.paint(g);
if(theImage != null) {
g.drawImage(theImage,20,80,this);
}
}
public static void main(String[] args) {
GetImage demo = new GetImage();
}
}
GetImage.java顯示網路上的圖片
Labels: Java程式範例 |
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment