Java程序圖形用戶界面設(shè)計(jì)之標(biāo)簽組件
Java程序設(shè)計(jì) 圖形用戶界面【三】
標(biāo)簽組件JLabel
JLabel組件表示的是一個(gè)標(biāo)簽,本身是用于顯示信息的,一般情況下是不能直接更改其顯示內(nèi)容的
常量 | 作用 |
---|---|
public static final int LEFT | 標(biāo)簽文本左對(duì)齊 |
public static final int CENTER | 標(biāo)簽文本居中對(duì)齊 |
public static final int RIGHT | 標(biāo)簽文本右對(duì)齊 |
方法 | 作用 |
---|---|
public JLabel() throws HeadlessException | 創(chuàng)建一個(gè)JLabel對(duì)象 |
public JLabel(String text) throws HeadlessException | 創(chuàng)建一個(gè)標(biāo)簽并指定文本內(nèi)容,默認(rèn)為左對(duì)齊 |
public Label(String text,int alignment) throws HeadlessException | 創(chuàng)建一個(gè)標(biāo)簽并指定文本內(nèi)容以及對(duì)齊方式 |
public JLabel(String text,Icon icon,int honzontalAlignment) | 創(chuàng)建具有指定文本,圖像和水平對(duì)齊方式的JLabel對(duì)象 |
public JLabel(Icon image,int honzontalAlignment) | 創(chuàng)建具有指定圖像和水平對(duì)齊方式的JLabel實(shí)例 |
public void setText(String text) | 設(shè)置標(biāo)簽的文本 |
public String getText() | 取得標(biāo)簽的文本 |
public void setAlignment(int alignment) | 設(shè)置標(biāo)簽的對(duì)齊方式 |
public void setIcon(Icon icon) | 設(shè)置指定的圖像 |
import javax.swing.*; import java.awt.*; public class Hello { public static void main(String[] args) { JFrame frame = new JFrame("一"); JLabel label = new JLabel("HELLO",JLabel.CENTER); frame.add(label); Dimension d = new Dimension(); d.setSize(500,600); frame.setSize(d); frame.setBackground(Color.black); Point point = new Point(300,200); frame.setLocation(point); frame.setVisible(true); } }
更改JLabel的文字樣式
更改使用的字體,則可以直接使用Component類中定義的以下方法
public void setFont(Font t)
Font類
常量 | 作用 |
---|---|
public static final int BOLD | 文字顯示為粗體 |
public static final int ITALIC | 文字顯示風(fēng)格為斜體 |
public static final int PLAIN | 文字顯示風(fēng)格為普通樣式 |
方法 | 作用 |
---|---|
public Font(String name,int style,int size) | 實(shí)例化對(duì)象,指定顯示風(fēng)格及大小 |
public String getFontName() | 得到字體的名稱 |
演示
import javax.swing.*; import java.awt.*; public class Hello { public static void main(String[] args) { JFrame frame = new JFrame("一"); JLabel label = new JLabel("HELLO",JLabel.CENTER); Font font = new Font("Serief",Font.ITALIC+Font.BOLD,28); label.setFont(font); label.setForeground(Color.BLUE); frame.add(label); Dimension d = new Dimension(); d.setSize(500,600); frame.setSize(d); frame.setBackground(Color.black); Point point = new Point(300,200); frame.setLocation(point); frame.setVisible(true); } }
設(shè)置圖片
ImageIcon類方法
方法 | 作用 |
---|---|
public ImageIcon(byte[] imageData) | 將保存圖片信息的byte數(shù)組設(shè)置到 ImageIcon 中 |
public ImageIcon(String filename) | 通過文件名稱創(chuàng)建 ImageIcon 對(duì)象 |
public ImageIcon(String filename,String description) | 設(shè)置圖片路徑以及圖片的簡(jiǎn)單描述 |
演示
import javax.swing.*; import java.awt.*; import java.io.File; public class Hello { public static void main(String[] args) { JFrame frame = new JFrame("一"); String picPath = "C:\\Users\\30452\\Desktop\\123.jpg"; Icon icon = new ImageIcon(picPath); JLabel lab = null; lab = new JLabel(icon,JLabel.CENTER); frame.add(lab); frame.setSize(800,800); frame.setBackground(Color.WHITE); frame.setLocation(300,200); frame.setVisible(true); } }
到此這篇關(guān)于Java程序圖形用戶界面設(shè)計(jì)之標(biāo)簽組件 的文章就介紹到這了,更多相關(guān)Java 圖形用戶界面標(biāo)簽組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的簡(jiǎn)單定義與使用方法
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之隊(duì)列的簡(jiǎn)單定義與使用方法,簡(jiǎn)單描述了隊(duì)列的功能、特點(diǎn),并結(jié)合java實(shí)例形式分析了隊(duì)列的簡(jiǎn)單定義與使用方法,需要的朋友可以參考下2017-10-10myatisplus的saveOrUpdate的提交總是update問題
這篇文章主要介紹了myatisplus的saveOrUpdate的提交總是update問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11mybatis-plus分頁查詢的實(shí)現(xiàn)示例
這篇文章主要介紹了mybatis-plus分頁查詢的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08關(guān)于json序列化(javaBean轉(zhuǎn)Json的細(xì)節(jié)處理)
這篇文章主要介紹了關(guān)于json序列化(javaBean轉(zhuǎn)Json的細(xì)節(jié)處理),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-03-03spring?boot使用@Async注解解決異步多線程入庫(kù)的問題
最近在寫項(xiàng)目是需要添加異步操作來提高效率,所以下面這篇文章主要給大家介紹了關(guān)于spring?boot使用@Async注解解決異步多線程入庫(kù)問題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05java面向?qū)ο笤O(shè)計(jì)原則之迪米特法則分析詳解
這篇文章主要為大家介紹了java面向?qū)ο笤O(shè)計(jì)原則之迪米特法則的示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,學(xué)有所得2021-10-10SpringBoot2.0解決Long型數(shù)據(jù)轉(zhuǎn)換成json格式時(shí)丟失精度問題
這篇文章主要介紹了SpringBoot2.0解決Long型數(shù)據(jù)轉(zhuǎn)換成json格式時(shí)丟失精度問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06關(guān)于maven配置項(xiàng)目一直提示程序包不存在以及scope的坑
這篇文章主要介紹了關(guān)于maven配置項(xiàng)目一直提示程序包不存在以及scope的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11springboot全局配置文件與多環(huán)境配置的全過程
SpringBoot項(xiàng)目在多環(huán)境配置上表現(xiàn)的非常優(yōu)秀,只需要非常簡(jiǎn)單的操作就可以完成配置,下面這篇文章主要給大家介紹了關(guān)于springboot全局配置文件與多環(huán)境配置的相關(guān)資料,需要的朋友可以參考下2021-12-12