简体   繁体   中英

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. 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/

but what i get now is this: http://img600.imageshack.us/i/19267006.png/

any other code i can use to get that?

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

Add the image to a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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