2010-04-24 17 views
2

我想把圖像作爲我的界面在java中的背景,我試圖寫一個類來做到這一點,並使用它,但有沒有更簡單的方法來做到這一點。在java中的背景圖像

這裏是我使用的code

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.BorderFactory; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 

public class BackgroundImagePanelExample { 

    // Set up contraints so that the user supplied component and the 
    // background image label overlap and resize identically 
    private static final GridBagConstraints gbc; 

    static { 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.anchor = GridBagConstraints.NORTHWEST; 
    } 

    /** 
    * Wraps a Swing JComponent in a background image. Simply invokes the overloded 
    * variant with Top/Leading alignment for background image. 
    * 
    * @param component - to wrap in the a background image 
    * @param backgroundIcon - the background image (Icon) 
    * @return the wrapping JPanel 
    */ 
    public static JPanel wrapInBackgroundImage(JComponent component, 
     Icon backgroundIcon) { 
     return wrapInBackgroundImage(
      component, 
      backgroundIcon, 
      JLabel.TOP, 
      JLabel.LEADING); 
    } 

    /** 
    * Wraps a Swing JComponent in a background image. The vertical and horizontal 
    * alignment of background image can be specified using the alignment 
    * contants from JLabel. 
    * 
    * @param component - to wrap in the a background image 
    * @param backgroundIcon - the background image (Icon) 
    * @param verticalAlignment - vertical alignment. See contants in JLabel. 
    * @param horizontalAlignment - horizontal alignment. See contants in JLabel. 
    * @return the wrapping JPanel 
    */ 
    public static JPanel wrapInBackgroundImage(JComponent component, 
     Icon backgroundIcon, 
     int verticalAlignment, 
     int horizontalAlignment) { 

     // make the passed in swing component transparent 
     component.setOpaque(false); 

     // create wrapper JPanel 
     JPanel backgroundPanel = new JPanel(new GridBagLayout()); 

     // add the passed in swing component first to ensure that it is in front 
     backgroundPanel.add(component, gbc); 

     // create a label to paint the background image 
     JLabel backgroundImage = new JLabel(backgroundIcon); 

     // set minimum and preferred sizes so that the size of the image 
     // does not affect the layout size 
     backgroundImage.setPreferredSize(new Dimension(1, 1)); 
     backgroundImage.setMinimumSize(new Dimension(1, 1)); 

     // align the image as specified. 
     backgroundImage.setVerticalAlignment(verticalAlignment); 
     backgroundImage.setHorizontalAlignment(horizontalAlignment); 

     // add the background label 
     backgroundPanel.add(backgroundImage, gbc); 

     // return the wrapper 
     return backgroundPanel; 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Background Image Panel Example"); 

     // Create some GUI 
     JPanel foregroundPanel = new JPanel(new BorderLayout(10, 10)); 
     foregroundPanel.setBorder(
      BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     foregroundPanel.setOpaque(false); 

     foregroundPanel.add(new JLabel("Comment:"), BorderLayout.NORTH); 
     foregroundPanel.add(new JScrollPane(new JTextArea(3, 10)), 
      BorderLayout.CENTER); 
     foregroundPanel.add(
      new JLabel(
      "Please enter your comments in text box above." 
      + " HTML syntax is allowed."), BorderLayout.SOUTH); 

     frame.setContentPane(wrapInBackgroundImage(foregroundPanel, 
      new ImageIcon(
      BackgroundImagePanelExample.class.getResource("backgd.jpg")))); 

     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

感謝

+2

這個 「接口:」 你說的是一個桌面應用程序,在Swing中實現的GUI?或者其他一些選擇? – 2010-04-24 20:48:12

+0

只是一個在Swing中實現的GUI – 2010-04-24 20:57:06

+0

如果沒有向我們展示一些代碼,我們如何知道是否有更簡單的方法? – Enrique 2010-04-24 21:03:15

回答

1

我猜你正在收到java.lang.NullPointerException,因爲找不到getResource()。您可能可以將圖像文件放在源文件旁邊並重建項目。或者,您可以在解決問題時將其作爲臨時措施從文件系統加載。

frame.setContentPane(wrapInBackgroundImage(
    foregroundPanel, new ImageIcon("image.jpg"))); 
1

我不真正看到你有什麼要求。如果你問,是否有更簡單的方法來構建swing應用程序 - 比答案是YES。將NetBeans IDE與Swing Builder配合使用,可以生成非常合理的生成代碼,並讓您可以編輯整個組件。手寫Swing比NetBeans中生成的代碼更經常被「破解」,並且耗時更多...

1
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class ImageTest { 

    public static void main(String[] args) { 
    ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage()); 

    JFrame frame = new JFrame(); 
    frame.getContentPane().add(panel); 
    frame.pack(); 
    frame.setVisible(true); 
    } 
} 

class ImagePanel extends JPanel { 

    private Image img; 

    public ImagePanel(String img) { 
    this(new ImageIcon(img).getImage()); 
    } 

    public ImagePanel(Image img) { 
    this.img = img; 
    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); 
    setPreferredSize(size); 
    setMinimumSize(size); 
    setMaximumSize(size); 
    setSize(size); 
    setLayout(null); 
    } 

    public void paintComponent(Graphics g) { 
    g.drawImage(img, 0, 0, null); 
    } 

} 
0
public class BackgroundPanel extends JPanel { 

private static final long serialVersionUID = 1L; 
Image image; 

public BackgroundPanel() { 
    super(); 
    initialize(); 
} 

/** 
* This method initializes this 
* 
* @return void 
*/ 
private void initialize() { 
    try { 
     image = new ImageIcon(getClass().getResource("background.jpg")).getImage(); 
    } catch (Exception e) { 
     /*handled in paintComponent()*/ 
    } 
    this.setSize(800, 470); 
    this.setLayout(null); 
} 

@Override 
protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if(image != null) { 
     g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this); 
    } 
} 
}