Java Tutorial/SWT/Display
Содержание
- 1 Causes the system hardware to emit a short sound if it supports this capability
- 2 Get color for the given constant
- 3 Get the bounds and client area of a display
- 4 Get the longest duration in milliseconds between two mouse button clicks that will be deemed a double-click.
- 5 Returns the button dismissal alignment
- 6 Returns the maximum allowed depth of icons on the display.
- 7 SWT Event Handling with Displays
- 8 To find the primary monitor, call getPrimaryMonitor.
- 9 Using Display Class
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:
- The Display object is the connection between the SWT and the underlying windowing system.
- Implementing the SWT event loop in terms of a platform event model
- Providing methods for accessing information about the operating system
- Managing operating system resources used by SWT
- Only a single Display instance is required in most SWT-based applications.
- The thread that creates the Display object is the event-loop thread (user-interface thread).