A Java SWT DesktopProgramSummary from development to packaging (1)
-- Overview and SWT Problems
I. Overview:
I have been using Kingsoft typing to practice English for a few days (my English is poor ).ArticleAll data has been swept n times. I can't help but think about how to add some external files. I found it too difficult to import the external files of Kingsoft typing. I have to manually copy the content to it. After reading the figure below, we can see that the efficiency is not high.
I just want to write a small software that can be imported in batches by myself. It is small but dirty. (In fact, the main purpose is to familiarize yourself with Java SE Development)
Here, I will mainly write about the problems, solutions, and notes encountered during this write process. For future use! Let's take a look at my achievements!
Ii. Questions about SWT
1. Remove the outer border of the SWT window: Shell = new shell (SWT. no_trim );
2. After the SWT window border is removed, the SWT window cannot be dragged.So you need to add events by yourself. You can press and hold the mouse to manually open the mouse window and move it to the place where the mouse is opened.
1) write an internal string that inherits the listener
1 // Window Movement 2 Private Class Shellmovelistenter Implements Listener { 3 Public Void Handleevent (event arg0 ){ 4 Switch (Arg0.type ){ 5 Case SWT. mousedown: 6 P. x = Arg0.x; 7 P. Y = Arg0.y; 8 Break ; 9 Case SWT. mousemove: 10 If (P. x =-1 ){ 11 Break ; 12 } 13 Point point = Shell. todisplay (arg0.x, arg0.y ); 14 Shell. setlocation (point. X-p. x, point. Y- P. y ); 15 Break ; 16 Case SWT. mouseup: 17 P. x =-1 ; 18 P. Y =-1 ; 19 Break ; 20 21 Default : 22 Break ; 23 } 24 } 25 }
2), bind the shell to the event.
1Listener listener =NewShellmovelistenter ();2 Shell. addlistener (SWT. mousedown, listener );3 Shell. addlistener (SWT. mousemove, listener );4Shell. addlistener (SWT. mouseup, listener );
3. Set the window to display in the middle of the screen
// Screen Resolution Rectangle area = Display. getdefault (). getclientarea (); Int Required wwidth = Area. width; Int Windowheight = Area. height; // Obtain the window width and height. Int Width = Shell. getbounds (). width; Int Height = Shell. getbounds (). height; // Set window position Int X = (wxwwidth-width)/2 ; Int Y = (windowheight-height)/2; Shell. setlocation (x, y );
4. Open the folder option box and set the obtained path to text.
1 // Open File Option box 2 Public String openfile (string text ){ 3 Directorydialog dd = New Directorydialog (Shell ); 4 Dd. settext (text ); 5 Dd. setfilterpath ("systemdrive"); 6 Dd. setmessage ("what is this ?? " ); 7 String selecteddir = Dd. open (); 8 Return Selecteddir; 9 }
Button. addselectionlistener (New selectionadapter () {public void widgetselected (selectionevent arg0) {string Path = openfile ("select the folder directory to import! "); If (path! = NULL) filetext. settext (PATH );}});
5. External resource Path ProblemsFor example, the background image is recommended to be placed under the project. In this way, you do not need to package resource files during packaging. My project structure is as follows:
1), unavailable:Stringpath = classloader. getsystemresource ("Res/"). getpath () + "bg.jpg ";// After packaging, a null pointer exception is reported. I don't know what is going on.
Recommended:Path1 = system. getproperty ("user. dir ");// Obtain the root directory of the project.
2) Chinese:Path1 = urldecoder. Decode (path1, "UTF-8 ");// Perform transcoding. Otherwise, an error occurred while finding the path.
6. Set the fade-in and fade-out effects for window opening and closing
1), opening: fade
Int alpha = 0; shell. setalpha (0); shell. open (); While (shell. getalpha () <255) {shell. setalpha (alpha ++); try {thread. sleep (3);} catch (interruptedexception e) {e. printstacktrace ();}}
2). When it is disabled: fade away
closebtn. addselectionlistener (New selectionadapter () {// close the window public void widgetselected (selectionevent event) {int alpha = 254; while (! (Shell. getalpha () <= 0) {shell. setalpha (alpha --); try {thread. sleep (3);} catch (interruptedexception E2) {e2.printstacktrace ();} shell. close () ;}});