gpt4 book ai didi

java - 简单的 JavaFX 浏览器仅在第一次打开时有效

转载 作者:行者123 更新时间:2023-11-30 06:39:51 25 4
gpt4 key购买 nike

我有这个代码来打开自定义 Java 浏览器:

private void openNavigator(){

Navigator browser = new Navigator();
SwingUtilities.invokeLater(() -> {

browser.initComponents();

browser.setVisible(true);

browser.loadURL("http://XXXXXXXX:8888/YYYYYY/ZZZZ");
});
}

另一方面,我有导航器的代码:

public class Navigator extends JFrame {

private static final Logger LOG = Logger.getLogger(Navegador.class.getName());
private static final long serialVersionUID = -1951385676682823399L;

private WebView view;
private JFXPanel javaFxPanel;
private WebEngine engine;

private JLabel labelStatus;
private JTextField direction;
private JProgressBar progressBar;

private java.net.CookieManager cookiesManager;


public void initComponents() {

if (cookiesManager != null) {
cookiesManager = new java.net.CookieManager();
java.net.CookieHandler.setDefault(cookiesManager);
}

javaFxPanel = new JFXPanel();
labelStatus = new JLabel();

JPanel panelTodo = new JPanel(new BorderLayout());
JButton botonBuscar = new JButton("Search");
direction = new JTextField();
progressBar = new JProgressBar();

createScene();

ActionListener direcctionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadURL(direction.getText());
}
};

botonBuscar.addActionListener(direcctionListener);
direction.addActionListener(direcctionListener);

progressBar.setPreferredSize(new Dimension(150, 18));
progressBar.setStringPainted(true);

JPanel topBar = new JPanel(new BorderLayout(5, 0));
topBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
topBar.add(direction, BorderLayout.CENTER);
topBar.add(botonBuscar, BorderLayout.EAST);

JPanel statusBar = new JPanel(new BorderLayout(5, 0));
statusBar.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
statusBar.add(labelStatus, BorderLayout.CENTER);
statusBar.add(progressBar, BorderLayout.EAST);

panelTodo.add(topBar, BorderLayout.NORTH);
panelTodo.add(javaFxPanel, BorderLayout.CENTER);
panelTodo.add(statusBar, BorderLayout.SOUTH);

getContentPane().add(panelTodo);

setPreferredSize(new Dimension(1024, 600));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
}

private void createScene() {

Platform.runLater(() -> {
view = new WebView();
engine = view.getEngine();

engine.setOnAlert((WebEvent<String> wEvent) -> {
System.out.println("JS alert() message: " + wEvent.getData());
});

engine.titleProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Navegador.this.setTitle(newValue);
}
});
}
});

engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {
@Override
public void handle(final WebEvent<String> event) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
labelStatus.setText(event.getData());
}
});
}
});

engine.locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String viejoValor, final String nuevoValor) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
direction.setText(nuevoValor);
}
});
}
});

engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressBar.setValue(newValue.intValue());
}
});
}
});

engine.getLoadWorker().exceptionProperty()
.addListener(new ChangeListener<Throwable>() {

@Override
public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) {
if (engine.getLoadWorker().getState() == FAILED) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//TODO Handling Exception
}
});
}
}
});

javaFxPanel.setScene(new Scene(view));
});
}

public void loadURL(String url) {
Platform.runLater(new Runnable() {
@Override
public void run() {
String urlTemporal = toURL(url);

if (urlTemporal == null) {
urlTemporal = toURL("http://" + url);
}

engine.load(urlTemporal);
}
});
}

private static String toURL(String str) {
try {
return new URL(str).toExternalForm();
} catch (MalformedURLException exception) {
return null;
}
}

}

我的问题是,当我第一次打开导航器时它可以工作,但是当我关闭它然后再次打开时它只显示 TextFieldButton

注释:

  1. 我使用 javaswing 按钮打开导航器openNavigator() 方法
  2. 当我离开一个导航器而没有关闭时,同时我打开一个或多个导航器,它运行良好

我第一次打开导航器:

enter image description here

我第二次打开导航器:

enter image description here

最佳答案

JavaFX 线程/工具包必须正在运行。显然,创建一个 JFXPanel 足以初始化它,但一旦关闭,线程就会自动终止。
阻止它自动关闭通话

Platform.setImplicitExit(false); 

因此,JavaFX 工具包仅在调用 Platform#exit 方法或整个应用程序终止时关闭。

关于java - 简单的 JavaFX 浏览器仅在第一次打开时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563447/

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