2016-04-15 82 views
1

我想使用JLabel在JFrame上顯示背景圖像。代碼運行並出現按鈕,但圖像不顯示。我已經研究過解決方案,但是我沒有找到專門針對我的代碼的解決方案。任何幫助將不勝感激。如何使用JLabel在JFrame上添加背景圖像?

/** 
* Adds details to interface and programs buttons 
* 
* Imani Davis 
* Final Project 
*/ 

import java.awt.*; 
import java.awt.GridBagLayout; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JLabel; 
import javax.swing.JButton; 
import javax.swing.ImageIcon; 
import javax.swing.border.EmptyBorder; 


public class Use_PF_Interface extends JFrame implements Pet_Fish_Interface 
{ 
    // instance variables - replace the example below with your own 
    private JFrame window; 
    private JPanel panel1, panel2, panel3; 
    private JLabel lblBackgroundImage = new JLabel(); 
    private JButton feedButton = new JButton("Feed Fish"); 
    private JButton playGamesButton = new JButton("Play Game"); 

    /** 
    * Constructor for objects of class Use_PF_Interface 
    */ 
    public Use_PF_Interface() 
    { 
     setTitle("Virtual Pet Fish"); 
     setSize(650, 650); 


     //initializes panels and panel layout 
     panel1 = new JPanel(); 
     panel2 = new JPanel(); 
     panel3 = new JPanel(); 
     panel1.setLayout(new FlowLayout()); 
     panel2.setLayout(new FlowLayout()); 
     panel3.setLayout(new FlowLayout()); 

     lblBackgroundImage.setLayout(new FlowLayout()); 

     //sets background image of panel 
     lblBackgroundImage.setIcon(new ImageIcon("C:\\Users\\This PC\\Desktop\\OCEAN2.JPEG")); 
     panel1.add(lblBackgroundImage); 
     validate(); 

     //adds button to panels 
     panel2.add(feedButton); 
     panel2.add(playGamesButton); 

     //add panels to frame 
     add(panel1); 
     add(panel2); 

    } 


} 
+0

您正在添加panel1,然後是panel2,panel2放置在panel1上方,因此panel1不可見,因此圖像不可見。 – rushabhshah1693

回答

2

JFrame使用BorderLayout默認情況下,BorderLayout只能管理中的任何它提供五個可用位置的單個組件,這意味着panel2是最有可能得到所示的唯一組成部分。

另一種方法是將組件添加到JLabel,但請記住,JLabel沒有默認佈局管理器。此外,請記住,JLabel只使用icontext屬性來計算其首選大小,因此如果內容需要更多空間,它們將被剪切。

開始由具有看看How to Use BorderLayout更多細節

此外,請記住,大多數Swing組件是不透明的一般,所以你需要將它們設置透明的,當你想要做這樣的事情

Background

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Use_PF_Interface extends JFrame { 
    // instance variables - replace the example below with your own 

    private JPanel panel2; 
    private JLabel lblBackgroundImage = new JLabel(); 
    private JButton feedButton = new JButton("Feed Fish"); 
    private JButton playGamesButton = new JButton("Play Game"); 

    /** 
    * Constructor for objects of class Use_PF_Interface 
    */ 
    public Use_PF_Interface() { 
     setTitle("Virtual Pet Fish"); 
     setSize(650, 650); 

     //initializes panels and panel layout 
     panel2 = new JPanel(); 
     panel2.setOpaque(false); 
     panel2.setLayout(new FlowLayout()); 

     lblBackgroundImage.setLayout(new FlowLayout()); 

     //sets background image of panel 
     lblBackgroundImage.setIcon(new ImageIcon("...")); 
     lblBackgroundImage.setLayout(new BorderLayout()); 

     //adds button to panels 
     panel2.add(feedButton); 
     panel2.add(playGamesButton); 

     lblBackgroundImage.add(panel2); 

     add(lblBackgroundImage); 

    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

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

} 
+0

但是如果我想要將按鈕定位在框架中的特定位置? – Yahya

+0

@john你會使用合適的佈局管理器 – MadProgrammer

0

試試這個,

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class ImageInFrame { 
    public static void main(String[] args) throws IOException { 
     String path = "Image1.jpg"; 
     File file = new File(path); 
     BufferedImage image = ImageIO.read(file); 
     JLabel label = new JLabel(new ImageIcon(image)); 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(label); 
     f.pack(); 
     f.setLocation(200,200); 
     f.setVisible(true); 
    } 
} 
+0

,但如何添加按鈕......他們不會出現! – Yahya

+0

如何使圖像與框架保持相同的大小,並調整大小?此外,就像@Yahya所說,我們添加的任何組件都沒有顯示。 –

相關問題