Java Tutorial/SWT/MessageBox
Содержание
- 1 A message box with the question icon, Yes/No button, and a simple question
- 2 Displaying a Message Box
- 3 Displaying Messages
- 4 MessageBox with Error Icon
- 5 MessageBox with Information Icon
- 6 MessageBox with Information Icon and OK Cancel Button
- 7 MessageBox with Question Icon and Yes No Button
- 8 MessageBox with Warning Icon and Yes No Cancel Button
- 9 MessageBox with Working Icon and Abort Retry and Ignore Button
- 10 The Button Styles for MessageBox
- 11 The Icon Styles for MessageBox
A message box with the question icon, Yes/No button, and a simple question
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MesssageBoxQuestionIconYESNOButton {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION |SWT.YES | SWT.NO);
messageBox.setMessage("Is this question simple?");
int rc = messageBox.open();
System.out.println(rc == SWT.YES);
System.out.println(rc == SWT.NO);
display.dispose();
}
}
Displaying a Message Box
A message box contains four customizable pieces of information:
- The text in the title bar
- The text message displayed within the dialog
- The icon displayed within the dialog
- The buttons displayed within the dialog
Displaying Messages
Use the MessageBox class for displaying some simple text or asking a question, and receiving one of the following responses:
- OK
- Yes
- No
- Cancel
- Retry
- Abort
- Ignore
MessageBox with Error Icon
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MessageBoxErrorIcon {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
int style = SWT.ICON_ERROR;
//SWT.ICON_QUESTION | SWT.YES | SWT.NO
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Message");
int rc = messageBox.open();
switch (rc) {
case SWT.OK:
System.out.println("SWT.OK");
break;
case SWT.CANCEL:
System.out.println("SWT.CANCEL");
break;
case SWT.YES:
System.out.println("SWT.YES");
break;
case SWT.NO:
System.out.println("SWT.NO");
break;
case SWT.RETRY:
System.out.println("SWT.RETRY");
break;
case SWT.ABORT:
System.out.println("SWT.ABORT");
break;
case SWT.IGNORE:
System.out.println("SWT.IGNORE");
break;
}
display.dispose();
}
}
MessageBox with Information Icon
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MessageBoxInformationIcon {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
int style = SWT.ICON_INFORMATION;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Message");
int rc = messageBox.open();
switch (rc) {
case SWT.OK:
System.out.println("SWT.OK");
break;
case SWT.CANCEL:
System.out.println("SWT.CANCEL");
break;
case SWT.YES:
System.out.println("SWT.YES");
break;
case SWT.NO:
System.out.println("SWT.NO");
break;
case SWT.RETRY:
System.out.println("SWT.RETRY");
break;
case SWT.ABORT:
System.out.println("SWT.ABORT");
break;
case SWT.IGNORE:
System.out.println("SWT.IGNORE");
break;
}
display.dispose();
}
}
MessageBox with Information Icon and OK Cancel Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MessageBoxInformationIconOKCancelButton {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
int style = SWT.ICON_INFORMATION |SWT.OK | SWT.CANCEL;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Message");
int rc = messageBox.open();
switch (rc) {
case SWT.OK:
System.out.println("SWT.OK");
break;
case SWT.CANCEL:
System.out.println("SWT.CANCEL");
break;
case SWT.YES:
System.out.println("SWT.YES");
break;
case SWT.NO:
System.out.println("SWT.NO");
break;
case SWT.RETRY:
System.out.println("SWT.RETRY");
break;
case SWT.ABORT:
System.out.println("SWT.ABORT");
break;
case SWT.IGNORE:
System.out.println("SWT.IGNORE");
break;
}
display.dispose();
}
}
MessageBox with Question Icon and Yes No Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MessageBoxQuestionIconYESNOButton {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
int style = SWT.ICON_QUESTION |SWT.YES | SWT.NO;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Message");
int rc = messageBox.open();
switch (rc) {
case SWT.OK:
System.out.println("SWT.OK");
break;
case SWT.CANCEL:
System.out.println("SWT.CANCEL");
break;
case SWT.YES:
System.out.println("SWT.YES");
break;
case SWT.NO:
System.out.println("SWT.NO");
break;
case SWT.RETRY:
System.out.println("SWT.RETRY");
break;
case SWT.ABORT:
System.out.println("SWT.ABORT");
break;
case SWT.IGNORE:
System.out.println("SWT.IGNORE");
break;
}
display.dispose();
}
}
MessageBox with Warning Icon and Yes No Cancel Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MessageBoxWarningBoxYESNOCancelButton {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
int style = SWT.ICON_WARNING | SWT.YES | SWT.NO | SWT.CANCEL;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Message");
int rc = messageBox.open();
switch (rc) {
case SWT.OK:
System.out.println("SWT.OK");
break;
case SWT.CANCEL:
System.out.println("SWT.CANCEL");
break;
case SWT.YES:
System.out.println("SWT.YES");
break;
case SWT.NO:
System.out.println("SWT.NO");
break;
case SWT.RETRY:
System.out.println("SWT.RETRY");
break;
case SWT.ABORT:
System.out.println("SWT.ABORT");
break;
case SWT.IGNORE:
System.out.println("SWT.IGNORE");
break;
}
display.dispose();
}
}
MessageBox with Working Icon and Abort Retry and Ignore Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class MessageBoxWorkingIconAbortRetryIgnoreButton {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
int style = SWT.ICON_WORKING |SWT.ABORT | SWT.RETRY | SWT.IGNORE;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setMessage("Message");
int rc = messageBox.open();
switch (rc) {
case SWT.OK:
System.out.println("SWT.OK");
break;
case SWT.CANCEL:
System.out.println("SWT.CANCEL");
break;
case SWT.YES:
System.out.println("SWT.YES");
break;
case SWT.NO:
System.out.println("SWT.NO");
break;
case SWT.RETRY:
System.out.println("SWT.RETRY");
break;
case SWT.ABORT:
System.out.println("SWT.ABORT");
break;
case SWT.IGNORE:
System.out.println("SWT.IGNORE");
break;
}
display.dispose();
}
}
The Button Styles for MessageBox
StyleDescriptionSWT.OKDisplays an OK buttonSWT.OK | SWT.CANCELDisplays an OK and a Cancel buttonSWT.YES | SWT.NODisplays a Yes and a No buttonSWT.YES | SWT.NO | SWT.CANCELDisplays a Yes, a No, and a Cancel buttonSWT.RETRY | SWT.CANCELDisplays a Retry and a Cancel buttonSWT.ABORT | SWT.RETRY | SWT.IGNOREDisplays an Abort, a Retry, and an Ignore button
The Icon Styles for MessageBox
StyleDescriptionSWT.ICON_ERRORDisplays the error iconSWT.ICON_INFORMATIONDisplays the information iconSWT.ICON_QUESTIONDisplays the question iconSWT.ICON_WARNINGDisplays the warning iconSWT.ICON_WORKINGDisplays the working icon