imageviewerbean.java

来自「这是java2核心技术第七版的源代码」· Java 代码 · 共 67 行

JAVA
67
字号
/**
   @version 1.21 2001-08-15
   @author Cay Horstmann
*/

package com.horstmann.corejava;

import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
   A bean for viewing an image.
*/
public class ImageViewerBean extends JLabel
{

   public ImageViewerBean()
   {
      setBorder(BorderFactory.createEtchedBorder());
   }

   /**
      Sets the fileName property.
      @param fileName the image file name
   */
   public void setFileName(String fileName)
   {  
      try
      {
         file = new File(fileName);
         setIcon(new ImageIcon(ImageIO.read(file)));
      }
      catch (IOException e)
      {
         file = null;
         setIcon(null);
      }  
   }

   /**
      Gets the fileName property.
      @return the image file name
   */
   public String getFileName()
   {  
      if (file == null) return null;
      else return file.getPath();
   }

   public Dimension getPreferredSize()
   {
      return new Dimension(XPREFSIZE, YPREFSIZE);
   }

   private File file = null;
   private static final int XPREFSIZE = 200;
   private static final int YPREFSIZE = 200;
}






⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?