首页 > 代码库 > Swing界面嵌套浏览器

Swing界面嵌套浏览器

package com.*;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserCommandEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserListener;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowOpeningEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserWindowWillOpenEvent;

/**
 * 主窗体
 * 
 * @author dxm
 * 
 */
public class MainFrame extends JFrame {

	private static final long serialVersionUID = -6348199501339114346L;

	// 最后操作时间
	public long lastTime = new Date().getTime();

	// 浏览器窗体
	private JWebBrowser webBrowser = null;
	private JLabel lbl = null;
	private JPanel panel = null;

	private int type = 2;

	/**
	 * 构造器
	 */
	public MainFrame() {
		initData();
		initGUI();
		addListance();
	}

	private void initData() {

		Toolkit toolkit = Toolkit.getDefaultToolkit();

		this.setIconImage(new ImageIcon(System.getProperty("user.dir") + "\\5.jpg").getImage());
		this.setUndecorated(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(600, 500);
		this.setLocationRelativeTo(null);
		this.setSize(toolkit.getScreenSize());
		this.setLocation(0, 0);

		webBrowser = new JWebBrowser();
		webBrowser.navigate(Config.url);
		webBrowser.setBarsVisible(false);
		webBrowser.setMenuBarVisible(false);
		webBrowser.setLocationBarVisible(false);
		webBrowser.setButtonBarVisible(false);
		webBrowser.setStatusBarVisible(false);

		ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\5.jpg");
		File img = new File(System.getProperty("user.dir") + "\\" + Config.img);
		if (img.exists()) {
			icon = new ImageIcon(img.getAbsolutePath());
		}
		lbl = new JLabel(icon);

		panel = new JPanel(new BorderLayout());
	}

	private void initGUI() {
		panel.add(webBrowser, BorderLayout.CENTER);
		this.setContentPane(panel);
	}

	private void addListance() {

		webBrowser.addWebBrowserListener(new WebBrowserListener() {

			@Override
			public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void windowClosing(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void titleChanged(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void statusChanged(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void locationChanging(WebBrowserNavigationEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void locationChanged(WebBrowserNavigationEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void loadingProgressChanged(WebBrowserEvent arg0) {
				lastTime = new Date().getTime();
			}

			@Override
			public void commandReceived(WebBrowserCommandEvent arg0) {
				lastTime = new Date().getTime();
			}
		});

		lbl.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				change(2);
			}
		});

	}

	/**
	 * 屏保切换
	 */
	public void change(final int type) {
		lastTime = new Date().getTime();

		if (type == this.type) {
			return;
		}
		this.type = type;

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				if (type == 1) {
					webBrowser.setVisible(false);
					panel.add(lbl, BorderLayout.CENTER);
				} else {
					panel.remove(lbl);
					webBrowser.setVisible(true);
				}
				panel.validate();
			}
		});
	}

	public static void main(String[] args) {
		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainFrame frame = new MainFrame();
				frame.setVisible(true);
				new ChangeWait(frame).tryStart();
			}
		});
		NativeInterface.runEventPump();
	}

}
jar包下载地址 http://download.csdn.net/detail/daixinmei/7827723


Swing界面嵌套浏览器