一、这个类解决什么问题?
swing 原生按钮 jbutton 本身功能完整,但实际项目中经常需要:
- 统一按钮的样式(背景色、字体、边框)
- 鼠标悬停时改变光标和背景色
- 按钮带图标
- 快速创建特定场景的按钮(搜索按钮、重置按钮、操作列按钮等)
如果每次都用原生写法,代码会非常冗余。buttonutils 的作用就是:封装常用按钮的创建逻辑,一行代码搞定。
二、类源码
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 按钮的常用创建方法
* * 使用示例:
* 1. 创建默认样式按钮:
* jbutton btn = buttonutils.createdefaultbtn("查询", () -> dosearch());
* 2. 创建带图标的按钮:
* jbutton btn = buttonutils.createactionbtn("导出", "icons/export.png", () -> export());
* 3. 创建表格操作列按钮:
* jbutton btn = buttonutils.createoperatecolbtn("编辑", editicon, "#409eff", "#ffffff", () -> edit(rowdata));
*/
public class buttonutils {
/** 默认的按钮颜色 */
public static final string color_btn = "#34ae7f";
/** 白色字体 */
public static final string color_white = "#ffffff";
/** 默认的字体名称 */
public static final string font_name = "microsoft yahei";
// ==================== 标签按钮(label模拟) ====================
/**
* 创建标签按钮(用 jlabel 模拟按钮)
* @param text 按钮文字
* @param icon 图标路径
* @param btnaction 点击事件
* @return 标签按钮
*/
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;
}
// ==================== 表单按钮 ====================
/**
* 创建表单按钮(固定高度50,绿色背景白色文字)
* @param btntext 按钮文字
* @param btnicon 图标路径
* @param btnaction 点击事件
* @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;
}
// ==================== 文本按钮 ====================
/**
* 创建文本按钮(用 jlabel 模拟,默认主题色)
* @param text 文字
* @param mouseclicked 点击事件
* @return 标签按钮
*/
public static jlabel createtextbtn(string text, runnable mouseclicked) {
return createtextbtn(text, color.decode(color_btn), mouseclicked);
}
/**
* 创建文本按钮(可指定字体颜色)
* @param text 文字
* @param fontcolor 字体颜色
* @param mouseclicked 点击事件
* @return 标签按钮
*/
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;
}
// ==================== 通用按钮创建 ====================
/**
* 创建按钮(最灵活版本)
* @param text 按钮文字
* @param bgcolorhex 背景色(十六进制)
* @param fontcolorhex 字体颜色
* @param mouseclick 点击事件
* @param mouseentered 鼠标进入事件(可为null)
* @param mouseexited 鼠标离开事件(可为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;
}
/**
* 创建按钮(带图标)
* @param text 按钮文字
* @param iconpath 图标路径
* @param bgcolorhex 背景色
* @param fontcolorhex 字体颜色
* @param mouseclick 点击事件
* @param mouseentered 鼠标进入事件
* @param mouseexited 鼠标离开事件
* @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)) {
// 需自行实现,获取图片
// button.seticon();
}
return button;
}
/**
* 创建按钮(带图标,无背景色/字体色设置)
* @param text 按钮文字
* @param iconpath 图标路径
* @param mouseclick 点击事件
* @param mouseentered 鼠标进入事件
* @param mouseexited 鼠标离开事件
* @return 按钮
*/
public static jbutton createactionbtn(string text, string iconpath, runnable mouseclick,
consumer<jcomponent> mouseentered, consumer<jcomponent> mouseexited) {
return createactionbtn(text, iconpath, "", "", mouseclick, mouseentered, mouseexited);
}
/**
* 创建按钮(无图标,带悬停回调)
* @param text 按钮文字
* @param mouseclick 点击事件
* @param mouseentered 鼠标进入事件
* @param mouseexited 鼠标离开事件
* @return 按钮
*/
public static jbutton createactionbtn(string text, runnable mouseclick,
consumer<jcomponent> mouseentered, consumer<jcomponent> mouseexited) {
return createactionbtn(text, "", "", mouseclick, mouseentered, mouseexited);
}
/**
* 创建按钮(带图标和背景色,无悬停回调)
* @param text 按钮文字
* @param iconpath 图标路径
* @param bgcolorhex 背景色
* @param fontcolorhex 字体颜色
* @param mouseclick 点击事件
* @return 按钮
*/
public static jbutton createactionbtn(string text, string iconpath, string bgcolorhex,
string fontcolorhex, runnable mouseclick) {
return createactionbtn(text, iconpath, bgcolorhex, fontcolorhex, mouseclick, null, null);
}
/**
* 创建按钮(带背景色,无图标)
* @param text 按钮文字
* @param bgcolorhex 背景色
* @param fontcolorhex 字体颜色
* @param mouseclick 点击事件
* @return 按钮
*/
public static jbutton createactionbtn(string text, string bgcolorhex, string fontcolorhex, runnable mouseclick) {
return createactionbtn(text, "", bgcolorhex, fontcolorhex, mouseclick);
}
/**
* 创建按钮(带图标,默认背景色)
* @param text 按钮文字
* @param iconpath 图标路径
* @param mouseclick 点击事件
* @return 按钮
*/
public static jbutton createactionbtn(string text, string iconpath, runnable mouseclick) {
return createactionbtn(text, iconpath, "", "", mouseclick);
}
/**
* 创建按钮(纯文字,默认背景色)
* @param text 按钮文字
* @param mouseclick 点击事件
* @return 按钮
*/
public static jbutton createactionbtn(string text, runnable mouseclick) {
return createactionbtn(text, "", mouseclick);
}
// ==================== 操作列按钮 ====================
/**
* 创建表格操作列按钮(尺寸紧凑)
* @param text 按钮文字
* @param imageicon 图标
* @param bgcolorhex 背景色
* @param fontcolorhex 字体颜色
* @param mouseclick 点击事件
* @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;
}
// ==================== 快捷按钮 ====================
/**
* 创建默认样式按钮(主题色背景白色文字)
* @param text 按钮文字
* @param mouseclick 点击事件
* @return 按钮
*/
public static jbutton createdefaultbtn(string text, runnable mouseclick) {
return createactionbtn(text, color_btn, color_white, mouseclick);
}
/**
* 创建带图标的默认样式按钮
* @param text 按钮文字
* @param iconpath 图标路径
* @param mouseclick 点击事件
* @return 按钮
*/
public static jbutton createdefaultbtn(string text, string iconpath, runnable mouseclick) {
return createactionbtn(text, iconpath, color_btn, color_white, mouseclick);
}
/**
* 添加查询面板搜索按钮
* @param parent 父面板
* @param mouseclick 点击事件
*/
public static void createsearchbtn(jcomponent parent, runnable mouseclick) {
createsearchbtn(parent, "查询", mouseclick);
}
/**
* 添加查询面板搜索按钮(可自定义文字)
* @param parent 父面板
* @param text 按钮文字
* @param mouseclick 点击事件
*/
public static void createsearchbtn(jcomponent parent, string text, runnable mouseclick) {
// 搜索图片存储位置,如 icons/table/search-white.png
string iconpath = "";
jbutton button = createactionbtn(text, iconpath, "#34ae7f", "#ffffff", mouseclick);
parent.add(button);
}
/**
* 添加查询面板重置按钮
* @param parent 父面板
* @param mouseclick 点击事件
*/
public static void createresetbtn(jcomponent parent, runnable mouseclick) {
createresetbtn(parent, "重置", mouseclick);
}
/**
* 添加查询面板重置按钮(可自定义文字)
* @param parent 父面板
* @param text 按钮文字
* @param mouseclick 点击事件
*/
public static void createresetbtn(jcomponent parent, string text, runnable mouseclick) {
// 重置图片存储位置,如 icons/table/reset-gray.png
string iconpath = "";
jbutton button = createactionbtn(text, iconpath, mouseclick);
parent.add(button);
}
}
三、核心方法说明
标签按钮(label模拟):createlabelbtn:用 jlabel 模拟按钮,适合图标+文字的简单点击场景
表单按钮:createformbtn:固定高度50,绿色背景白色文字,适合表单提交按钮
文本按钮:createtextbtn:用 jlabel 模拟超链接样式,适合“忘记密码”等场景
通用按钮:createactionbtn:最灵活的按钮创建方法,提供多个重载版本
操作列按钮:createoperatecolbtn:表格行内操作按钮,尺寸紧凑
快捷按钮: createdefaultbtn:默认样式(主题色背景白色文字) createsearchbtn / createresetbtn:快捷创建查询/重置按钮并添加到父面板
四、使用示例
4.1 创建默认样式按钮
jbutton searchbtn = buttonutils.createdefaultbtn("查询", () -> {
system.out.println("执行查询");
});
panel.add(searchbtn);
4.2 创建带图标按钮
// 图片存储位置,如 icons/export.png
string iconpath = "";
jbutton exportbtn = buttonutils.createactionbtn("导出", iconpath, () -> {
system.out.println("执行导出");
});
panel.add(exportbtn);
4.3 创建自定义颜色按钮
jbutton dangerbtn = buttonutils.createactionbtn("删除", "#f56c6c", "#ffffff", () -> {
int result = joptionpane.showconfirmdialog(null, "确认删除?", "提示", joptionpane.yes_no_option);
if (result == joptionpane.yes_option) {
system.out.println("执行删除");
}
});
panel.add(dangerbtn);
4.4 创建文本按钮(超链接样式)
jlabel linklabel = buttonutils.createtextbtn("忘记密码?", () -> {
system.out.println("跳转到找回密码");
});
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("编辑行数据");
});
五、方法重载说明
createactionbtn 提供了多个重载版本:
| 参数情况 | 使用方法 |
|---|---|
| 只要文字+点击事件 | createactionbtn(text, runnable) |
| 文字+图标+点击事件 | createactionbtn(text, iconpath, runnable) |
| 文字+背景色+字体色+点击事件 | createactionbtn(text, bgcolor, fontcolor, runnable) |
| 文字+图标+背景色+字体色+点击事件 | createactionbtn(text, iconpath, bgcolor, fontcolor, runnable) |
| 需要自定义悬停效果 | createactionbtn(text, runnable, mouseentered, mouseexited) |
六、注意事项
- 代码中涉及图标的地方已注释或标注,需要自行实现图标加载
- 使用了 color_btn、color_white、font_name,读者可在自己的常量类中定义
七、小结
buttonutils 封装了 swing 按钮的常见创建场景,核心设计思路:
- 统一样式:背景色、字体色、边框、光标
- 简化调用:一行代码完成按钮创建
- 灵活扩展:支持多种参数组合和自定义悬停效果
以上就是java swing实现自定义按钮组件的完整代码的详细内容,更多关于java swing自定义按钮组件的资料请关注代码网其它相关文章!
发表评论