Java Swing to develop the simplest browser source code download

Source: Internet
Author: User

Original: Java Swing development of the simplest browser source code download

Source code: Http://www.zuidaima.com/share/1550463483628544.htm

Java Swing develops the simplest browser that has been tested ....

Package Com.zuidaima; Import Org.eclipse.swt.swt;import Org.eclipse.swt.browser.browser;import Org.eclipse.swt.browser.closewindowlistener;import Org.eclipse.swt.browser.locationadapter;import Org.eclipse.swt.browser.locationevent;import Org.eclipse.swt.browser.openwindowlistener;import Org.eclipse.swt.browser.progressadapter;import Org.eclipse.swt.browser.progressevent;import Org.eclipse.swt.browser.statustextevent;import Org.eclipse.swt.browser.statustextlistener;import Org.eclipse.swt.browser.titleevent;import Org.eclipse.swt.browser.titlelistener;import Org.eclipse.swt.browser.windowevent;import Org.eclipse.swt.events.keyadapter;import Org.eclipse.swt.events.keyevent;import Org.eclipse.swt.events.mouseadapter;import Org.eclipse.swt.events.mouseevent;import Org.eclipse.swt.events.selectionadapter;import Org.eclipse.swt.events.selectionevent;import Org.eclipse.swt.layout.griddata;import Org.eclipse.swt.layout.gridlayout;import Org.eclipse.swt.widgets.button;import ORG.ECLIPSE.SWT.Widgets.combo;import Org.eclipse.swt.widgets.composite;import Org.eclipse.swt.widgets.display;import Org.eclipse.swt.widgets.label;import Org.eclipse.swt.widgets.menu;import Org.eclipse.swt.widgets.menuitem;import Org.eclipse.swt.widgets.progressbar;import Org.eclipse.swt.widgets.shell;import Org.eclipse.swt.widgets.tabfolder;import org.eclipse.swt.widgets.tabitem;/** * Very good ... * @author www.zuidaima.com * */public class Liulanqi {//tag-based browser private volatile String Newurl = null;//Latest Input Link private volatile Boolean loadcomplet  ed = false;//indicates that the current page is fully imported into private volatile Boolean opennewitem=false;//to indicate that the new page opens in a new window/* * Browser's current label parameter */Private TabItem  tabitem_now;//current tab Item Private Browser browser_now;//Current function Browser/* * Browser settings parameter */private String homepage = "www.baidu.com";// Browser's homepage/* Browser Shape layout */private button button_back;//Back button Private button button_forward;//Forward button private button button_go; Forward button Private button button_stop;//stop button private Combo combo_address;//Address bar private Browser Browser_Default = null;//Browse window private ProgressBar progressbar_status;//Web page Open Progress table, that is, the page import situation Bar private Label label_status;//The final page open process display Show private Tabfolder tabfolder;//browser container private Composite composite_tool;//toolbar area private Composite Composite_browser ;//Browse window area private Composite composite_status;//status bar area protected display display; protected Shell Shell_default; /** * Launch the application * * @param args */public static void main (string[] args) {try {liulanqi window = n   EW Liulanqi ();  window.open ();  } catch (Exception e) {e.printstacktrace ();  }}/** * Open the window */public void Open () {display = Display.getdefault ();  Shell_default = new Shell (display);  Createcontents ();  Shell_default.open ();  Shell_default.layout ();  while (!shell_default.isdisposed ()) {if (!display.readanddispatch ()) display.sleep ();  }}/** * Create contents of the window */protected void createcontents () {shell_default.setsize (649, 448);  Shell_default.settext ("browser"); GridLayout Gl_Shell = new GridLayout (); Gl_shell.marginwidth = 0;//The horizontal distance of the component from the edge of the container gl_shell.marginheight = the vertical distance between the 0;//component and the container edge gl_shell.horizontalspacing = 0;//component  The horizontal distance between the Gl_shell.verticalspacing = 0;//components of the vertical distance shell_default.setlayout (Gl_shell);  /* * Create a browser interface *///createmenu ();//not implement Createtool ();  Createbrowser ();  Createstatus (); /* * Create browser-related event monitoring */Runthread (); }/* * Create basic toolbar, not including related event listener */private void Createtool () {composite_tool = new composite (Shell_default, SWT.  BORDER); Griddata () The first parameter is the horizontal arrangement, the second parameter is the vertical arrangement, the third is whether the horizontal preemption is, and the fourth parameter is whether the vertical preemption is griddata gd_composite = new Griddata (SWT. FILL, SWT.  CENTER, True, false);  Gd_composite.heighthint = 30;//height and width gd_composite.widthhint = 549;  Composite_tool.setlayoutdata (Gd_composite);  GridLayout fl_composite = new GridLayout ();  Fl_composite.numcolumns = 8;  Composite_tool.setlayout (Fl_composite); Button_back = new Button (Composite_tool, SWT.  NONE); Button_back.setlayoutdata (New Griddata, SWT. default);//set size and format Button_back.settext ("≪-"); Button_forward = new Button (Composite_tool, SWT.  NONE); Button_forward.setlayoutdata (New Griddata, SWT.  DEFAULT));    Button_forward.settext (","); combo_address = new Combo (Composite_tool, SWT.  BORDER); Final Griddata gd_combo_3 = new Griddata (SWT. FILL, SWT. Left, true, false);//When the window changes, the size of the auto-expand horizontal direction Gd_combo_3.widthhint = 300;//start width gd_combo_3.minimumwidth = 50;//set minimum width c  Ombo_address.setlayoutdata (Gd_combo_3); Button_go = new Button (Composite_tool, SWT.  NONE); Button_go.setlayoutdata (New Griddata, SWT.  DEFAULT));  Button_go.settext ("Go"); Button_stop = new Button (Composite_tool, SWT.  NONE); Button_stop.setlayoutdata (New Griddata, SWT.  DEFAULT));  Button_stop.settext ("Stop"); Final Label label = new Label (Composite_tool, SWT. SEPARATOR | Swt.  VERTICAL); Label.setlayoutdata (New Griddata (2, 17)); }/* * Create browser, do not include related event listener */private void Createbrowser () {composite_browser = new composite (Shell_default, SWT.  NONE); Final Griddata gd_composite = new Griddata (Swt. FILL, SWT.  Fill, true, true),//fills the window, and the horizontal and vertical directions change with the window Gd_composite.heighthint = 273;  Composite_browser.setlayoutdata (Gd_composite);  GridLayout gl_composite = new GridLayout (); Gl_composite.marginheight = 0;//makes the component up and down in the direction of the container Gl_composite.marginwidth = 0;//fills the assembly with the left and right sides of the container composite_browser.setlayout (gl_  Composite); Tabfolder = new Tabfolder (Composite_browser, SWT.  NONE); Final Griddata Gd_tabfolder = new Griddata (SWT. FILL, SWT.  FILL, True, true);  Gd_tabfolder.heighthint = 312;  Gd_tabfolder.widthhint = 585;      Tabfolder.setlayoutdata (Gd_tabfolder);    /* Add right-click function for Label */Tabfolder.addmouselistener (new Mouseadapter () {@Override public void mouseUp (MouseEvent e) { if (e.button==3) {//Right-click menu Menu_itemrightmouse=new menu (SHELL_DEFAULT,SWT.     POP_UP);     Tabfolder.setmenu (Menu_itemrightmouse); MenuItem menuitem_itemclose=new MenuItem (MENU_ITEMRIGHTMOUSE,SWT.     NONE);     Menuitem_itemclose.settext ("Close current Tab");  Menuitem_itemclose.addselectionlistener (New Selectionadapter () {    @Override public void widgetselected (Selectionevent e) {if (Tabfolder.getitemcount ()!=1) {//Not only one label is present in the case        Browser_now.dispose ();        Tabitem_now.dispose ();       Tabfolder.redraw ();        }else{//only one label Browser_now.seturl (": blank");       Browser_now.settext ("");     }      }     }); MenuItem menuitem_itemcloseall=new MenuItem (MENU_ITEMRIGHTMOUSE,SWT.     NONE);     Menuitem_itemcloseall.settext ("Close all labels"); Menuitem_itemcloseall.addselectionlistener (New Selectionadapter () {@Override public void widgetselected (Selectio      Nevent e) {shell_default.close ();    }     });      }   }  }); Final TabItem Tabitem_default = new TabItem (Tabfolder, SWT.  NONE); Browser_default = new browser (Tabfolder, SWT.  NONE);  Tabitem_default.setcontrol (Browser_default); Browser_default.seturl (homepage);//Display Browser home/* * Put the initialized tag on top, select */tabfolder.setselection (Tabitem_default); }/* * Create a status bar at the bottom of the browser, not including related event listener */private void Createstatus () {composite_status = new Composite (Shell_default, SWT.  NONE); Final Griddata gd_composite = new Griddata (SWT. FILL, SWT.  FILL, True, false);//Parameter True enables the status bar to automatically scale horizontally gd_composite.heighthint = 20;  Gd_composite.widthhint = 367;  Composite_status.setlayoutdata (Gd_composite);  GridLayout gl_composite = new GridLayout ();  Gl_composite.numcolumns = 2;  Gl_composite.marginbottom = 5;  Composite_status.setlayout (Gl_composite); Label_status = new Label (Composite_status, SWT.  NONE); Griddata gd_status = new Griddata (SWT. FILL, SWT.  CENTER, True, false);  Gd_status.heighthint = 13;  Gd_status.widthhint = 525;  Label_status.setlayoutdata (Gd_status); Progressbar_status = new ProgressBar (Composite_status, SWT. BORDER | Swt.  SMOOTH);  Progressbar_status.setlayoutdata (New Griddata (80, 12)); Progressbar_status.setvisible (false);//Open process initially invisible} private void Runthread () {/* * Browser new tab forward, Back button default availability is not available */bu  Tton_back.setenabled (FALSE);    Button_forward.setenabled (FALSE); /* * Get the current label and function of the browser browser */tAbitem_now=tabfolder.getitem (Tabfolder.getselectionindex ());     browser_now= (browser) Tabitem_now.getcontrol (); /* * When the selected event occurs, modify the current browser tab */Tabfolder.addselectionlistener (new Selectionadapter () {@Override public void Widgetsele    CTED (selectionevent e) {TabItem temp= (TabItem) E.item;     if (Temp!=tabitem_now) {//Prevent re-election of a label, prevent multiple triggering of the same event tabitem_now=temp;     browser_now= (browser) Tabitem_now.getcontrol (); System.out.println ("The current label has been modified");//DEBUG Statement/* * In the corresponding tab, the forward and backward button availability is not the same */if (browser_now.isbackenable     D ()) {//Back button availability button_back.setenabled (TRUE);     }else{button_back.setenabled (FALSE);     } if (browser_now.isforwardenabled ()) {//forward button availability button_forward.setenabled (TRUE);     }else{button_forward.setenabled (FALSE);     }          }   }  }); /* Add browser's back, forward, forward, stop button event listener */Button_back.addselectionlistener (new Selectionadapter () {@Override public void W idgetselected (selectionevent arg0) {if (Browser_now.isbackenaBled ()) {//This time can be back browser_now.back (); Button_forward.setenabled (TRUE);//next to advance, the forward button can be//system.out.println ("fallback");//DEBUG Statement} if (!browser_now.isbackenable    D ()) {//Do not rewind the next time, the Back button is not available button_back.setenabled (false);  }   }  }); Button_forward.addselectionlistener (New Selectionadapter () {@Override public void widgetselected (Selectionevent arg0     {if (browser_now.isforwardenabled ()) {//This time can forward browser_now.forward (); Button_back.setenabled (TRUE);//The Back button can be//system.out.println ("forward");//DEBUG Statement} if (!browser_now.isforwardenabled ()) {/         /next time not forward, the forward button is not available button_forward.setenabled (false);  }   }  });    Button_stop.addselectionlistener (New Selectionadapter () {@Override public void widgetselected (Selectionevent arg0) {   Browser_now.stop ();    }  });    Combo_address.addkeylistener (New Keyadapter () {//Enter the address bar manually, press ENTER to go to the appropriate URL @Override public void keyreleased (KeyEvent e) { if (E.keycode = = SWT. CR) {//Enter trigger event Browser_now.seturl (Combo_address.gettext ());  }   }  });   /* * 1> in open () write E.browser = Browser_new under Addopenwindowlistener (), import a new hyperlink that occurs only when you click on the link on the page and the link is not open on the new page.   * 2> the Open () under Addopenwindowlistener () does not write to E.browser = browser_new case, import a new hyperlink, * Only when the link on the page is clicked and the link is opened on the new page. * Except for the above two, of course also includes Browser.back (), Browser.forward (), Browser.go (), Browser.seturl () trigger, * but changing () does not write E.browser = Browser_new case, not triggered by browser.seturl () */Browser_now.addlocationlistener (new Locationadapter () {@Override public voi D changing (locationevent e) {//indicates that the hyperlink address changed if (opennewitem==false) {//New page is opened in the same tab button_back.setenabled (TRUE);//Back button is available, this sentence is the logical starting point for the back button to be used to determine}//system.out.println ("location_changing");//DEBUG statement} @Override public void changed (LOC Ationevent e) {//Found page link address combo_address.settext (e.location);//Change link address display/* * New page already open, browser Locationlistener    Already listening, Opennewitem reply to default value */if (opennewitem==true) {opennewitem=false; }//system.out.println ("location_changed");//DEBUG statement} }); /* * The percentage of imports of the new hyperlink page, which occurs when a new page is imported, the link address is known */Browser_now.addprogresslistener (new Progressadapter () {@Override PU Blic void Changed (Progressevent e) {//This event continues to occur during the import of the page progressbar_status.setmaximum (e.total);    E.total represents the numeric progressbar_status.setselection (e.current) from the first page to the final page;     if (e.current! = e.total) {//page has not been fully imported loadcompleted = false;     Progressbar_status.setvisible (TRUE);//The Import status bar of the page is visible} else {loadcompleted = true;    Progressbar_status.setvisible (false);//page Import condition bar not visible}//system.out.println ("progress_changed");//debug statement} @Override public void completed (Progressevent arg0) {//Occurs on one import page, this listener changed event before the last occurrence//system.out.println ("Progress_comple  Ted ");//DEBUG Statement}}); /* * Get page content process, text display Addprogresslistener () process, but also can detect the presence of the open page hyperlink, is to use the function to get * NEW link address */Browser_now.addstatustextli Stener (New Statustextlistener () {public void changed (Statustextevent e) {if (loadcompleted = = False) {Label_st    Atus.settext (E.text); }else {newurl = e.text;//page Import complete, capture the link that may open on the page}//system.out.println ("statustext_changed");//debug statement});     /* * Display prompt statement for page, occurs during new page import */Browser_now.addtitlelistener (new Titlelistener () {public void changed (Titleevent e) {    Shell_default.settext (E.title);    if (E.title.length () > 3) {//Displays the current page prompt character on the label Tabitem_now.settext (e.title.substring (0, 3) + "...");    } else {tabitem_now.settext (e.title);  } tabitem_now.settooltiptext (E.title);//label display Prompt}}); /* Opens a new page, the current open page The new link needs to occur when the new Window page opens. Addopenwindowlistener A sentence in open () * E.browser = browser_new; Key section. Contact the value transfer hubs for Addopenwindowlistener, Addvisibilitywindowlistener * and Adddisposelistener */Browser_ Now.addopenwindowlistener (New Openwindowlistener () {//Opens the clicked Link page in the current page public void open (WindowEvent e) {Browser Browser_new = new browser (Tabfolder, SWT.      NONE); TabItem tabitem_new = new TabItem (Tabfolder, SWT.      NONE);      Tabitem_new.setcontrol (browser_new); Tabfolder.setselection (Tabitem_new)///New open page label Tabfolder.redraw ();//Refresh Container Browser_new.seturl (newurl);//new Tab set new link address opennewitem=true;//      The new page opens in the new tab */* Key section, tells the new page by Browser_new Open, as long as the implementation of this sentence will not pop up the operating system default Browser */E.browser = browser_new; System.out.println ("Openwindowlistener_open");//DEBUG Statement/* * Add Event Listener for new tags in browser */Display.sync        Exec (New Runnable () {public void run () {runthread ();                      }      });    }    }); /* * Browser Shutdown event, close the current function browser, otherwise the main browser window is closed, and the process is running */Browser_now.addclosewindowlistener (new Closewindowlistener () {Publi   c void Close (WindowEvent e) {browser_now.dispose (); }  }); }}

Java Swing to develop the simplest browser source code download

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.