【问题标题】:web picture is cropped in jframe网页图片在jframe中被裁剪
【发布时间】:2014-07-30 09:11:13
【问题描述】:

来自相机的图片(可通过直接 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

【问题讨论】:

    标签: java image swing user-interface jscrollpane


    【解决方案1】:

    变化:

    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);
    

    【讨论】:

      猜你喜欢
      • 2013-07-12
      • 2018-07-06
      • 2023-03-11
      • 1970-01-01
      • 2013-07-05
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      相关资源
      最近更新 更多