[英]Image not being displayed?
Ive最近嘗試將圖像添加到Jrame,由於某種原因,我遇到了錯誤。 我按了一段視頻和一篇文章,但仍然無法正常工作。 Eclipse沒有顯示任何錯誤,並且程序運行,但是控制台中存在一個與imageIcon行有關的錯誤。 這是代碼:
public class Image extends JFrame{
private ImageIcon image;
public Image(){
super("Image Display");
setLayout(new FlowLayout());
setMinimumSize(new Dimension(500,500));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
image = new ImageIcon(this.getClass().getResource("button.png"));
pack();
}
public void paint(Graphics g1){
Graphics g = (Graphics)g1;
//g.setColor(Color.yellow);
//g.fillRect(0, 0, 500, 500);
image.paintIcon(this, g, 0, 0);
}
}
我跟隨了一個視頻和一篇文章,
無需進行自定義繪畫即可顯示圖像,並且視頻和文章未顯示執行此操作的正確方法。 您不應該覆蓋JFrame的paint()方法。
從Swing教程開始,了解Swing的基礎知識。 它包含您可以下載和執行的示例代碼。
要閱讀的第一部分可能是有關How to Use Icons
的部分,該示例顯示了如何使用現有組件來執行此操作。
如果您想了解有關自定義繪畫的更多信息,則可以查看“ Performing Custom Painting
”部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.