2015-02-10 103 views
1

我想添加一個背景圖像到我的frame.But當我添加一個圖像到我的框架它已成功添加,但其他東西,如j標籤和按鈕添加到框架afterwords不會出現在框架上。我在桌面上保留了圖像。下面是我的代碼JFrame背景圖背景圖像設置時,其他按鈕不顯示在圖像上的圖像?

package UI; 

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import java.awt.Color; 
import java.io.File; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import java.awt.GridBagLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Font; 
import javax.swing.JButton; 
import java.awt.Insets; 

public class EditTeamInterface extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        EditTeamInterface frame = new EditTeamInterface(); 
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

        //frame.getContentPane().setBackground(Color.white); 
        frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Abdullah/Desktop/cricketBackGround1.jpg"))))); 
        frame.pack(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public EditTeamInterface() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 624, 356); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     GridBagLayout gbl_contentPane = new GridBagLayout(); 
     gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0}; 
     gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0}; 
     gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; 
     gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; 
     contentPane.setLayout(gbl_contentPane); 

     JLabel lblNewLabel = new JLabel("EDIT OPTIONS"); 
     lblNewLabel.setFont(new Font("SketchFlow Print", Font.BOLD, 18)); 
     GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); 
     gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); 
     gbc_lblNewLabel.gridx = 0; 
     gbc_lblNewLabel.gridy = 0; 
     contentPane.add(lblNewLabel, gbc_lblNewLabel); 

     JButton btnNewButton = new JButton("New button"); 
     GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); 
     gbc_btnNewButton.gridx = 5; 
     gbc_btnNewButton.gridy = 5; 
     contentPane.add(btnNewButton, gbc_btnNewButton); 
    } 

} 
+0

圖像大小被設置爲我的筆記本電腦的屏幕的尺寸是1337水平由670個垂直像素 – 2015-02-10 04:32:08

回答

1

EditTeamInterface的構造,可以設置使用setContentPane(contentPane);框架中的內容窗格中,但在static void main,你

frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Abdullah/Desktop/cricketBackGround1.jpg"))))); 

更換這會放棄你做的一切之前對於這個新的內容...

而不是EditTeamInterfaceJFrame延伸,改變它,使它從JPanel

創建JFrame的新實例,將內容窗格設置爲您的標籤,然後將EditTeamInterface添加到框架。

因爲JLabel沒有默認的佈局管理器,你應該叫setLayout(new BorderLayout())在框架上後,你設置它的內容窗格標籤

雖然使用JLabel,這樣一來將工作,JLabel將不計算它的首選大小是基於其內容的需求,而是基於icontext的屬性。這並不能真正適合這項任務。

更好的解決方案是使用自定義JPanel並覆蓋它的paintComponent來渲染所需的圖像。如果你聰明,你會做到這一點,以便這個班級可以重複使用。

喜歡的東西this for example