Java-JFrame-swing nested browser steps,

Source: Internet
Author: User

Java-JFrame-swing nested browser steps,

Java-JFrame-swing nested browser steps

1. functions to be implemented using swing nested browsers:

By embedding a browser in a form using java swing, you can display the content of another project in this browser. You only need to go back to the url of the home page of another project, in this way, another project can be properly displayed in a nested swing browser;

 

Ii. When using the methods in the following browser tool class, you only need to obtain the stirng type url of the homepage of another project from the server of another project and the string type title to be displayed in the upper left corner of the browser. The parameters are url and title;

(1) main steps:

 

1. Use the following java code to create a browser;

2. Create a JFrame form and perform basic settings on the form;

 

3. Set the positioning size displayed by the browser and add the browser to the JFrame form:

Note: The java code that adds the browser to the form must be added to the java code of the toolbar at the top of the browser to the form. Otherwise, the browser content cannot be displayed.;

4. Set the icon on the left of the toolbar on the top of the browser;

5. Set the title text on the left of the toolbar on the top of the browser;

6. Set the pictures and mouse events on the right of the toolbar at the top of the browser, such as minimization, maximization, and closure;

7. Add an event to the form: when the size of the form changes, the Java code in the method body is executed, because the size of the form can be changed;

8. The following line of code must be commented out in the browser tool class. Otherwise, this form will not pop up when other forms call this form;

 

// The downstream code must be commented out; otherwise, the form cannot be redirected. // NativeInterface. runEventPump ();

 

(2) All the code for the tool class of the browser is as follows:

Package com; import java. awt. borderLayout; import java. awt. color; import java. awt. flowLayout; import java. awt. font; import java. awt. image; import java. awt. event. componentAdapter; import java. awt. event. componentEvent; import java. awt. event. mouseAdapter; import java. awt. event. mouseEvent; import javax. swing. imageIcon; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JLayeredPane; import javax. swing. JPanel; import javax. swing. JRootPane; import javax. swing. swingUtilities; import com. util. resizeFrame; import com. util. screenSize; import chrriis. common. UIUtils; import chrriis. dj. nativeswing. swtimpl. nativeInterface; import chrriis. dj. nativeswing. swtimpl. components. JWebBrowser;/*** CS package browser, first visit the project homepage ** @ author admin **/public class EagleBrowser1 extends JPanel {private static final long serialVersionUID = 1L; private JPanel webBrowserPanel; private JWebBrowser webBrowser; private JFrame frame; private Double browserWidth; // form width private Double browserHeight; // form height public EagleBrowser1 () {}// constructor, pass url and title to create a CS inclusive browser form public EagleBrowser1 (String url, String title) {/*** create a browser */webBrowserPanel = new JPanel (new BorderLayout ()); webBrowser = new JWebBrowser (); webBrowser. navigate (url); webBrowser. setButtonBarVisible (false); webBrowser. setMenuBarVisible (false); webBrowser. setBarsVisible (false); webBrowser. setStatusBarVisible (false); webBrowserPanel. add (webBrowser, BorderLayout. CENTER); // add (webBrowserPanel, BorderLayout. CENTER); // webBrowser.exe cuteJavascript ("javascrpit: window. location. href = 'HTTP: // www.baidu.com '"); // webBrowser.exe cuteJavascript (" alert ('hahaha') "); // execute the Js Code UIUtils. setPreferredLookAndFeel (); NativeInterface. open (); SwingUtilities. invokeLater (new Runnable () {public void run () {frame = new JFrame (); // remove the decorative frame of the window. setUndecorated (true); // uses the specified window decoration style frame. getRootPane (). setWindowDecorationStyle (JRootPane. NONE); frame. setdefaclocloseoperation (JFrame. DISPOSE_ON_CLOSE); frame. setLocationByPlatform (true); frame. setLayout (null); // note the visibility, form size, and center positioning of the form. It is best to write the code in this order. // set the visible frame of the form. setVisible (true); frame. setResizable (true); // you can specify the width and height of a frame. setSize (1600,900); // set the frame to be centered in the form. setLocationRelativeTo (frame. getOwner ();/*** get the total width of the form */browserWidth = frame. getSize (). getWidth (); browserHeight = frame. getSize (). getHeight ();/*** insert a background image with a narrow bar in the browser's header * // create a layer-based JLayeredPaneJLayeredPane layeredPane = new JLayeredPane (); layeredPane. setBounds (0, 0, browserWidth. intValue (), 40); // Add a layered JLayeredPaneframe. getContentPane (). add (layeredPane, BorderLayout. CENTER); // create the image object ImageIcon img = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_head.jpg"); // you can specify the width and height of an image in the img format. setImage (img. getImage (). getScaledInstance (browserWidth. intValue (), 40, Image. SCALE_DEFAULT); JPanel = new JPanel (); panel. setBounds (0, 0, browserWidth. intValue (), 40); layeredPane. add (panel, JLayeredPane. DEFAULT_LAYER); JLabel lblNewLabel = new JLabel (""); panel. add (lblNewLabel); lblNewLabel. setIcon (img);/*** Add the webpage to the form frame. * // set the distance from the browser to the header. webBrowserPanel. setBounds (0, 29, browserWidth. intValue (), browserHeight. intValue ()-29); // Add the webpage's JPanel. Note: You must place this line of code in the frame below the Add JLayeredPane. getContentPane (). add (webBrowserPanel, BorderLayout. CENTER);/*** Insert the image on the left of the browser's header * // create the image object ImageIcon headLeftImg = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_head_left.jpg"); // you can specify the width and height of headLeftImg. setImage (headLeftImg. getImage (). getScaledInstance (24, 40, Image. SCALE_DEFAULT); JPanel headLeftPanel = new JPanel (); headLeftPanel. setBounds (0,-5, 24, 40); layeredPane. add (headLeftPanel, JLayeredPane. MODAL_LAYER); JLabel headLeftLabel = new JLabel (""); headLeftPanel. add (headLeftLabel); headLeftLabel. setIcon (headLeftImg);/*** Insert the title on the left of the browser form */JPanel titlePanel = new JPanel (); titlePanel. setBounds (32, 6, browserWidth. intValue ()-150, 20); layeredPane. add (titlePanel, JLayeredPane. MODAL_LAYER); // set the text alignment in JPanel to left alignment titlePanel. setLayout (new FlowLayout (FlowLayout. LEFT); // set JPanel to transparent, so that the background color behind the JPanel can be displayed as titlePanel. setBackground (null); titlePanel. setOpaque (false); JLabel titleLbel = new JLabel (); titleLbel. setText (title); titleLbel. setSize (100, 20); titleLbel. setHorizontalAlignment (JLabel. LEFT); titleLbel. setFont (new Font ("", 0, 12); titleLbel. setForeground (Color. white); titlePanel. add (titleLbel);/*** Insert the background image and function on the right * // create an image object ImageIcon headCloseImg = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_close_01.jpg"); // you can specify the width and height of the headCloseImg image. setImage (headCloseImg. getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); JPanel headClosePanel = new JPanel (); headClosePanel. setBounds (browserWidth. intValue ()-40,-5, 40, 40); layeredPane. add (headClosePanel, JLayeredPane. MODAL_LAYER); JLabel headCloseLabel = new JLabel (""); headClosePanel. add (headCloseLabel); headCloseLabel. setIcon (headCloseImg); headCloseLabel. addMouseListener (new MouseAdapter () {// click and close the image to disable the form. @ Overridepublic void mouseClicked (MouseEvent e) {frame. dispose (); // System. exit (0); // use dispose (); you can also close it, but it is not really close} // enter with the mouse, change to the closed background image @ Overridepublic void mouseEntered (MouseEvent e) {// create the image object ImageIcon closeImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_close_02.jpg"); // set the width and height of the image displayed in the form closeImg1.setImage (closeImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headCloseLabel. setIcon (closeImg1);} // move the mouse away. For the disabled background image @ Overridepublic void mouseExited (MouseEvent e) {// create the image object ImageIcon headCloseImg = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_close_01.jpg"); // you can specify the width and height of the headCloseImg image. setImage (headCloseImg. getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headCloseLabel. setIcon (headCloseImg) ;}});/*** Insert the maximized background image on the right and functions * // create an image object ImageIcon headMaxImg = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_middle_01.jpg"); // set the width and height of the image displayed in the form headMaxImg. setImage (headMaxImg. getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); JPanel headMaxPanel = new JPanel (); headMaxPanel. setBounds (browserWidth. intValue ()-40-40,-5, 40, 40); layeredPane. add (headMaxPanel, JLayeredPane. MODAL_LAYER); JLabel headMaxLabel = new JLabel (""); headMaxPanel. add (headMaxLabel); headMaxLabel. setIcon (headMaxImg); headMaxLabel. addMouseListener (new MouseAdapter () {// click and close the image to close the form. @ Overridepublic void mouseClicked (MouseEvent e) {// you can determine the current width of the form, if the width is the same as that of the Computer resolution, the width of the form is changed to the default value. if the width is smaller than the width of the Computer resolution, the form is changed to maximize if (frame. getSize (). getWidth () <ScreenSize. getScreenWidth () {frame. setExtendedState (JFrame. MAXIMIZED_BOTH); // maximize the form // create an image object ImageIcon maxImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_max_01.jpg"); // sets the width and height of the image displayed in the form maxImg1.setImage (maxImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMaxLabel. setIcon (maxImg1);} else {frame. setSize (1600,900); frame. setLocationRelativeTo (frame. getOwner (); // create the image object ImageIcon maxImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_middle_01.jpg"); // sets the width and height of the image displayed in the form maxImg1.setImage (maxImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMaxLabel. setIcon (maxImg1) ;}// enter with the mouse. for the disabled background image @ Overridepublic void mouseEntered (MouseEvent e) {// determine the current width of the form and the width of the Computer resolution if (frame. getSize (). getWidth () <ScreenSize. getScreenWidth () {// The width of the form is smaller than the width of the computer's resolution. The image should be displayed in the middle. // create the image object ImageIcon maxImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_middle_02.jpg"); // sets the width and height of the image displayed in the form maxImg1.setImage (maxImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMaxLabel. setIcon (maxImg1);} else {// create an image object ImageIcon maxImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_max_02.jpg"); // sets the width and height of the image displayed in the form maxImg1.setImage (maxImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMaxLabel. setIcon (maxImg1) ;}// move the mouse away. For the disabled background image @ Overridepublic void mouseExited (MouseEvent e) {// determine the current width of the form and the width of the Computer resolution if (frame. getSize (). getWidth () <ScreenSize. getScreenWidth () {// The width of the form is smaller than the width of the computer's resolution. The image should be displayed in the middle. // create the image object ImageIcon maxImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_middle_01.jpg"); // sets the width and height of the image displayed in the form maxImg1.setImage (maxImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMaxLabel. setIcon (maxImg1);} else {// create an image object ImageIcon maxImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_max_01.jpg"); // sets the width and height of the image displayed in the form maxImg1.setImage (maxImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMaxLabel. setIcon (maxImg1) ;}});/*** insert a minimized background image to the right and function * // create an image object ImageIcon headMinImg = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_min_01.jpg"); // set the width and height of the image displayed in the form. setImage (headMinImg. getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); JPanel headMinPanel = new JPanel (); headMinPanel. setBounds (browserWidth. intValue ()-40-40-40,-5, 40, 40); layeredPane. add (headMinPanel, JLayeredPane. MODAL_LAYER); JLabel headMinLabel = new JLabel (""); headMinPanel. add (headMinLabel); headMinLabel. setIcon (headMinImg); headMinLabel. addMouseListener (new MouseAdapter () {// click and close the image to disable the form. @ Overridepublic void mouseClicked (MouseEvent e) {frame. setExtendedState (JFrame. ICONIFIED); // minimize the form} // enter with the mouse. For the closed background image @ Overridepublic void mouseEntered (MouseEvent e) {// create the image object ImageIcon mainImg1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_min_02.jpg"); // set the width and height of the image displayed in the form mainImg1.setImage (mainImg1.getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMinLabel. setIcon (mainImg1);} // move the mouse away. For the disabled background image @ Overridepublic void mouseExited (MouseEvent e) {// create the image object ImageIcon headMinImg = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_min_01.jpg"); // set the width and height of the image displayed in the form. setImage (headMinImg. getImage (). getScaledInstance (40, 40, Image. SCALE_DEFAULT); headMinLabel. setIcon (headMinImg) ;}});/*** sets events for detecting changes in the form size */frame. addComponentListener (new ComponentAdapter () {@ Overridepublic void componentResized (ComponentEvent e) {/*** obtain the width and height of the form after the size change */browserWidth = frame. getSize (). getWidth (); browserHeight = frame. getSize (). getHeight ();/*** the width of the narrow background image at the top of the browser is changed */layeredPane. setBounds (0,-5, browserWidth. intValue (), 34); titlePanel. setBounds (32, 6, browserWidth. intValue ()-150, 20); // create the image object ImageIcon img1 = new ImageIcon (EagleBrowser1.class. getResource ("/images/browser_head.jpg"); // sets the width and height of the image displayed in the form img1.setImage (img1.getImage (). getScaledInstance (browserWidth. intValue (), 40, Image. SCALE_DEFAULT); panel. setBounds (0,-5, browserWidth. intValue (), 40); panel. add (lblNewLabel); lblNewLabel. setIcon (img1);/*** minimize, maximize, and close button image on the right side of the narrow bar at the top of the browser to locate the Image Based on the width */headClosePanel. setBounds (browserWidth. intValue ()-40,-5, 40, 40); // close the Image Positioning headMaxPanel at the top of the browser. setBounds (browserWidth. intValue ()-40-40,-5, 40, 40); // maximize the image position on the top of the browser headMinPanel. setBounds (browserWidth. intValue ()-40-40-40,-5, 40, 40 ); // minimize the image position on the top of the browser. *** the width and height of the browser are changed. * // set the distance from the browser to the header. webBrowserPanel. setBounds (0, 29, browserWidth. intValue (), browserHeight. intValue ()-29) ;}}) ;}}); // The downstream code must be commented out; otherwise, the form cannot be redirected. // NativeInterface. runEventPump ();} public static void main (String [] args) {String url = "http: // localhost: 8080/NativeSwitingTest/user/user_list.html "; string title = "test CS package Browser"; EagleBrowser1 eagleBrowser = new EagleBrowser1 (url, title );}}

 

 

 

The tool code used in the above Code to obtain the current computer size is as follows:

 

Package com. util; import java. awt. dimension; import java. awt. toolkit;/*** tool class for obtaining the resolution of the current computer * @ author admin **/public class ScreenSize {// get the width of the resolution of the current computer public static Double getScreenWidth () {Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize (); return screenSize. getWidth () ;}// get the height of the current computer's resolution public static Double getScreenHeight () {Dimension screenSize = Toolkit. getdefatooltoolkit (). getScreenSize (); return screenSize. getHeight ();}}

 

  

 

 

 

 

Related Article

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.