java - 更改 Java 中 MessageBox 中按钮的预选?

标签 java messagebox

我有以下情况:

MessageBox messageBox = new MessageBox(GetShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
      messageBox.setText("Question");
      messageBox.setMessage("Message");
      int answer = messageBox.open();

这会输出一个消息框,其中预先选择了"is"按钮。是否可以将初始选择的"is"更改为“否”,或者只能通过对话框来完成?

最佳答案

不,没有办法做到这一点。这种行为是给定的。不过,您可以通过添加两个按钮并在您想要的按钮上调用 forceFocus 来创建自己的对话框:

Button button1 = new Button(shell, SWT.PUSH);

Button button2 = new Button(shell, SWT.PUSH);

button1.setText("Click me!");
button2.setText("Look at me, I have the focus now.");

//Here you set the focus to the second button
button2.forceFocus();
button1.setBounds(0, 0, 100, 30);
button2.setBounds(200, 0, 200, 30);

shell.pack();
shell.open();
//Prevent from closing
while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
    display.sleep();
  }
}
display.dispose();

关于java - 更改 Java 中 MessageBox 中按钮的预选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42573675/

相关文章:

java - 保存 Canvas 只保存背景,不保存绘图

java - 有没有办法忽略 'Unreachable statement' 错误?

c# - WPF MessageBox 取消复选框检查

java - NumberFormatException 指向整数的 double 输入

java - Div 随图像动态更新..... JSF

c - Linux 中 C 中的消息框

python - 改变 tkinter 消息框的大小

c# - MessageBox 按钮 - 设置语言?

delphi - 如何抑制 Delphi DataSnap 错误消息对话框?

java - 如何将Retrofit onResponse方法返回的Json数据发送到其他类?