JavaFX WebView扩展到整个区域

时间:2015-07-22 21:28:50

标签: java swing javafx

我试图制作一个扩展到周围JPanel的javafx WebView。根据这个帖子:http://www.coderanch.com/t/634791/JavaFX/java/Resizing-HTMLEditor-JavaFX,WebView中存在一个错误,您需要在WebView上调用 awk '(($3 == "fgfgfg") || ($4 == "fgfgfg")) && ($2 ~ /^fg[0-9]{5}/)' file_name GridPane.setHgrow。我已经用这些调用填充了我的代码,但WebView仍然是600x800px。jxframe sadness

请注意,场景具有蓝色背景,因此您可以看到javafx内容确实填充了整个JPanel。但是WebView没有填充javafx组/场景。

GridPane.setVgrow

2 个答案:

答案 0 :(得分:11)

一种方法是将WebView添加到StackPane,其中“将尝试调整每个孩子的大小以填充其内容区域”。我已将JFXPanel括起640 x 480StackPane大小的WebView;调整框架大小以查看Pos.CENTER如何根据默认import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javafx.application.Platform; import javafx.embed.swing.JFXPanel; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; /** @see https://stackoverflow.com/a/31576647/230513 */ public class WebViewTest { private void initAndShowGUI() { // This method is invoked on the EDT thread JFrame frame = new JFrame("Swing and JavaFX"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JFXPanel fxPanel = new JFXPanel(){ @Override public Dimension getPreferredSize() { return new Dimension(640, 480); } }; frame.add(fxPanel, BorderLayout.CENTER); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); Platform.runLater(() -> { initFX(fxPanel); }); } private void initFX(JFXPanel fxPanel) { // This method is invoked on the JavaFX thread Scene scene = createScene(); fxPanel.setScene(scene); } private Scene createScene() { StackPane root = new StackPane(); Scene scene = new Scene(root); WebView webView = new WebView(); WebEngine webEngine = webView.getEngine(); webEngine.load("http://www.example.com"); root.getChildren().add(webView); return scene; } public static void main(String[] args) { SwingUtilities.invokeLater(new WebViewTest()::initAndShowGUI); } } 重排left join内容。

preferred

select cm.*,
       (select ct.image_one
        from car_type ct
        where ct.car_id = cm.id
        order by ct.id
        limit 1
       ) as image_one
from car_make cm;

答案 1 :(得分:1)

实际上,我认为我的解决方案基于http://docs.oracle.com/javafx/2/webview/WebViewSample.java.htm

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class WebViewSample {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Swing and JavaFX");
        frame.setSize(1000, 1000);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout(0, 0));
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        final JFXPanel jfxPanel = new JFXPanel();
        panel.add(jfxPanel);

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFx(jfxPanel);
            }
       });
    }

    private static void initFx(JFXPanel fxPanel) {
        Stage stage = new Stage();
        stage.setTitle("Web View");
        Scene scene = new Scene(new Browser(), 1000, 1000, Color.web("#666970"));
        stage.setScene(scene);
        fxPanel.setScene(scene);
    }

}

class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        // load the home page
        webEngine.load("http://www.google.com");

        //add components
        getChildren().add(browser);
    }

    @Override
    protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(browser,0,0,w,h,0,HPos.CENTER,VPos.CENTER);
    }

}

我认为诀窍是子类化Region,以便您可以手动将WebView布局在哪里。