gpt4 book ai didi

java - JFXTextField 不适用于 JxBrowser

转载 作者:行者123 更新时间:2023-11-30 10:33:05 29 4
gpt4 key购买 nike

我有点坚持这一点。我为我的 JavaFX 项目使用 JxBrowser。我还使用 JFoenix 来插入外观。现在,当我浏览某些站点时,作为 JFXTextField 的 URLBar 变得不可用。我不能再写了。但默认的 JavaFX 上下文菜单仍然有效。有趣的是,网站上的关键听众,例如 youtube 上的全屏 F 仍然有效。要解决此问题,唯一可行的方法是在 PC 上单击一个完全不同的窗口。例如,我点击 IntelliJ 而不是回到 JavaFX 框架,而不是它再次工作。正如我只在特定网站上写的那样。不抛出异常。

如果它可能很重要,我使用 2 个 TabPanes,一个用于选项卡,另一个用于内容。

有人有想法吗?

生成标签的类:

package de.liz3.liz3web.browser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import de.liz3.liz3web.gui.controller.MainController;
import javafx.application.Platform;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.KeyCode;

import java.util.Vector;

/**
* Created by Liz3 on 22.02.2017.
*/
public class TabManager {

private Vector<BrowserTab> activeTabs;
private MainController controller;

public TabManager(MainController controller) {
this.controller = controller;

activeTabs = new Vector<>();

controller.getUrlField().setOnMouseClicked(event -> controller.getUrlField().requestFocus());
TabPane head = controller.getHeaderTabPane();
controller.getUrlField().setOnKeyReleased(event -> {

if (event.getCode() == KeyCode.ENTER) {

selectedBrowserTab().browseOrSearch(TabManager.this.controller.getUrlField().getText());
}
});
controller.getBackBtn().setOnAction(event -> selectedBrowserTab().getEngine().goBack());
controller.getForwardBtn().setOnAction(event -> selectedBrowserTab().getEngine().goForward());
head.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

if (newValue == controller.getNewTab()) {
newTab(null);
return;
}
controller.getMainTabPane().getSelectionModel().select(getContentTab(newValue));

controller.getUrlField().setText(selectedBrowserTab().getCurrentUrl());
});
}

public void newTab(String url) {


new Thread(() -> {

Browser browser = new Browser();
BrowserView view = new BrowserView(browser);

Tab headerTab = new Tab("New Tab");
Tab contentTab = new Tab();
contentTab.setContent(view);
BrowserTab t = new BrowserTab(browser, view, contentTab, headerTab);
activeTabs.add(t);


Platform.runLater(() -> {

controller.getHeaderTabPane().getTabs().add(headerTab);
controller.getMainTabPane().getTabs().add(contentTab);
controller.getHeaderTabPane().getTabs().remove(controller.getNewTab());
controller.getHeaderTabPane().getTabs().add(controller.getNewTab());
controller.getMainTabPane().getSelectionModel().select(contentTab);
controller.getHeaderTabPane().getSelectionModel().select(headerTab);
controller.getUrlField().setEditable(true);
controller.getUrlField().setText("");
t.browseOrSearch(url);

});
}).start();

}


public BrowserTab selectedBrowserTab() {

for (BrowserTab tab : activeTabs) {

if (tab.getHeaderTab() == controller.getHeaderTabPane().getSelectionModel().getSelectedItem()) {
return tab;
}
}
return null;
}

public Tab getSelectedHeaderTab() {

return controller.getHeaderTabPane().getSelectionModel().getSelectedItem();
}

public Tab getSelectedContentTab() {

return controller.getMainTabPane().getSelectionModel().getSelectedItem();
}

public Tab getHeaderTab(Tab contentTab) {

for (BrowserTab tab : activeTabs) {

if (tab.getContentTab() == contentTab) {
return tab.getHeaderTab();
}
}
return null;
}

public Tab getContentTab(Tab headerTab) {

for (BrowserTab tab : activeTabs) {

if (tab.getHeaderTab() == headerTab) {
return tab.getContentTab();
}
}
return null;
}

public MainController getController() {
return controller;
}

public Vector<BrowserTab> getActiveTabs() {
return activeTabs;
}
}

浏览器类:

package de.liz3.liz3web.browser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.events.*;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import de.liz3.liz3web.Main;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Tab;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
* Created by Liz3 on 21.02.2017.
*/
public class BrowserTab {

private Browser engine;
private BrowserView view;
private Tab contentTab;
private Tab headerTab;
private String currentUrl;



public BrowserTab(Browser engine, BrowserView view, Tab contentTab, Tab headerTab) {
this.engine = engine;
this.view = view;
this.contentTab = contentTab;
this.headerTab = headerTab;
this.currentUrl = "";

setUpTab();

}

public void browseOrSearch(String address) {

if (address == null) {
return;
}

if (address.startsWith("http://") || address.startsWith("https://")) {

this.engine.loadURL(address);

return;
}
if (!address.contains(" ") && address.contains(".")) {

address = "http://" + address;
this.engine.loadURL(address);
return;
}

String encode = null;
try {
encode = URLEncoder.encode(address, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

this.engine.loadURL("https//google.com/search?q=" + encode);

}

private void setUpTab() {

this.headerTab.setClosable(true);
this.headerTab.setOnCloseRequest(new EventHandler<Event>() {
@Override
public void handle(Event event) {

Main.tm.getController().getMainTabPane().getTabs().remove(contentTab);
Main.tm.getActiveTabs().remove(this);

}
});
this.engine.addTitleListener(titleEvent -> Platform.runLater(() -> BrowserTab.this.headerTab.setText(titleEvent.getTitle())));
this.engine.addLoadListener(new LoadListener() {
@Override
public void onStartLoadingFrame(StartLoadingEvent startLoadingEvent) {

Platform.runLater(() -> {
BrowserTab.this.currentUrl = BrowserTab.this.engine.getURL();
if (Main.tm.getSelectedHeaderTab() == BrowserTab.this.headerTab) {

Main.tm.getController().getUrlField().setText(BrowserTab.this.currentUrl);
}
});

}

@Override
public void onProvisionalLoadingFrame(ProvisionalLoadingEvent provisionalLoadingEvent) {

}

@Override
public void onFinishLoadingFrame(FinishLoadingEvent finishLoadingEvent) {
Platform.runLater(() -> {

BrowserTab.this.currentUrl = BrowserTab.this.engine.getURL();
if (Main.tm.getSelectedHeaderTab() == BrowserTab.this.headerTab) {

Main.tm.getController().getUrlField().setText(BrowserTab.this.currentUrl);
}
});
}

@Override
public void onFailLoadingFrame(FailLoadingEvent failLoadingEvent) {

}

@Override
public void onDocumentLoadedInFrame(FrameLoadEvent frameLoadEvent) {

}

@Override
public void onDocumentLoadedInMainFrame(LoadEvent loadEvent) {

}
});
this.engine.setPopupHandler(popupParams -> {
Platform.runLater(() -> Main.tm.newTab(popupParams.getURL()));

return null;
});

}

public Browser getEngine() {
return engine;
}

public BrowserView getView() {
return view;
}

public Tab getContentTab() {
return contentTab;
}

public Tab getHeaderTab() {
return headerTab;
}

public String getCurrentUrl() {
return currentUrl;
}
}

最佳答案

默认情况下,JxBrowser 在 HEAVYWEIGHT 中运行渲染模式。不建议在像您这样带有选项卡和图层的应用程序中使用 HEAVYWEIGHT 渲染模式。请使用LIGHTWEIGHT渲染模式。它应该可以正常工作:

Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
BrowserView view = new BrowserView(browser);

关于java - JFXTextField 不适用于 JxBrowser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381369/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com