[英]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();
}
}
大家好! 我只是試圖在屏幕上沒有任何幀的情況下打印圖像。 我認為,該代碼應將圖像打印到屏幕上。 等待兩秒鍾,然后處理。 我究竟做錯了什么?
順便說一句,我什么都沒收到,該程序保持運行2秒鍾,然后死了。
您的圖像在您的JLabel中。 如果未顯示JLabel的框架,為什么要在屏幕上打印?
您已經設置了未裝飾的框架。 設置在框架上可見,將起作用。
您不需要最后的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.