2012-12-09 162 views
1

我有一個JPanel,我想添加一個圖像作爲它的背景。我怎樣才能做到這一點 ?添加背景圖像到面板

frame = new JFrame("Some frame"); 
panel1 = new JPanel(); 
panel1.setBorder(new EmptyBorder(5, 5, 5, 5)); 
// NEED TO ADD AN IMAGE TO THIS PANEL 

panel1.setLayout(cardlayout); 
frame.getContentPane().add(panel1); 

frame.setLocationByPlatform(true); 
frame.setVisible(true); 

我需要將圖像添加到panel,我該怎麼做?

更新1

panel1 = new JPanel() 
    { 
    private static final long serialVersionUID = 1L; 

    @Override 
    public void paintComponent(Graphics g) 
    { 

     g.drawImage(Toolkit.getDefaultToolkit().createImage("1.jpg"), 0, 0, null); 
    } 
}; 
+0

在更新我看你是忘了叫'super.paintComponent方法(G),在''重載方法paintComponent',這可能是一個大問題(甚至除了提取圖像問題在'paintComponent'中)。重寫方法應該優化地首先調用他們的'超' –

+0

看到這些答案[這裏](http://stackoverflow.com/questions/13237908/loading-image-in-java-code-from-c-drive/13238468#13238468)或[this one](http://stackoverflow.com/questions/12660122/image-resizing-and-displaying-in-a-jpanel-or-a-jlabel-without-loss-of-quality/12660146#12660146)對於一些例子 –

+0

@DavidKroukamp我試圖添加'super.paintComponent(g);'但它仍然不會添加圖像到Jpanel –

回答

4

你需要覆蓋的方法JPanelpaintComponent(Graphics g)Graphics對象g上使用drawImage()this example


另外,通過@trashgod檢查這兩個例子:

  1. example
  2. example
+0

我已更新我的代碼。它不起作用 –

+0

此外,我會使用'ImageIO'來加載圖像 – MadProgrammer

+0

@sharonHwk'Toolkit.getDefaultToolkit()。createImage(「1.jpg」)'可能會返回'null'結果或空圖像如果您嘗試引用的圖像不存在。確保圖像存在並且可以從應用程序的上下文中引用 – MadProgrammer

3

您有資源位置問題。

Toolkit#createImage如果找不到資源,可能會返回一個空的圖像。

我建議你使用ImageIO API,它支持更廣泛的圖像格式,但如果圖像未找到或無法加載,也會引發異常。

加載圖像的方式還取決於圖像的位置。

如果文件系統上存在的圖像,你可以簡單地用一個File對象引用,如果圖像是嵌入的資源(你的應用程序中),您將需要使用Class#getResource獲得URL它。

enter image description here

public class TestGraphics { 

    public static void main(String[] args) { 
     new TestGraphics(); 
    } 

    public TestGraphics() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(new PaintTest()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class PaintTest extends JPanel { 

     private BufferedImage image; 

     public PaintTest() { 

      setLayout(new BorderLayout()); 
      try { 
       // Use this if the image exists within the file system 
       image = ImageIO.read(new File("/path/to/image/imageName.png")); 
       // Use this if the image is an embedded resource 
//    image = ImageIO.read(getClass().getResource("/path/to/resource/imageName.png")); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return image == null ? super.getPreferredSize() : new Dimension (image.getWidth(), image.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (image != null) { 
       int x = (getWidth() - image.getWidth())/2; 
       int y = (getHeight()- image.getHeight())/2; 
       g.drawImage(image, x, y, this); 
      } 
     } 

    } 

} 
+0

+1好建議和示例。我總是使用'ImageIO#read',但從來不會想到'Toolkit#createImage'會有什麼不同,很好理解。 –