Button 如何使用WebView和WebEngine在JavaFX中编程后退和前进按钮?

Button 如何使用WebView和WebEngine在JavaFX中编程后退和前进按钮?,button,javafx,back,forward,Button,Javafx,Back,Forward,我正在学习JavaFX,试图编写一个简单的浏览器,但是如何使用WebView和WebEngine在JavaFX中编程后退和前进按钮呢?有任何示例代码吗?我找到了: public String goBack() { final WebHistory history=eng.getHistory(); ObservableList<WebHistory.Entry> entryList=history.getEntries(); int curre

我正在学习JavaFX,试图编写一个简单的浏览器,但是如何使用WebView和WebEngine在JavaFX中编程后退和前进按钮呢?有任何示例代码吗?

我找到了:

  public String goBack()
  {    
    final WebHistory history=eng.getHistory();
    ObservableList<WebHistory.Entry> entryList=history.getEntries();
    int currentIndex=history.getCurrentIndex();
//    Out("currentIndex = "+currentIndex);
//    Out(entryList.toString().replace("],","]\n"));

    Platform.runLater(new Runnable() { public void run() { history.go(-1); } });
    return entryList.get(currentIndex>0?currentIndex-1:currentIndex).getUrl();
  }

  public String goForward()
  {    
    final WebHistory history=eng.getHistory();
    ObservableList<WebHistory.Entry> entryList=history.getEntries();
    int currentIndex=history.getCurrentIndex();
//    Out("currentIndex = "+currentIndex);
//    Out(entryList.toString().replace("],","]\n"));

    Platform.runLater(new Runnable() { public void run() { history.go(1); } });
    return entryList.get(currentIndex<entryList.size()-1?currentIndex+1:currentIndex).getUrl();
  }
公共字符串goBack()
{    
最终WebHistory history=eng.getHistory();
ObservableList entryList=history.getEntries();
int currentIndex=history.getCurrentIndex();
//输出(“currentIndex=“+currentIndex”);
//Out(entryList.toString().replace(“],”,“]\n”);
Platform.runLater(new Runnable(){public void run(){history.go(-1);}});
返回entryList.get(currentIndex>0?currentIndex-1:currentIndex).getUrl();
}
公共字符串goForward()
{    
最终WebHistory history=eng.getHistory();
ObservableList entryList=history.getEntries();
int currentIndex=history.getCurrentIndex();
//输出(“currentIndex=“+currentIndex”);
//Out(entryList.toString().replace(“],”,“]\n”);
Platform.runLater(new Runnable(){public void run(){history.go(1);}});
return entryList.get(currentIndex如何对按钮进行编码的一个很好的例子是“我找到了”下面的代码,除了如果按原样运行,它会抛出越界异常。例如,如果用户在WebEngine浏览器首次加载时单击“后退”,则会发生这种情况。在这种情况下,entryList的长度为1,并调用history.goBack(-1)尝试在当前位置减去1(即0-1)访问entryList,这超出了范围。调用历史记录也存在类似的情况。当currentIndex已经是entryList的末尾时,goForward使用(1),在这种情况下,调用尝试在超出其长度的索引处访问列表,再次超出范围

下面的简单示例代码处理条目列表在任何时间点的边界:

public void goBack()
{ 
  final WebHistory history = webEngine.getHistory();
  ObservableList<WebHistory.Entry> entryList = history.getEntries();
  int currentIndex = history.getCurrentIndex();

  Platform.runLater(() -> 
  {
    history.go(entryList.size() > 1 
      && currentIndex > 0
            ? -1
            : 0); 
  });        
}

public void goForward()
{
  final WebHistory history = webEngine.getHistory();   
  ObservableList<WebHistory.Entry> entryList = history.getEntries();
  int currentIndex = history.getCurrentIndex();

  Platform.runLater(() -> 
  {
    history.go(entryList.size() > 1
      && currentIndex < entryList.size() - 1
                    ? 1
                    : 0); 
  });    
}
public void goBack()
{ 
最终WebHistory history=webEngine.getHistory();
ObservableList entryList=history.getEntries();
int currentIndex=history.getCurrentIndex();
Platform.runLater(()->
{
history.go(entryList.size()>1
&&当前索引>0
? -1
: 0); 
});        
}
公开发行
{
最终WebHistory history=webEngine.getHistory();
ObservableList entryList=history.getEntries();
int currentIndex=history.getCurrentIndex();
Platform.runLater(()->
{
history.go(entryList.size()>1
&¤tIndex
如果您不需要获取或设置任何索引,下面是一种使用javascript编写自定义上下文菜单的后退和前进按钮的简明方法:

public void goBack() {
    Platform.runLater(() -> {
        webEngine.executeScript("history.back()");
    });
}

public void goForward() {
    Platform.runLater(() -> {
        webEngine.executeScript("history.forward()");
    });
}