简体   繁体   English

尝试打印无边框图像

[英]Trying to print image without frame

public class Main{
    public static void main(String []args){
        JLabel c=new JLabel();
        c.setIcon(new ImageIcon("picture.png"));
        JFrame frame = new JFrame();
        frame.setBackground(Color.WHITE);
        frame.setUndecorated(true);
        frame.getContentPane().add(c);
        frame.pack();
        BufferedImage bi = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = bi.createGraphics();
        c.print(graphics);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        graphics.dispose();
        frame.dispose();
    }
}

Hi all! 大家好! I am simply trying to print an image without any frame onto the screen. 我只是试图在屏幕上没有任何帧的情况下打印图像。 This code should, I think, print the image to the screen; 我认为,该代码应将图像打印到屏幕上。 wait two seconds and then dispose of it. 等待两秒钟,然后处理。 What am I doing wrong? 我究竟做错了什么?

BTW I get no errors whatsoever, the program just stays alive for 2 seconds then dies. 顺便说一句,我什么都没收到,该程序保持运行2秒钟,然后死了。

Your image is in your JLabel. 您的图像在您的JLabel中。 Why should it be printed on your screen if the frame where JLabel is is not showing? 如果未显示JLabel的框架,为什么要在屏幕上打印?

You are already setting the frame undecorated. 您已经设置了未装饰的框架。 Setting visible on the frame, will work. 设置在框架上可见,将起作用。

You don't need the Graphics part at the end and also you forgot to call setVisible(true); 您不需要最后的Graphics部分,而且您忘记了调用setVisible(true);。

public class Main{
    public static void main(String []args){
        JLabel c=new JLabel();
        c.setIcon(new ImageIcon("picture.png"));
        JFrame frame = new JFrame();
        frame.setBackground(Color.WHITE);
        frame.setUndecorated(true);
        frame.getContentPane().add(c);
        frame.pack();
        frame.setVisible(true);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        frame.dispose();
    }
}

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

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