Netbeans中的嵌入式浏览器在Swing应用程序中使用javaFX WebView

时间:2017-09-25 21:48:28

标签: java swing javafx netbeans

我试图在我的java Netbeans项目中实现嵌入式浏览器到目前为止这么好但是浏览器还不够复杂,不能存储cookie或任何高级功能我的目标基本上是能够访问谷歌帐户并检查电子邮件或日历或任何类似的操作..有人可以指导我正确的代码吗?提前致谢。 这是我的代码:

import com.sun.javafx.application.PlatformImpl;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeListener;

/** 
* SwingFXWebView 
 */  
public class SwingFXWebView extends JPanel {  

    private Stage stage;  
    private WebView browser;  
    private JFXPanel jfxPanel;  
    private JButton swingButton;  
    private WebEngine webEngine;  

    public SwingFXWebView(){  
        initComponents();  
    }  

    public static void main(String ...args){  
        // Run this later:
        SwingUtilities.invokeLater(new Runnable() {  
            @Override
            public void run() {  
                final JFrame frame = new JFrame();  

                frame.getContentPane().add(new SwingFXWebView());  

                frame.setMinimumSize(new Dimension(640, 480));  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                frame.setVisible(true);  
            }  
        });     
    }  

    private void initComponents(){  

        jfxPanel = new JFXPanel();  
        createScene();  

        setLayout(new BorderLayout());  
        add(jfxPanel, BorderLayout.CENTER);  

        swingButton = new JButton();  
        swingButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        webEngine.reload();
                    }
                });
            }
        });  
        swingButton.setText("Reload");  

        add(swingButton, BorderLayout.SOUTH);  
    }     

    /** 
     * createScene 
     * 
     * Note: Key is that Scene needs to be created and run on "FX user thread" 
     *       NOT on the AWT-EventQueue Thread 
     * 
     */  
    private void createScene() {  
        PlatformImpl.startup(new Runnable() {  
            @Override
            public void run() {  

                stage = new Stage();  

                stage.setTitle("Hello Java FX");  
                stage.setResizable(true);  

                Group root = new Group();  
                Scene scene = new Scene(root,80,20);  
                stage.setScene(scene);  

                // Set up the embedded browser:
                browser = new WebView();
                webEngine = browser.getEngine();
                webEngine.load("https://www.google.com");


                //https://calendar.google.com
                ObservableList<Node> children = root.getChildren();
                children.add(browser);                     

                jfxPanel.setScene(scene);  
            }  
        });  
    }
}

0 个答案:

没有答案