代码如下
import java.awt.Image; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 
/** 
* 加载显示图象,需要JDK1.5或以上 
*/ 
public class showtu extends JFrame { 
public showtu(String bmpFile) { 
Image image = null; 
try { 
image = ImageIO.read(new File(bmpFile)); 
} catch (IOException ex) { 

JLabel label = new JLabel(new ImageIcon(image)); 
add(label); 
setDefaultCloseOperation(EXIT_ON_CLOSE); 
pack(); 
} public static void main(String[] args){ 
final String fileName = "F:\\456备用\\亮个相.JPG"; //换成你要显示的图片 SwingUtilities.invokeLater(new Runnable(){ 
public void run(){ 
new showtu(fileName).setVisible(true); 

}); 

} 图片超出了屏幕范围 只能显示一部分 加滚动条能解决吗

解决方案 »

  1.   


    package novemberTestDay;import java.awt.Image;  
    import java.io.File;  
    import java.io.IOException;  
    import javax.imageio.ImageIO;  
    import javax.swing.ImageIcon;  
    import javax.swing.JFrame;  
    import javax.swing.JLabel;  
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;  
    /**  
    * 加载显示图象,需要JDK1.5或以上  
    */  
    public class TestImage extends JFrame {  
    public TestImage(String bmpFile) {  
    Image image = null;  
    try {  
    image = ImageIO.read(new File(bmpFile));  
    } catch (IOException ex) {  
    }  
    JLabel label = new JLabel(new ImageIcon(image));
    JScrollPane scroll = new JScrollPane(label);
    scroll.setAutoscrolls(true);
    getContentPane().add(scroll);scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);add(label);  
    setDefaultCloseOperation(EXIT_ON_CLOSE);  
    pack();  
    }  public static void main(String[] args){  
    final String fileName = "D:\\c.jpg"; //换成你要显示的图片  SwingUtilities.invokeLater(new Runnable(){  
    public void run(){  
    new TestImage(fileName).setVisible(true);  
    }  
    });  
    }  
    }  
    你试试 ...
      

  2.   


    package novemberTestDay;import java.awt.EventQueue;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;public class ImgTest extends JFrame { /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    final String fileName = "D:\\c.jpg"; //换成你要显示的图片   ImgTest frame = new ImgTest(fileName);
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    } /**
     * Create the frame
     */
    public ImgTest(String img) {
    super();
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //
    JLabel lab=new JLabel(new ImageIcon(img));
    Image image = null;  
    try {  
    image = ImageIO.read(new File(img));  
    } catch (IOException ex) {  

    JScrollPane scroll = new JScrollPane(lab);
    getContentPane().add(scroll);
    }}
    用这个 绝对可以 ,我试了!