【從零開始學 Java 程式設計】 JLable 標籤元件

【從零開始學 Java 程式設計】 線上教學課程目錄,使用 Java 程式語言,開發應用程式。

JLable

JLable 標籤元件,可以顯示圖片與文字的元件,其中文字不可以選擇與編輯。

範例

範例中採用 GridBagLayout 來進行元件佈局,分別設定 lab1,只顯示文字; lab2,只顯示圖片; lab3,同時顯示圖片與文字。此範例同時學習到如何讀取並顯示自己電腦本機端資料夾中的圖片。

資源檔圖片

在 resources 目錄下新增一個 images 資料夾後,放入 logo.jpg

logo.png

關鍵程式碼

protected Image createImageIcon(String path) {
        java.net.URL imgURL = DemoJLabel.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL).getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public void addComponentsToPane(Container pane) {

        //宣告 GridBagLayout
        GridBagLayout gridBagLayout = new GridBagLayout();

        //設定採用 BoxLayout
        pane.setLayout(gridBagLayout);

        //宣告網格約束變數
        GridBagConstraints c;

        //lab1 只顯示文字
        JLabel lab1 = new JLabel("lab1 只顯示文字");
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;//第一列,結束行
        c.gridwidth = GridBagConstraints.REMAINDER;
        pane.add(lab1, c);

        //設定要顯示的圖片路徑
        Image img = createImageIcon("images/logo.png");

        //lab2 只顯示圖片
        JLabel lab2 = new JLabel();
        lab2.setIcon(new ImageIcon(img));
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(40, 0, 0, 0);
        c.gridwidth = GridBagConstraints.REMAINDER;
        pane.add(lab2, c);

        //lab3 同時顯示文字與圖片
        JLabel lab3 = new JLabel("lab3 顯示文字與圖片", new ImageIcon(img), JLabel.CENTER);
        //設定圖片與文字排列關係
        lab3.setVerticalTextPosition(JLabel.BOTTOM);
        lab3.setHorizontalTextPosition(JLabel.CENTER);
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(40, 0, 0, 0);
        c.gridwidth = GridBagConstraints.REMAINDER;
        pane.add(lab3, c);
    }

完整程式碼

import javax.swing.*;
import java.awt.*;

public class DemoJLabel {

    protected Image createImageIcon(String path) {
        java.net.URL imgURL = DemoJLabel.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL).getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public void addComponentsToPane(Container pane) {

        //宣告 GridBagLayout
        GridBagLayout gridBagLayout = new GridBagLayout();

        //設定採用 BoxLayout
        pane.setLayout(gridBagLayout);

        //宣告網格約束變數
        GridBagConstraints c;

        //lab1 只顯示文字
        JLabel lab1 = new JLabel("lab1 只顯示文字");
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;//第一列,結束行
        c.gridwidth = GridBagConstraints.REMAINDER;
        pane.add(lab1, c);

        //設定要顯示的圖片路徑
        Image img = createImageIcon("images/logo.png");

        //lab2 只顯示圖片
        JLabel lab2 = new JLabel();
        lab2.setIcon(new ImageIcon(img));
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(40, 0, 0, 0);
        c.gridwidth = GridBagConstraints.REMAINDER;
        pane.add(lab2, c);

        //lab3 同時顯示文字與圖片
        JLabel lab3 = new JLabel("lab3 顯示文字與圖片", new ImageIcon(img), JLabel.CENTER);
        //設定圖片與文字排列關係
        lab3.setVerticalTextPosition(JLabel.BOTTOM);
        lab3.setHorizontalTextPosition(JLabel.CENTER);
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(40, 0, 0, 0);
        c.gridwidth = GridBagConstraints.REMAINDER;
        pane.add(lab3, c);
    }

    public DemoJLabel() {
        JFrame frame = new JFrame("HKT線上教室");
        // 獲取螢幕解析度
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        // 設定視窗大小佔螢幕比例
        frame.setSize(dimension.width / 3, dimension.height / 3);

        //設定視窗顯示在螢幕畫面中間位置
        int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉可以關掉程式
        //在 Pane 畫面中加入元件
        addComponentsToPane(frame.getContentPane());
//        frame.pack();
        frame.setVisible(true);
    }

    //最一開始程式進入點
    public static void main(String[] args) {
        //使用 invokeLater 確保 UI 在排程執行緒內
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DemoJLabel();
            }
        });
    }
}

JLable 常用方法

常用方法

方法說明
public void setText(String text)設定標籤文字
public String getText()取得標籤文字
public void setFont(Font font)設定字型
public void setSize(Dimension d)字體大小
public void setForeground(Color fg)設定字體顏色

參考資料


那這次的課程就介紹到這邊囉~
順帶一提,KT 線上教室,臉書粉絲團,會不定期發佈相關資訊,不想錯過最新資訊,不要忘記來按讚,加追蹤喔!也歡迎大家將這套課程分享給更多人喔。
我們下次再見囉!!!掰掰~

這個網誌中的熱門文章

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

2022 最新入門零基礎 Flutter教學 【Flutter 程式設計入門實戰 30 天】Flutter 教學課程目錄 (IntelliJ IDEA 開發教學)

nano 文字編輯器

16天記下7000單字

最新入門零基礎 Java 教學【從零開始學 Java 程式設計】Java教學課程目錄 (IntelliJ IDEA 開發教學)