网页图片在jframe中裁剪

时间:2014-07-30 09:11:13

标签: java image swing user-interface jscrollpane

来自相机的图片(可访问的直接IP地址,如" http://1111.11.11.1") 是符合我的最大屏幕尺寸。我添加了一个滚动条,但它似乎没有影响。也许我需要按照其他顺序实施它们,但我似乎无法找到方法。 这是代码,由于我无法给出相机IP的原因,但我添加了一个相当大的图片链接作为演示。

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

public class ShowWebPic {
    private static JFrame frame;

    public static void main(String[] args){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BufferedImage image = null;
        try {
            URL url = new URL("http://wallpoper.com/images/00/40/86/79/galaxies-nebulae_00408679.jpg");
            image = ImageIO.read(url);}
        catch (IOException e) {e.printStackTrace();}
        JLabel lblimage = new JLabel(new ImageIcon(image));
        frame.addKeyListener(escape);
        frame.getContentPane().add(lblimage, BorderLayout.CENTER);
        JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        frame.getContentPane().add(pane, BorderLayout.EAST);
        // frame.add(pane, BorderLayout.EAST); // tried this as well
        frame.pack();
        frame.setVisible(true);
        }   //end of show

     private static KeyListener escape = new KeyAdapter() {
          @Override
          public void keyTyped(KeyEvent e) {if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {frame.dispose();}}};
}

代码本身可以独立运行并且可以工作......而不是我需要的方式:D

1 个答案:

答案 0 :(得分:4)

更改:

frame.getContentPane().add(lblimage, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(pane, BorderLayout.EAST);

致:

//frame.getContentPane().add(lblimage, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane(lblimage, // add image to scroll-pane!
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.getContentPane().add(pane, BorderLayout.CENTER);