Java?Swing實(shí)現(xiàn)自定義按鈕組件的完整代碼
一、這個(gè)類解決什么問(wèn)題?
Swing 原生按鈕 JButton 本身功能完整,但實(shí)際項(xiàng)目中經(jīng)常需要:
- 統(tǒng)一按鈕的樣式(背景色、字體、邊框)
- 鼠標(biāo)懸停時(shí)改變光標(biāo)和背景色
- 按鈕帶圖標(biāo)
- 快速創(chuàng)建特定場(chǎng)景的按鈕(搜索按鈕、重置按鈕、操作列按鈕等)
如果每次都用原生寫法,代碼會(huì)非常冗余。ButtonUtils 的作用就是:封裝常用按鈕的創(chuàng)建邏輯,一行代碼搞定。
二、類源碼
import cn.hutool.core.util.StrUtil;
import com.hyjk.met.base.module.constant.MetConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Consumer;
/**
* 按鈕工具類
* 封裝 Swing 按鈕的常用創(chuàng)建方法
* * 使用示例:
* 1. 創(chuàng)建默認(rèn)樣式按鈕:
* JButton btn = ButtonUtils.createDefaultBtn("查詢", () -> doSearch());
* 2. 創(chuàng)建帶圖標(biāo)的按鈕:
* JButton btn = ButtonUtils.createActionBtn("導(dǎo)出", "icons/export.png", () -> export());
* 3. 創(chuàng)建表格操作列按鈕:
* JButton btn = ButtonUtils.createOperateColBtn("編輯", editIcon, "#409EFF", "#FFFFFF", () -> edit(rowData));
*/
public class ButtonUtils {
/** 默認(rèn)的按鈕顏色 */
public static final String COLOR_BTN = "#34AE7F";
/** 白色字體 */
public static final String COLOR_WHITE = "#FFFFFF";
/** 默認(rèn)的字體名稱 */
public static final String FONT_NAME = "Microsoft YaHei";
// ==================== 標(biāo)簽按鈕(Label模擬) ====================
/**
* 創(chuàng)建標(biāo)簽按鈕(用 JLabel 模擬按鈕)
* @param text 按鈕文字
* @param icon 圖標(biāo)路徑
* @param btnAction 點(diǎn)擊事件
* @return 標(biāo)簽按鈕
*/
public static JLabel createLabelBtn(String text, String icon, Runnable btnAction) {
// 獲取圖片
// ImageIcon imageIcon = ;
JLabel btn = new JLabel(imageIcon);
btn.setText(text);
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (null != btnAction) {
btnAction.run();
}
}
});
return btn;
}
// ==================== 表單按鈕 ====================
/**
* 創(chuàng)建表單按鈕(固定高度50,綠色背景白色文字)
* @param btnText 按鈕文字
* @param btnIcon 圖標(biāo)路徑
* @param btnAction 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createFormBtn(String btnText, String btnIcon, Runnable btnAction) {
JButton button = createActionBtn(btnText, btnIcon, btnAction);
button.setPreferredSize(new Dimension(button.getPreferredSize().width, 50));
button.setBackground(Color.decode(COLOR_BTN));
button.setForeground(Color.WHITE);
button.setBorderPainted(false);
button.setIconTextGap(15);
return button;
}
// ==================== 文本按鈕 ====================
/**
* 創(chuàng)建文本按鈕(用 JLabel 模擬,默認(rèn)主題色)
* @param text 文字
* @param mouseClicked 點(diǎn)擊事件
* @return 標(biāo)簽按鈕
*/
public static JLabel createTextBtn(String text, Runnable mouseClicked) {
return createTextBtn(text, Color.decode(COLOR_BTN), mouseClicked);
}
/**
* 創(chuàng)建文本按鈕(可指定字體顏色)
* @param text 文字
* @param fontColor 字體顏色
* @param mouseClicked 點(diǎn)擊事件
* @return 標(biāo)簽按鈕
*/
public static JLabel createTextBtn(String text, Color fontColor, Runnable mouseClicked) {
JLabel textBtn = new JLabel(text);
textBtn.setBorder(BorderFactory.createEmptyBorder(3, 0, 3, 0));
textBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
textBtn.setForeground(fontColor);
textBtn.setFont(new Font(FONT_NAME, Font.BOLD, 16));
textBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (null != mouseClicked) {
mouseClicked.run();
}
}
});
return textBtn;
}
// ==================== 通用按鈕創(chuàng)建 ====================
/**
* 創(chuàng)建按鈕(最靈活版本)
* @param text 按鈕文字
* @param bgColorHex 背景色(十六進(jìn)制)
* @param fontColorHex 字體顏色
* @param mouseClick 點(diǎn)擊事件
* @param mouseEntered 鼠標(biāo)進(jìn)入事件(可為null)
* @param mouseExited 鼠標(biāo)離開(kāi)事件(可為null)
* @return 按鈕
*/
public static JButton createActionBtn(String text, String bgColorHex, String fontColorHex,
Runnable mouseClick, Consumer<JComponent> mouseEntered,
Consumer<JComponent> mouseExited) {
JButton button = new JButton(text);
button.setMargin(new Insets(5, 10, 5, 10));
button.setFont(new Font(FONT_NAME, Font.PLAIN, 16));
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
button.setBackground(StrUtil.isNotBlank(bgColorHex) ? Color.decode(bgColorHex) : Color.decode("#F2F3F5"));
button.setForeground(StrUtil.isNotBlank(fontColorHex) ? Color.decode(fontColorHex) : Color.decode("#666666"));
button.setBorderPainted(false);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
if (null != mouseEntered) {
mouseEntered.accept(button);
}
}
@Override
public void mouseExited(MouseEvent e) {
if (null != mouseExited) {
mouseExited.accept(button);
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (null != mouseClick) {
mouseClick.run();
}
}
});
return button;
}
/**
* 創(chuàng)建按鈕(帶圖標(biāo))
* @param text 按鈕文字
* @param iconPath 圖標(biāo)路徑
* @param bgColorHex 背景色
* @param fontColorHex 字體顏色
* @param mouseClick 點(diǎn)擊事件
* @param mouseEntered 鼠標(biāo)進(jìn)入事件
* @param mouseExited 鼠標(biāo)離開(kāi)事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, String iconPath, String bgColorHex, String fontColorHex,
Runnable mouseClick, Consumer<JComponent> mouseEntered,
Consumer<JComponent> mouseExited) {
JButton button = createActionBtn(text, bgColorHex, fontColorHex, mouseClick, mouseEntered, mouseExited);
if (StrUtil.isNotBlank(iconPath)) {
// 需自行實(shí)現(xiàn),獲取圖片
// button.setIcon();
}
return button;
}
/**
* 創(chuàng)建按鈕(帶圖標(biāo),無(wú)背景色/字體色設(shè)置)
* @param text 按鈕文字
* @param iconPath 圖標(biāo)路徑
* @param mouseClick 點(diǎn)擊事件
* @param mouseEntered 鼠標(biāo)進(jìn)入事件
* @param mouseExited 鼠標(biāo)離開(kāi)事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, String iconPath, Runnable mouseClick,
Consumer<JComponent> mouseEntered, Consumer<JComponent> mouseExited) {
return createActionBtn(text, iconPath, "", "", mouseClick, mouseEntered, mouseExited);
}
/**
* 創(chuàng)建按鈕(無(wú)圖標(biāo),帶懸停回調(diào))
* @param text 按鈕文字
* @param mouseClick 點(diǎn)擊事件
* @param mouseEntered 鼠標(biāo)進(jìn)入事件
* @param mouseExited 鼠標(biāo)離開(kāi)事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, Runnable mouseClick,
Consumer<JComponent> mouseEntered, Consumer<JComponent> mouseExited) {
return createActionBtn(text, "", "", mouseClick, mouseEntered, mouseExited);
}
/**
* 創(chuàng)建按鈕(帶圖標(biāo)和背景色,無(wú)懸?;卣{(diào))
* @param text 按鈕文字
* @param iconPath 圖標(biāo)路徑
* @param bgColorHex 背景色
* @param fontColorHex 字體顏色
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, String iconPath, String bgColorHex,
String fontColorHex, Runnable mouseClick) {
return createActionBtn(text, iconPath, bgColorHex, fontColorHex, mouseClick, null, null);
}
/**
* 創(chuàng)建按鈕(帶背景色,無(wú)圖標(biāo))
* @param text 按鈕文字
* @param bgColorHex 背景色
* @param fontColorHex 字體顏色
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, String bgColorHex, String fontColorHex, Runnable mouseClick) {
return createActionBtn(text, "", bgColorHex, fontColorHex, mouseClick);
}
/**
* 創(chuàng)建按鈕(帶圖標(biāo),默認(rèn)背景色)
* @param text 按鈕文字
* @param iconPath 圖標(biāo)路徑
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, String iconPath, Runnable mouseClick) {
return createActionBtn(text, iconPath, "", "", mouseClick);
}
/**
* 創(chuàng)建按鈕(純文字,默認(rèn)背景色)
* @param text 按鈕文字
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createActionBtn(String text, Runnable mouseClick) {
return createActionBtn(text, "", mouseClick);
}
// ==================== 操作列按鈕 ====================
/**
* 創(chuàng)建表格操作列按鈕(尺寸緊湊)
* @param text 按鈕文字
* @param imageIcon 圖標(biāo)
* @param bgColorHex 背景色
* @param fontColorHex 字體顏色
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createOperateColBtn(String text, ImageIcon imageIcon, String bgColorHex,
String fontColorHex, Runnable mouseClick) {
JButton button = createActionBtn(text, imageIcon, bgColorHex, fontColorHex, mouseClick);
button.setMargin(new Insets(2, 8, 2, 8));
button.setFont(new Font(FONT_NAME, Font.PLAIN, 15));
Dimension dimension = button.getPreferredSize();
button.setPreferredSize(dimension);
button.setMinimumSize(dimension);
button.setMaximumSize(dimension);
return button;
}
// ==================== 快捷按鈕 ====================
/**
* 創(chuàng)建默認(rèn)樣式按鈕(主題色背景白色文字)
* @param text 按鈕文字
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createDefaultBtn(String text, Runnable mouseClick) {
return createActionBtn(text, COLOR_BTN, COLOR_WHITE, mouseClick);
}
/**
* 創(chuàng)建帶圖標(biāo)的默認(rèn)樣式按鈕
* @param text 按鈕文字
* @param iconPath 圖標(biāo)路徑
* @param mouseClick 點(diǎn)擊事件
* @return 按鈕
*/
public static JButton createDefaultBtn(String text, String iconPath, Runnable mouseClick) {
return createActionBtn(text, iconPath, COLOR_BTN, COLOR_WHITE, mouseClick);
}
/**
* 添加查詢面板搜索按鈕
* @param parent 父面板
* @param mouseClick 點(diǎn)擊事件
*/
public static void createSearchBtn(JComponent parent, Runnable mouseClick) {
createSearchBtn(parent, "查詢", mouseClick);
}
/**
* 添加查詢面板搜索按鈕(可自定義文字)
* @param parent 父面板
* @param text 按鈕文字
* @param mouseClick 點(diǎn)擊事件
*/
public static void createSearchBtn(JComponent parent, String text, Runnable mouseClick) {
// 搜索圖片存儲(chǔ)位置,如 icons/table/search-white.png
String iconPath = "";
JButton button = createActionBtn(text, iconPath, "#34AE7F", "#FFFFFF", mouseClick);
parent.add(button);
}
/**
* 添加查詢面板重置按鈕
* @param parent 父面板
* @param mouseClick 點(diǎn)擊事件
*/
public static void createResetBtn(JComponent parent, Runnable mouseClick) {
createResetBtn(parent, "重置", mouseClick);
}
/**
* 添加查詢面板重置按鈕(可自定義文字)
* @param parent 父面板
* @param text 按鈕文字
* @param mouseClick 點(diǎn)擊事件
*/
public static void createResetBtn(JComponent parent, String text, Runnable mouseClick) {
// 重置圖片存儲(chǔ)位置,如 icons/table/reset-gray.png
String iconPath = "";
JButton button = createActionBtn(text, iconPath, mouseClick);
parent.add(button);
}
}
三、核心方法說(shuō)明
標(biāo)簽按鈕(Label模擬):createLabelBtn:用 JLabel 模擬按鈕,適合圖標(biāo)+文字的簡(jiǎn)單點(diǎn)擊場(chǎng)景
表單按鈕:createFormBtn:固定高度50,綠色背景白色文字,適合表單提交按鈕
文本按鈕:createTextBtn:用 JLabel 模擬超鏈接樣式,適合“忘記密碼”等場(chǎng)景
通用按鈕:createActionBtn:最靈活的按鈕創(chuàng)建方法,提供多個(gè)重載版本
操作列按鈕:createOperateColBtn:表格行內(nèi)操作按鈕,尺寸緊湊
快捷按鈕: createDefaultBtn:默認(rèn)樣式(主題色背景白色文字) createSearchBtn / createResetBtn:快捷創(chuàng)建查詢/重置按鈕并添加到父面板
四、使用示例
4.1 創(chuàng)建默認(rèn)樣式按鈕
JButton searchBtn = ButtonUtils.createDefaultBtn("查詢", () -> {
System.out.println("執(zhí)行查詢");
});
panel.add(searchBtn);
4.2 創(chuàng)建帶圖標(biāo)按鈕
// 圖片存儲(chǔ)位置,如 icons/export.png
String iconPath = "";
JButton exportBtn = ButtonUtils.createActionBtn("導(dǎo)出", iconPath, () -> {
System.out.println("執(zhí)行導(dǎo)出");
});
panel.add(exportBtn);
4.3 創(chuàng)建自定義顏色按鈕
JButton dangerBtn = ButtonUtils.createActionBtn("刪除", "#F56C6C", "#FFFFFF", () -> {
int result = JOptionPane.showConfirmDialog(null, "確認(rèn)刪除?", "提示", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
System.out.println("執(zhí)行刪除");
}
});
panel.add(dangerBtn);
4.4 創(chuàng)建文本按鈕(超鏈接樣式)
JLabel linkLabel = ButtonUtils.createTextBtn("忘記密碼?", () -> {
System.out.println("跳轉(zhuǎn)到找回密碼");
});
panel.add(linkLabel);
4.5 查詢面板中使用
JPanel queryPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
queryPanel.add(new JLabel("用戶名:"));
queryPanel.add(new JTextField(10));
ButtonUtils.createSearchBtn(queryPanel, this::doSearch);
ButtonUtils.createResetBtn(queryPanel, this::doReset);
4.6 表格操作列中使用
// 獲取圖片ImageIcon
//ImageIcon editIcon = xxxx;
JButton editBtn = ButtonUtils.createOperateColBtn("編輯", editIcon, "#409EFF", "#FFFFFF", () -> {
System.out.println("編輯行數(shù)據(jù)");
});
五、方法重載說(shuō)明
createActionBtn 提供了多個(gè)重載版本:
| 參數(shù)情況 | 使用方法 |
|---|---|
| 只要文字+點(diǎn)擊事件 | createActionBtn(text, runnable) |
| 文字+圖標(biāo)+點(diǎn)擊事件 | createActionBtn(text, iconPath, runnable) |
| 文字+背景色+字體色+點(diǎn)擊事件 | createActionBtn(text, bgColor, fontColor, runnable) |
| 文字+圖標(biāo)+背景色+字體色+點(diǎn)擊事件 | createActionBtn(text, iconPath, bgColor, fontColor, runnable) |
| 需要自定義懸停效果 | createActionBtn(text, runnable, mouseEntered, mouseExited) |
六、注意事項(xiàng)
- 代碼中涉及圖標(biāo)的地方已注釋或標(biāo)注,需要自行實(shí)現(xiàn)圖標(biāo)加載
- 使用了 COLOR_BTN、COLOR_WHITE、FONT_NAME,讀者可在自己的常量類中定義
七、小結(jié)
ButtonUtils 封裝了 Swing 按鈕的常見(jiàn)創(chuàng)建場(chǎng)景,核心設(shè)計(jì)思路:
- 統(tǒng)一樣式:背景色、字體色、邊框、光標(biāo)
- 簡(jiǎn)化調(diào)用:一行代碼完成按鈕創(chuàng)建
- 靈活擴(kuò)展:支持多種參數(shù)組合和自定義懸停效果
以上就是Java Swing實(shí)現(xiàn)自定義按鈕組件的完整代碼的詳細(xì)內(nèi)容,更多關(guān)于Java Swing自定義按鈕組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
sharding-jdbc 兼容 MybatisPlus動(dòng)態(tài)數(shù)據(jù)源的配置方法
這篇文章主要介紹了sharding-jdbc 兼容 MybatisPlus動(dòng)態(tài)數(shù)據(jù)源的配置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
如何使用IntelliJ?IDEA寫一個(gè)簡(jiǎn)單的JSP網(wǎng)頁(yè)
目前市場(chǎng)上流傳很多jsp項(xiàng)目,雖然是很老的項(xiàng)目,但是對(duì)于畢業(yè)設(shè)計(jì)項(xiàng)目來(lái)說(shuō)還是很好的學(xué)習(xí)項(xiàng)目,下面這篇文章主要介紹了如何使用IntelliJ?IDEA寫一個(gè)簡(jiǎn)單的JSP網(wǎng)頁(yè),需要的朋友可以參考下2025-07-07
Mybatis反射核心類Reflector的實(shí)現(xiàn)
本文主要介紹了Mybatis反射核心類Reflector的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
基于SpringBoot和Vue實(shí)現(xiàn)分片上傳系統(tǒng)
最近想做一個(gè)關(guān)于文件上傳的個(gè)人小網(wǎng)盤,一開(kāi)始嘗試使用了OSS的方案,但是該方案對(duì)于大文件來(lái)說(shuō)并不友好,所以開(kāi)始嘗試分片上傳方案的探索,接下來(lái)小編給大家詳細(xì)的介紹一下如何基于SpringBoot和Vue實(shí)現(xiàn)分片上傳系統(tǒng),需要的朋友可以參考下2023-12-12
從0到1學(xué)SpringCloud之SpringCloud?gateway網(wǎng)關(guān)路由配置示例詳解
Spring?Cloud?Gateway的目標(biāo)提供統(tǒng)一的路由方式且基于Filter?鏈的方式提供了網(wǎng)關(guān)基本的功能,?例如:安全、監(jiān)控、指標(biāo)和限流?,這篇文章主要介紹了從0到1學(xué)SpringCloud之SpringCloud?gateway網(wǎng)關(guān)路由配置示例詳解,需要的朋友可以參考下2023-04-04
Spring Security @PreAuthorize注解分析
本教程介紹了如何使用 Spring 方法級(jí)安全和 @PreAuthorize 注解來(lái)保護(hù) RestController 方法,通過(guò)這些步驟,您可以確保只有具有適當(dāng)角色或權(quán)限的用戶才能訪問(wèn)特定的 REST API,感興趣的朋友跟隨小編一起看看吧2024-11-11
SpringBoot實(shí)現(xiàn)簡(jiǎn)單文件上傳功能
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)簡(jiǎn)單文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

