Java swing 做一个传统Web项目的桌面程序启动器(内嵌浏览器)

背景:公司有个老项目,web项目,但是使用者都想要一个桌面应用程序。实际上,是web程序的启动较为麻烦。这里每次都需要启动Tomcat和浏览器。

想法:重写一个项目太麻烦,想想成本,人间不值得。于是我想着简化一下整个流程。

要求:界面要美,主要是流畅,看不出来是网页的效果。加载完美。

过程:启动程序时自动打开tomcat+内嵌浏览器自动全屏并加载页面+关闭时自动关闭tomcat(需要手动修改配置把tomcat后台隐藏)

完善:

1.启动和关闭tomcat通过管理员命令来实现

2.通过对tomcat配置隐藏掉tomcat的后台

3.为了更为便捷,增加了设置文件;分别是启动bat脚本、关闭bat脚本、内嵌浏览器启动时加载的主页+程序的标题名称

页面效果大概如下:

首先是后台加载页面(其实就是在等待系统执行打开web服务器的脚本)

贴一下代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class DoRun extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private final int DELAY = 50;// 转动快慢设置
//	private final static Long time = (long) 5000;	//窗体关闭事件
	private static Timer timer;	//动画计时器
	private int x = 0;
	/**
	 * 调用
	 */

	public void start(boolean flag) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//本类为Panel
		frame.add(new DoRun());
		frame.setSize(300, 300);
		frame.setLocation(400, 400);
	
		// 设置窗体居中显示
		frame.setLocationRelativeTo(frame.getOwner());
		
		//窗体定时关闭
		/*try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
		}
		// 停止 Timer,使它停止向其侦听器发送动作事件。
		timer.stop();
		frame.setVisible(false);
		frame.dispose();*/
		
	}
	
	/**
	 * 面板构造函数,初始化面板。包括Timer 的场景。
	 */
	public DoRun() {
		timer = new Timer(DELAY, new ReboundListener());
		timer.start();
	}

	/**
	 * 动画效果:不断的更新图像的位置,以达到动画的效果。
	 */
	private class ReboundListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (x < 360) {
				//控制每个DELAY周期旋转的角度,+ 为逆时针  - 为顺时针
				x = x - 5;
			} else {
				x = 0;
			}
			repaint();
		}
	}

	/**
	 * 绘出图像在面板中的位置
	 */
	public void paintComponent(Graphics page) {
		super.paintComponent(page);
		drawArc(page);
	}
	
	/**
	 * 画图形
	 */
	private void drawArc(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		//抗锯齿 
		//JDK文档:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/awt/RenderingHints.html
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int width = getWidth();
		int height = getHeight();
		//设置画笔颜色
		g2d.setColor(Color.BLACK);
		g2d.drawArc(width / 2 - 110, height / 2 - 110, 10 + 200, 10 + 200, 0, 360);
		g2d.setColor(Color.GREEN);
		g2d.fillArc(width / 2 - 110, height / 2 - 110, 10 + 200, 10 + 200, x, 240);
		g2d.setColor(Color.BLACK);
		g2d.fillArc(width / 2 - 90, height / 2 - 90, 10 + 160, 10 + 160, 0, 360);
		g2d.dispose();
	}
}
package ceims;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class FileRead {
    /**
     * 读取txt文件的内容
     * @param file 想要读取的文件对象
     * @return 返回文件内容
     */
    public String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
    
//    public static void main(String[] args){
//        File file = new File("C:/3.txt");
//        System.out.println(txt2String(file));
//    }
}
package ceims;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.RoundRectangle2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.*;


import com.sun.awt.AWTUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

/**
 * swing实现简单的浏览器窗口
 * 
 * @author WangSong
 *
 */
public class SimpleBrowser extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 引入窗口、jweb
	private JPanel webBrowserPanel;
	private JWebBrowser webBrowser;

	// 构造器传入参数
	public SimpleBrowser(String url) {
		super(new BorderLayout());
		webBrowserPanel = new JPanel(new BorderLayout());
		webBrowser = new JWebBrowser();
		webBrowser.navigate(url);// 默认打开的网址
		webBrowser.setButtonBarVisible(true);// 按钮组
		webBrowser.setMenuBarVisible(true);// 菜单
		webBrowser.setBarsVisible(true);// 组件可见
		webBrowser.setStatusBarVisible(true);// 组件可用
		webBrowserPanel.add(webBrowser, BorderLayout.CENTER);// 添加浏览器、居中显示
		add(webBrowserPanel, BorderLayout.CENTER);// Container中添加Panel、居中显示
		// 执行Js代码
//        webBrowser.executeJavascript("alert('浏览器打开了....')");

	}

	/**
	 * 在swing里内嵌浏览器
	 * 
	 * @param url   要访问的url
	 * @param title 窗体的标题
	 */
	public static void openForm(String url, String title) {
		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		// 新的线程运行浏览器效果
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame(title);
				// 设置窗体关闭的时候不关闭应用程序
				frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				frame.getContentPane().add(new SimpleBrowser(url), BorderLayout.CENTER);
				frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
				frame.setLocationByPlatform(true);

				// 重置窗体大小
				frame.setResizable(true);
				Toolkit kit = Toolkit.getDefaultToolkit();
		        Dimension dimension = kit.getScreenSize();
		        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		        frame.setBounds(0, 0, dimension.width, dimension.height);
		        frame.setUndecorated(true);
				// 让窗体可见
				frame.setVisible(true);
				// 设置窗体的宽度、高度
				//frame.setSize(1400, 700);
				// 设置窗体居中显示
				frame.setLocationRelativeTo(frame.getOwner());
				frame.getGraphicsConfiguration().getDevice() 
                .setFullScreenWindow(frame);
				frame.addWindowListener(new WindowAdapter() {

					public void windowClosing(WindowEvent e) {
						try {
							Runtime.getRuntime().exec("c:\\close.bat");
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						System.exit(0);
					}

				});
			}
		});
		NativeInterface.runEventPump();
	}

	// 主程序入口
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//本类为Panel
		frame.add(new DoRun());

		frame.setSize(300, 300);
		frame.setLocation(400, 400);
		frame.setUndecorated(true);
		/** 设置圆角 */  
        AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(  
            0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 40.0D,  
            40.0D));
		frame.setVisible(true);
		frame.setOpacity(0.7f);
		// 设置窗体居中显示
		frame.setLocationRelativeTo(frame.getOwner());
		try {				
			// 运行bat文件
			Process process = Runtime.getRuntime().exec("c:\\start.bat");
			InputStream in = process.getInputStream();
			String line;
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			if ((line = br.readLine()) != null) {
				System.out.println(line);
				frame.setVisible(false);
				frame.dispose();
				FileRead fr =new FileRead();
				String url = fr.txt2String(new File("C:\\url.txt"));
				String title = fr.txt2String(new File("C:\\title.txt"));
				openForm(url,title);
			}
			in.close();
			// process.waitFor();
			System.out.println("执行成功");
		} catch (Exception e) {
			System.out.println("执行失败");
			frame.setVisible(false);
			frame.dispose();
			
			JOptionPane.showMessageDialog(null, "初始化失败!", "", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}

	}
}

有问题的或者相关思路可以交流一下

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值