Java Implementation class MSN, QQ friends online notification Interface

Source: Internet
Author: User

I believe everyone has used instant chat software such as MSN and QQ, so it is no stranger to the online reminder function of their friends? A small page is displayed in the lower right corner of the screen. The page gradually rises and disappears. Can we make the same functions in our own programs? Yes! I use the SWT user interface components of JAVA and eclipse to implement this function.

  What is SWT?

SWT was originally a graphical interface API written by the eclipse project team for the development of eclipse IDE. during runtime, SWT first checks whether the local machine has the same interface elements. If yes, it directly calls the display, if not, simulate the display. Its operating mechanism makes it much faster than AWT and SWING.

For more information, see: http://www.eclipse.org/swt

  Writing ideas

First, obtain the user's screen size, and use the screen height minus the height of the popup interface to calculate the highest position displayed on the screen (when the interface moves to this position, it stops moving ).

Rectangle area = Display.getDefault().getClientArea();
int upPosition = area.height - 100;

Use the screen height and the height of the popup interface to calculate the initial position of the popup interface (invisible during initialization, and then slowly move up to the upPosition point to stop moving, and then the page disappears after several seconds ).

int downPosition = area.height + 100;
We use a thread to implement the position movement. After the interface is initialized, the start () method is called to run this thread. The thread cyclically determines whether the downPosition is smaller than upPosition, if the value is smaller than the value, it indicates that the border of the popup interface is set to downPosition before it is stopped for 10 ms. If the value of downPosition is greater than upPosition, it indicates that the popup interface has moved to the highest position. Call sleep () to pause for 5 seconds and then close the interface and exit the program. That's simple. OK, Lets go! The whole program code is given below:

Description:

(Test is the main interface. click the button above and call Popup to display a popup interface like MSN and QQ in the lower right corner .)

Figure 1 shows the implementation in the source code, and Figure 2 shows the modified interface (just like QQ .)


Figure 1 Figure 2
Source code:

// Test. java
// There is only one button on the main interface. When you click Popup, the pop-up interface like MSN and QQ is displayed in the lower right corner.

Import org. eclipse. swt. SWT;
Import org. eclipse. swt. events. SelectionAdapter;
Import org. eclipse. swt. events. SelectionEvent;
Import org. eclipse. swt. widgets. Button;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Shell;

Public class Test {

Public static void main (String [] args ){

Final Display display = new Display ();
Shell shell = new Shell ();
Shell. setText ("aaa ");
Shell. setSize (250,150 );

Final Button button = new Button (shell, SWT. NONE );
Button. setBounds (50, 20,100, 25 );
Button. setText ("button ");
// Listen to the button event. When the user clicks, the Popup class is called to display the popup interface.
Button. addSelectionListener (new SelectionAdapter (){
Public void widgetSelected (SelectionEvent e ){
// Instantiate the popup class. The constructor is the prompt information displayed on the popup interface.
Popup popup = new Popup ("Your friend xxx is online. ");
Popup. start ();
}
});

Shell. open ();

While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Display. dispose ();
}
}

// Popup. java
// Implement popup notifications for friends like MSN and QQ

Import org. eclipse. swt. SWT;
Import org. eclipse. swt. graphics. Rectangle;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Shell;
Import org. eclipse. swt. widgets. Text;

Public class Popup extends Thread {

Shell shell;

Protected int moveStep = 2; // The pixel that is moved each time
Protected int upPosition; // The top coordinate that can be moved
Protected int downPosition; // The border coordinate of the current popup
Protected int leftPosition; // the coordinate of the Left Border of popup public Popup (final String message ){

Shell = new Shell (SWT. ON_TOP );
Text text = new Text (shell, SWT. MULTI | SWT. WRAP );
Text. setBounds (10, 20,180, 80 );
Text. setBackground (shell. getBackground ());
Text. setText (message );

// Large screen size
Rectangle area = Display. getDefault (). getClientArea ();

UpPosition = area. height-100; // calculate the maximum position of the popup interface displayed on the screen.
DownPosition = area. height + 100; // calculate the initial position of the popup interface.
LeftPosition = area. width-180;

Shell. setSize (180,100 );

// Initialize the popup position
Shell. setLocation (leftPosition, downPosition );

Shell. open ();

}

Public void run (){

Display display = shell. getDisplay ();
While (true ){
Try {
Thread. sleep (10 );

// Determine whether the current position is smaller than the highest position that can appear. If the value is smaller than the current position, it indicates that it can be moved up.
If (downPosition-moveStep)> upPosition ){
Display. asyncExec (new Runnable (){
Public void run (){
Shell. setLocation (leftPosition, downPosition-moveStep );
DownPosition-= moveStep;
}
});
// At this time, the window has been moved to the highest position. After 5 seconds, close the window and exit.
} Else {
Thread. sleep (5000 );
Display. asyncExec (new Runnable (){
Public void run (){
Shell. dispose ();
}
});
}
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
}

The source code above completes the function shown in figure 1, which can be modified by the reader to make its interface more powerful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.