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

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

Версия 17:44, 31 мая 2010

Causes the system hardware to emit a short sound if it supports this capability

import org.eclipse.swt.widgets.Display;
public class MainClass {
  static Display display = new Display();
  public static void main(String[] args) {
    display.beep();
  }
}





Get color for the given constant

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
public class MainClass {
  static Display display = new Display();
  public static void main(String[] args) {
    Color col = display.getSystemColor(SWT.COLOR_BLUE);
  }
}



The returned color should not be freed because it was allocated and managed by the system.


Get the bounds and client area of a display

import org.eclipse.swt.widgets.Display;
public class DisplayBounds{
  public static void main(String[] args) {
    Display display = new Display();
    System.out.println("Display Bounds=" + display.getBounds() + " Display ClientArea="
        + display.getClientArea());
    display.dispose();
  }
}





Get the longest duration in milliseconds between two mouse button clicks that will be deemed a double-click.

import org.eclipse.swt.widgets.Display;
public class MainClass {
  static Display display = new Display();
  public static void main(String[] args) {
    System.out.println(display.getDoubleClickTime());
  }
}





Returns the button dismissal alignment

Alignment can be either SWT.LEFT or SWT.RIGHT. The button dismissal alignment is used when positioning the default dismissal button for a dialog. For example, if alignement is LEFT, the button order should be OK, CANCEL.



import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
public class MainClass {
  static Display display = new Display();
  public static void main(String[] args) {
    System.out.println(display.getDismissalAlignment() == SWT.LEFT);
    System.out.println(display.getDismissalAlignment() == SWT.RIGHT);
  }
}





Returns the maximum allowed depth of icons on the display.

import org.eclipse.swt.widgets.Display;
public class MainClass {
  static Display display = new Display();
  public static void main(String[] args) {
    System.out.println(display.getIconDepth());
  }
}





SWT Event Handling with Displays

The readAndDispatch reads events from the operating system"s event queue and then dispatches them appropriately.



import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTEventHandlingDisplay {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Hello, world!");
    shell.open();
    while(! shell.isDisposed()) { // Event loop.
        if(! display.readAndDispatch())
          display.sleep();
    }
    display.dispose();
  }
}





To find the primary monitor, call getPrimaryMonitor.

Get all the monitors attached to the device, call Display.getMonitors()



import org.eclipse.swt.widgets.Display;
public class MainClass {
  static Display display = new Display();
  public static void main(String[] args) {
    System.out.println(display.getPrimaryMonitor().getClientArea());
  }
}





Using Display Class

The primary functions of Display instances are as follows:

  1. The Display object is the connection between the SWT and the underlying windowing system.
  2. Implementing the SWT event loop in terms of a platform event model
  3. Providing methods for accessing information about the operating system
  4. Managing operating system resources used by SWT
  5. Only a single Display instance is required in most SWT-based applications.
  6. The thread that creates the Display object is the event-loop thread (user-interface thread).