Swing使用JavaFXweb組件


概述

swing中內嵌入web組件的 需要使用一些其他的jar包 ,但是如果使用javafx的組件,那么也比較的方便,性能也比較高.

代碼

  • webview 在javafx 中是作為 scene出現的所以不需要單獨設置部件類型.

  • 下面是單獨的地址處理方法

private static void gotoURL(String url) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webView.getEngine().load(url);
			}
		});
	}

  • swing嵌入fx 一般的寫法
    這里注意 webview 最好是 靜態化
	Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webView = new WebView();
				jFXPanel.setScene(new Scene(webView));
				webView.getEngine().load("http://www.baidu.com");
			}
		});
  • 剩下的就是布局處理 你喜歡就好 , 這里我選擇的了一個splash,出場動畫, 可要可不要.

public class SwingFinal {

	static WebView webView = null;

	private static void gotoURL(String url) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webView.getEngine().load(url);
			}
		});
	}

	/**
	 * @param args
	 *            the command line arguments
	 * @throws URISyntaxException
	 */
	public static void main(String[] args) throws MalformedURLException, URISyntaxException {
		// TODO code application logic here
		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		frame.setSize(800, 600);
		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		JFXPanel jFXPanel = new JFXPanel();
		frame.add(jFXPanel, "Center");

		JPanel controlPanel = new JPanel();
		frame.add(controlPanel, "North");
		JTextField urlField = new JTextField();
		JButton goButton = new JButton("GO");
		///////////////////////////////////////////////////////////////////////////////////
		urlField.setText("http://www.baidu.com");

		controlPanel.setLayout(new BorderLayout());
		urlField.setPreferredSize(new Dimension(frame.getWidth() - 100, 1));
		controlPanel.add(urlField, BorderLayout.WEST);
		controlPanel.add(goButton, BorderLayout.EAST);

		controlPanel.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				controlPanel.setLayout(new BorderLayout());
				urlField.setPreferredSize(new Dimension(frame.getWidth() - 100, 1));
				controlPanel.add(urlField, BorderLayout.WEST);
				controlPanel.add(goButton, BorderLayout.EAST);
			}

		});
		frame.addWindowStateListener(new WindowStateListener() {
			@Override
			public void windowStateChanged(WindowEvent e) {
				controlPanel.setLayout(new BorderLayout());
				urlField.setPreferredSize(new Dimension(frame.getWidth() - 100, 1));
				controlPanel.add(urlField, BorderLayout.WEST);
				controlPanel.add(goButton, BorderLayout.EAST);
			}
		});
		goButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String urlString = urlField.getText();
				gotoURL(urlString);
			}

		});
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webView = new WebView();
				jFXPanel.setScene(new Scene(webView));
				webView.getEngine().load("http://www.baidu.com");
			}
		});

		JWindow splashWindow = new JWindow();
		splashWindow.setSize(1024, 768);
		splashWindow.setLocationRelativeTo(null);
		splashWindow.setLayout(new BorderLayout());
		File file = new File(SwingFinal.class.getResource("fox.png").toURI());
		ImageIcon icon = new ImageIcon(file.toURL());
		JLabel label = new JLabel(icon);
		splashWindow.add(label);
		Thread t = new Thread() {
			public void run() {
				frame.setVisible(false);
				splashWindow.setVisible(true);
				try {
					Thread.sleep(5000);
				} catch (InterruptedException ex) {
					Logger.getLogger(SwingFinal.class.getName()).log(Level.SEVERE, null, ex);
				}
				splashWindow.setVisible(false);
				frame.setVisible(true);

			}
		};
		t.setDaemon(true);
		t.start();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(null, "????", "??�?", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}

		});

	
	}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM