Java Tutorial/SWT/Display

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

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

   <source lang="java">

import org.eclipse.swt.widgets.Display; public class MainClass {

 static Display display = new Display();
 public static void main(String[] args) {
   display.beep();
 }

}</source>





Get color for the given constant

   <source lang="java">

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);
 }

}</source>



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

   <source lang="java">

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();
 }

}</source>





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

   <source lang="java">

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());
 }

}</source>





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.



   <source lang="java">

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);
 }

}</source>





Returns the maximum allowed depth of icons on the display.

   <source lang="java">

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());
 }

}</source>





SWT Event Handling with Displays

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



   <source lang="java">

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();
 }

}</source>





To find the primary monitor, call getPrimaryMonitor.

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



   <source lang="java">

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());
 }

}</source>





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).