日期:2014-05-20  浏览次数:20729 次

我写了个 图片浏览器,为什么从网页上得到的图片不能显示? 请看问题详情。。
我写了一个打开图片的浏览器。 代码如下:
package tom.myBrowse;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.sun.image.codec.jpeg.JPEGEncodeParam;

public class MyBrowseFrame extends JScrollPane {

// 定义一个 标题为 "Picture Browse" 的窗格 A Frame is a top-level window with a title and a border.
private static JFrame myFrame = new JFrame("My Image Browse Frame");
// JPanel 是一般轻量级容器。 继承-->javax.swing.JComponent-->java.awt.Container
public static JPanel myPanel ;
// 菜单中的项的实现。菜单项本质上是位于列表中的按钮。当用户选择“按钮”时,将执行与菜单项关联的操作。JPopupMenu 中包含的 JMenuItem 正好执行该功能。 
private static JMenuItem tempItem;
// 用于短文本字符串或图像或二者的显示区。
public static JLabel lab;

// FileChooser choose = new FileChooser();

public void showGUI() throws MalformedURLException{
// 设置此 frame 是否可由用户调整大小。
myFrame.setResizable(false);
myPanel = (JPanel) myFrame.getContentPane();
// 将组件移到新位置。通过此组件父级坐标空间中的 x 和 y 参数来指定新位置的左上角。
myPanel.setLocation(200, 300);
// 设置此容器的布局管理器。
myPanel.setLayout(new BorderLayout());

//菜单栏的实现。将 JMenu 对象添加到菜单栏以构造菜单。当用户选择 JMenu 对象时,就会显示其关联的 JPopupMenu,允许用户选择其上的某一个 JMenuItem。
JMenuBar menuBar = new JMenuBar();
myFrame.setJMenuBar(menuBar);

//菜单的该实现是一个包含 JMenuItem 的弹出窗口,用户选择 JMenuBar 上的项时会显示该 JMenuItem。
JMenu FileMenu = new JMenu("File");
menuBar.add(FileMenu);
// 通过URL 得到图片 ------ 不知道为什么得不到。
tempItem = new JMenuItem("Load By URL");
tempItem.addActionListener(new LoadByURLListener());
FileMenu.add(tempItem);

// ImageIcon imageIcon = new ImageIcon(new URL("http://img1.mtime.com/CMS/News/2008/44/20081028203940.52112744.jpg"));
ImageIcon imageIcon = new ImageIcon(new URL("http://www.baidu.com/img/baidu_logo.gif"));
lab = new JLabel(imageIcon);
myPanel.add(lab,BorderLayout.CENTER);

tempItem = new JMenuItem("Load From Location");

myFrame.setMinimumSize(new Dimension(800,800));
myFrame.setLocation(60, 80); // 窗口的 左上角 的 位置
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) throws MalformedURLException {
MyBrowseFrame test = new MyBrowseFrame();
test.showGUI();
}

class LoadByURLListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String imgURL = "http://img1.mtime.com/CMS/News/2008/44/20081028203940.52112744.jpg";

JPanel imgPanel = new JPanel();
imgPanel.setLayout(new BorderLayout());

try {
ImageIcon imageIcon = new ImageIcon(new URL(imgURL));
// JLabel label = new JLabel(imageIcon);
System.out.println(imageIcon.getIconHeight()+" -- "+imageIcon.getIconWidth()+" -- "+imageIcon.getDescription());
lab = new JLabel(imageIcon);
// imgPanel.add(lab);
myPanel.add(lab,BorderLayout.CENTER);
//
// myFrame.add(myPanel);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}
}

为什么从 LoadByURLListener 中 的到的 图片不能显示在我 的图片浏览区 Frame 中呢???

请帮忙看一下。。

我的程序你可以直接 复制下来 直接运行。

在线等您 。。。。谢谢。。。。。


------解决方案--------------------