如何关闭Javafx? [英] How to shut down Javafx?

查看:130
本文介绍了如何关闭Javafx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与以下内容有关:如何阅读来自此页面的带有Java的HTML内容?

This question is related to : How to read html content from this page with Java?

我使用以下程序来获取网址的内容,但是如何在获取网址后将其关闭html内容,因为来自我的Netbeans,它似乎在获取内容后仍在运行:

I use the following program to get the content of a url, but how to shut it down after it gets the html content, because from my Netbeans, it seems it's still running after it gets the content :

import java.awt.*;
import java.io.*;
import java.net.URL;

import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javax.swing.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

/**
 Note using the browser might require setting the properties
 - http.proxyHost
 - http.proxyPort
 e.g. -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080
*/

public class Html_Loader extends JPanel
{
  private final int PANEL_WIDTH_INT=1130;
  private final int PANEL_HEIGHT_INT=840;
  private JFXPanel browserFxPanel;
  private Pane browser;
  WebView view;
  WebEngine eng;
  static String Url,Urls[]=new String[]{"https://www.iplocation.net/?query=63.149.124.133"};
//  static boolean Show_Frame_B=true;
  static boolean Show_Frame_B=false;
  StringBuffer Str_Buf;
  public Html_Loader() { init(); }

  public Html_Loader(String Url)
  {
    this.Url=Url;
    init();
    setURL(Url);
  }

  void init()
  {
    if (Show_Frame_B)
    {
      FlowLayout FL=new FlowLayout();
      FL.setHgap(0);
      FL.setVgap(0);
      setLayout(FL);
    }
    setPreferredSize(new Dimension(PANEL_WIDTH_INT,PANEL_HEIGHT_INT));
    browserFxPanel=new JFXPanel();
    add(browserFxPanel);
    Platform.runLater(new Runnable() { public void run() { createScene(); } });
  }

  public String getURL() { return eng.getLocation(); }

  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();
  }

  public void refresh() { Platform.runLater(new Runnable() { public void run() { eng.reload(); } }); }

  public void stop() { Platform.runLater(new Runnable() { public void run() { eng.getLoadWorker().cancel(); } }); }

  public void setURL(final String Url)
  {
    Platform.runLater(new Runnable()
    {
      public void run()
      {
        if (new File(Url).exists()) setURL(new File(Url));
        else eng.load((Url.startsWith("http://") || Url.startsWith("https://"))?Url:"http://"+Url);

//        browserFxPanel.setEnabled(false);
      }
    });
  }

  public void setURL(final URL Url)
  {
    Platform.runLater(new Runnable()
    {
      public void run()
      {
        try { eng.load(Url.toString()); }
        catch (Exception e) { e.printStackTrace(); }
      }
    });
  }

  public void setURL(final File file)
  {
    Platform.runLater(new Runnable()
    {
      public void run()
      {
        try { eng.load(file.toURI().toURL().toString()); }
        catch (Exception e) { e.printStackTrace(); }
      }
    });
  }

  private void createScene()
  {
    browser=createBrowser();
    browserFxPanel.setScene(new Scene(browser));
  }

  private Pane createBrowser()
  {
    view=new WebView();
    if (Show_Frame_B)
    {
      Double widthDouble=new Integer(PANEL_WIDTH_INT).doubleValue();
      Double heightDouble=new Integer(PANEL_HEIGHT_INT).doubleValue();
      view.setMinSize(widthDouble,heightDouble);
      view.setPrefSize(widthDouble,heightDouble);
    }
    eng=view.getEngine();
    eng.getLoadWorker()                                                        // addListener()
       .stateProperty()
       .addListener((obs,oldValue,newValue)->
    {
      System.out.println(newValue);
      if (newValue==State.SUCCEEDED)
      {
        Str_Buf=new StringBuffer();
//        System.out.println("finished loading");
        try
        {
          TransformerFactory transformerFactory=TransformerFactory.newInstance();
          Transformer transformer=transformerFactory.newTransformer();
          StringWriter stringWriter=new StringWriter();
          transformer.transform(new DOMSource(eng.getDocument()),new StreamResult(stringWriter));

          Str_Buf.append(stringWriter.getBuffer());
          Out(Str_Buf.toString());
        }
        catch (Exception e) { e.printStackTrace(); }
      }
    });
    GridPane grid=new GridPane();
    grid.getChildren().addAll(view);
    return grid;
  }

  public static void out(String message) { System.out.print(message); }   
  public static void Out(String message) { System.out.println(message); }

  public static void main(String[] args)
  {
    final Html_Loader demo=new Html_Loader(Urls[0]);

    if (Show_Frame_B)
    {
      SwingUtilities.invokeLater(new Runnable()
      {
        public void run()
        {
          JFrame frame=new JFrame("JavaFX 2.2 in Swing");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(demo);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
        }
      });
    }

  }
}

我不喜欢不需要它来显示一个面板,只需要用它来获取html内容并返回字符串,然后关闭,如何修复它?有没有办法将它简化为最小,只是用它来获取HTML?

I don't need it to show a panel, just need to use it to get the html content and return the string, then shut down, how to fix it ? Is there a way to simplify it to the minimum to just use it to get the html ?

推荐答案

好的,我找到了解决方案:

OK, I found the solution :

import java.awt.*;
import java.io.*;
import java.net.URL;
import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javax.swing.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

/**
 Note using the browser might require setting the properties
 - http.proxyHost
 - http.proxyPort
 e.g. -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080
*/

public class JavaFX_Html_Loader extends JPanel
{
  private final int PANEL_WIDTH_INT=1130;
  private final int PANEL_HEIGHT_INT=840;
  private JFXPanel browserFxPanel;
  private Pane browser;
  WebView view;
  WebEngine eng;
  static String Url,Urls[]=new String[]{"https://www.iplocation.net/?query=63.149.124.133"};
  static boolean Show_Frame_B=true;
//  static boolean Show_Frame_B=false;
  StringBuffer Str_Buf;

  public JavaFX_Html_Loader(String Url)
  {
    this.Url=Url;
    init();
    setURL(Url);
  }

  void init()
  {
    if (Show_Frame_B)
    {
      FlowLayout FL=new FlowLayout();
      FL.setHgap(0);
      FL.setVgap(0);
      setLayout(FL);
    }
    setPreferredSize(new Dimension(PANEL_WIDTH_INT,PANEL_HEIGHT_INT));
    browserFxPanel=new JFXPanel();
    add(browserFxPanel);
    Platform.runLater(new Runnable() { public void run() { createScene(); } });
  }

  public void setURL(final String Url)
  {
    Platform.runLater(new Runnable()
    {
      public void run()
      {
        if (new File(Url).exists()) setURL(new File(Url));
        else eng.load((Url.startsWith("http://") || Url.startsWith("https://"))?Url:"http://"+Url);
//        browserFxPanel.setEnabled(false);
      }
    });
  }

  public void setURL(final URL Url)
  {
    Platform.runLater(new Runnable()
    {
      public void run()
      {
        try { eng.load(Url.toString()); }
        catch (Exception e) { e.printStackTrace(); }
      }
    });
  }

  public void setURL(final File file)
  {
    Platform.runLater(new Runnable()
    {
      public void run()
      {
        try { eng.load(file.toURI().toURL().toString()); }
        catch (Exception e) { e.printStackTrace(); }
      }
    });
  }

  private void createScene()
  {
    browser=createBrowser();
    browserFxPanel.setScene(new Scene(browser));
  }

  private Pane createBrowser()
  {
    view=new WebView();
    if (Show_Frame_B)
    {
      Double widthDouble=new Integer(PANEL_WIDTH_INT).doubleValue();
      Double heightDouble=new Integer(PANEL_HEIGHT_INT).doubleValue();
      view.setMinSize(widthDouble,heightDouble);
      view.setPrefSize(widthDouble,heightDouble);
    }
    eng=view.getEngine();
    eng.getLoadWorker().stateProperty().addListener((obs,oldValue,newValue)->            // addListener()
    {
      System.out.println(newValue);
      if (newValue==State.SUCCEEDED)
      {
        Str_Buf=new StringBuffer();
//        System.out.println("finished loading");
        try
        {
          TransformerFactory transformerFactory=TransformerFactory.newInstance();
          Transformer transformer=transformerFactory.newTransformer();
          StringWriter stringWriter=new StringWriter();
          transformer.transform(new DOMSource(eng.getDocument()),new StreamResult(stringWriter));

          Str_Buf.append(stringWriter.getBuffer());
          Out(Str_Buf.toString());
          if (Str_Buf.toString().endsWith("</BODY></HTML>")) System.exit(0);
        }
        catch (Exception e) { e.printStackTrace(); }
      }
    });
    GridPane grid=new GridPane();
    grid.getChildren().addAll(view);
    return grid;
  }

  public static void out(String message) { System.out.print(message); }   
  public static void Out(String message) { System.out.println(message); }

  public static void main(String[] args)
  {
    final JavaFX_Html_Loader demo=new JavaFX_Html_Loader(Urls[0]);

    if (Show_Frame_B)
    {
      SwingUtilities.invokeLater(new Runnable()
      {
        public void run()
        {
          JFrame frame=new JFrame("JavaFX 2.2 in Swing");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(demo);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
        }
      });
    }

  }
}

这篇关于如何关闭Javafx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆