button 按钮
button 组件支持用户出发一个操作。
XML 元素 |
|
|---|---|
Java 类 |
|
基本用法
一个按钮可以显示文字、图标或两者都显示。使用 title 属性配置一个提示文本。
<button id="helloButton"
text="Say hello!"
title="Click me!"/>
<button id="saveButton1"
text="Save"
icon="ARCHIVE"/>
<button id="saveButton2"
title="Save"
icon="ARCHIVE"/>
使用编程式操作
在 Java 中,可以创建操作并通过 setAction() 为按钮设置操作。通过操作定义按钮的文字以及点击时需要执行的操作。
<button id="buttonAction"/>
@ViewComponent
protected JmixButton buttonAction;
@Autowired
protected Notifications notifications;
@Subscribe
protected void onInit(InitEvent event) {
buttonAction.setAction(
new BaseAction("action")
.withText("Click me!")
.withHandler(actionPerformedEvent -> notifications.show("Action performed"))
);
}
使用图片
按钮的图标可以是一个组件,例如 image。下面的示例中,加载了一个图片并通过 setIcon() 方法为按钮设置。
<button id="logoButton" themeNames="icon"/>
private static final String LOGO_SRC_PATH = "/META-INF/resources/icons/jmix-logo.png";
@ViewComponent
private JmixButton logoButton;
@Subscribe
public void onInit(InitEvent event) {
Image logo = new Image(DownloadHandler.forClassResource(getClass(), LOGO_SRC_PATH), "jmix-logo");
logo.setWidth("100px");
logoButton.setIcon(logo);
}
样式版本
使用 themeNames 属性配置一个或多个样式。
| 样式 | 描述 | 支持的主题 |
|---|---|---|
|
主要按钮样式 |
Aura, Lumo |
|
成功样式 |
Aura, Lumo |
|
警告样式 |
Aura, Lumo |
|
错误样式 |
Aura, Lumo |
|
对比样式 |
Lumo |
|
按钮较大 |
Aura, Lumo |
|
按钮较小 |
Aura, Lumo |
|
使用图标按钮样式 |
Aura, Lumo |
|
低优先级的按钮样式 |
Aura, Lumo |
|
适用于文本内容内部使用的内联按钮样式 |
Lumo |
XML 属性
The following attributes are specific to button:
| 名称 | 描述 | 默认值 |
|---|---|---|
|
设置按钮关联的视图和组件操作。参见 声明式操作。 |
— |
按钮在点击之后就自动禁用,主要用来防止(意外的)多次点击。之后可以通过调用 |
|
|
设置按钮的图标。 |
— |
|
将图标放在按钮文本之后。 |
|
|
控制按钮文本中的空格和换行如何处理。 |
|
下面是 button 支持的其他通用属性:
id - alignSelf - ariaLabel - ariaLabelledBy - autofocus - classNames - css - enabled - focusShortcut - height - maxHeight - maxWidth - minHeight - minWidth - tabIndex - text - themeNames - title - visible - width
事件和处理器
下面是 button 的特殊事件和处理器:
| 名称 | 描述 |
|---|---|
当用户点击按钮时发送。用 |
|
当用户在短时间点击按钮两次时发送。 |
|
当用户单击后,距离下一次点击的时间超出双击的判断时,发送单击事件。 |
下面是 button 支持的其他通用事件:
XML 内部元素
A button can include icon, prefix and suffix, and tooltip components as its nested elements.