close

JFrame中加入背景圖常見的作法為,利用JLabelJPanel等二個元件,塞至Frame之最下層,以作為Frame之背景圖。不過該範例,只適合固定視窗之情況,因其不能隨著縮放大小視窗而自行變動及延展圖片。

 

1. 範例:在此展示一個具有背景圖之視窗,但並不會根據使用者拉大/縮小視窗而有所變動。

package Demo;

import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Elsa
 */
public class ImgBackgroundFromLabelDemo extends JFrame {
    JPanel contentPanel = null, imagePanel = null;
    JLabel wordLabel = null, bgLabel = null;
    ImageIcon background = null;
    
    public ImgBackgroundFromLabelDemo(){
        // 1.設置frame title及Layout之類型
        this.setTitle("利用JLabel在JFrame中加入背景圖");
        this.setLayout(new BorderLayout());
        
        // 2.設置要顯示之資訊與元件
        contentPanel = new JPanel();
        contentPanel.setLayout(null);
        contentPanel.setOpaque(false);              // 將JPanel設置為具透明化
        wordLabel = new JLabel("Hello World!!");
        wordLabel.setBounds(10, 10, 100, 100);        // 設置位置跟寬高
        contentPanel.add(wordLabel);
        this.getContentPane().add(contentPanel, BorderLayout.CENTER);
        
        // 3.於JFrame中設置背景圖片 - 圖片無法縮放大小
        background = new ImageIcon(getClass().getResource("/Demo/Res/bgDemo.jpg"));       // 背景圖片
        bgLabel = new JLabel(background);      // 把背景圖顯示在Label中
        bgLabel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());    // 把含有背景圖之Label位置設置為圖片剛好填充整個版面
        // 把内容視窗轉為JPanel,否則不能使用setOpaque()來使視窗變成透明
        imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));     // 把背景圖添加到分層窗格的最底層以作為背景
        
        // 4.設置frame之基本設定
        this.setMinimumSize(new java.awt.Dimension(900, 675));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    
    public static void main(String[] args) {
        new ImgBackgroundFromLabelDemo();
    }
}

 

 

2. 執行結果

ImgBackgroundFromLabelDemo_1.png

生成時之樣貌

 

ImgBackgroundFromLabelDemo_2.png

被向右及下拉動之樣貌

 

 

 

arrow
arrow
    文章標籤
    JFrame 背景圖 FrameBackground
    全站熱搜
    創作者介紹
    創作者 Elsa 的頭像
    Elsa

    Elsaの程式學習筆記

    Elsa 發表在 痞客邦 留言(0) 人氣()