简体   繁体   English

如何将800x600图像放入jLabel,而无需滚动查看整个图像

[英]How to put a 800x600 image in a jLabel without having to scroll to see the whole image

i tried ways like getScaledInstance but all does not work. 我尝试了类似getScaledInstance的方法,但是都无法正常工作。 i could not find any solutions online either. 我也找不到在线任何解决方案。 the code that i am currently using is: 我当前使用的代码是:

public class ShowImage_1 extends Panel {

    BufferedImage image;

    public ShowImage_1() {
        try {
            File input = new File("C:/Lighthouse.jpg");
            image = ImageIO.read(input);
            image.getGraphics().drawImage(image, 0, 0, 400, 400, null);
        } catch (IOException ie) {
            System.out.println("Error:" + ie.getMessage());
        }
    }

    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    static public void main(String args[]) throws Exception {
        JFrame frame = new JFrame("Display image");
        Panel panel = new ShowImage_1();

        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

it does not show the full image on the panel. 它不会在面板上显示完整图像。 any idea on how to go about doing it? 关于如何去做的任何想法?

i want to achieve this effect: http://img209.imageshack.us/i/77894822.png/ 我想达到这种效果: http : //img209.imageshack.us/i/77894822.png/

but what i get now is this: http://img600.imageshack.us/i/19267006.png/ 但是我现在得到的是: http : //img600.imageshack.us/i/19267006.png/

any other code i can use to get that? 我可以使用任何其他代码来获得它吗?

使面板足够大以容纳图像。这可能会解决您的问题

Add the image to a JLabel. 将图像添加到JLabel。 Add the label to the frame. 将标签添加到框架。 Then use: 然后使用:

frame.pack();

NOT

frame.setSize(800, 600);

If you need more help post your SSCCE demonstrating the problem. 如果您需要更多帮助,请发布SSCCE演示问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM