Java Tutorial/SWT/MessageBox — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:21, 31 мая 2010

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:

  1. The text in the title bar
  2. The text message displayed within the dialog
  3. The icon displayed within the dialog
  4. 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:

  1. OK
  2. Yes
  3. No
  4. Cancel
  5. Retry
  6. Abort
  7. 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