gpt4 book ai didi

java - 带有自定义按钮标题的 SWT MessageBox

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:26 25 4
gpt4 key购买 nike

我想在我的非 RCP SWT 应用程序中添加自定义按钮标题。

    MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.ABORT | SWT.RETRY | SWT.IGNORE);
messageBox.setText("Warning");
messageBox.setMessage("Save the changes before exiting?");
int buttonID = messageBox.open();
switch(buttonID) {
case SWT.YES:
// saves changes ...
case SWT.NO:
// exits here ...
break;
case SWT.CANCEL:
// does nothing ...
}
System.out.println(buttonID);

}

它工作正常,但我的按钮标题是“中止”、“重试”、“忽略”我想要自定义按钮标题,例如“覆盖”、“重命名”、“取消”。怎么做到的?
请帮忙。

*** 编辑 ********

我也试过

MessageDialog dialog = new MessageDialog(null, "Dangerous Activity", null,
"Are you sure you want to delete?", MessageDialog.CONFIRM,
new String[]{"Preview>", "Delete", "Cancel"}, 0)
{
protected void buttonPressed(int buttonId) {
setReturnCode(buttonId);
// close(); Call close for Delete or Cancel?
}};

但是 MessageDialog 要求应用程序是 RCP,因此没有导入所需的包。帮助。

最佳答案

这是一个非常简单的示例,说明如何在 SWT 中创建您自己的 Dialog(尽管使用 JFace 可以使用更舒适的方法来执行此操作):

public class CustomDialog extends Dialog
{
private String message = "";
private Shell shell;

public CustomDialog(Shell parent)
{
// Pass the default styles here
this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell = parent;
}

public CustomDialog(Shell parent, int style)
{
// Let users override the default styles
super(parent, style);
shell = parent;
}

public String getMessage()
{
return message;
}

public void setMessage(String message)
{
this.message = message;
}

public void open()
{
shell.setText(getText());
createContents(shell);
shell.pack();
shell.open();
Display display = getParent().getDisplay();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
}

/**
* Creates the dialog's contents
*
* @param shell
* the dialog window
*/
private void createContents(final Shell shell)
{
shell.setLayout(new GridLayout(3, true));

// Show the message
Label label = new Label(shell, SWT.NONE);
label.setText(message);
GridData data = new GridData();
data.horizontalSpan = 3;
label.setLayoutData(data);

// Display the input box
Label image = new Label(shell, SWT.NONE);
image.setImage(shell.getDisplay().getSystemImage(SWT.ICON_ERROR));
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.horizontalSpan = 3;
image.setLayoutData(data);

Button preview = new Button(shell, SWT.PUSH);
preview.setText("Preview");
data = new GridData(SWT.FILL, SWT.END, true, true);
preview.setLayoutData(data);
preview.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
System.out.println("Preview");
}
});

Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
data = new GridData(SWT.FILL, SWT.END, true, true);
delete.setLayoutData(data);
delete.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
System.out.println("Delete");
}
});

Button cancel = new Button(shell, SWT.PUSH);
cancel.setText("Cancel");
data = new GridData(SWT.FILL, SWT.END, true, true);
cancel.setLayoutData(data);
cancel.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent event)
{
shell.close();
}
});

shell.setDefaultButton(preview);
}

public static void main(String[] args)
{
CustomDialog dialog = new CustomDialog(new Shell());
dialog.setText("Title");
dialog.setMessage("Message");

dialog.open();
}
}

看起来像这样:

enter image description here

关于java - 带有自定义按钮标题的 SWT MessageBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14832378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com