java之迷你浏览器
迷你浏览器预览视图:
代码如下:
package mini_browser;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmallBrowser extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel $jpanel;
SWTPane $SWTPane;
//创建弹出菜单
PopupMenu popup=new PopupMenu();
//创建菜单
Menu jm=new Menu("弹出消息");
//创建菜单项数组
MenuItem[] item ={new MenuItem("警告消息"),new MenuItem("信息消息"),
new MenuItem("错误消息"),new MenuItem("退出程序"),
new MenuItem("显示窗口"),new MenuItem("重新访问")};
//定义SystemTray成员变量
SystemTray tray;
//定义TrayIcon成员变量
TrayIcon trayIcon;
public SmallBrowser()
{
$jpanel = new JPanel();
$SWTPane = new SWTPane();
//$jpanel.add($SWTPane);
//循环对菜单项进行处理
for(int i=0;i<item.length;i++)
{
//为菜单乡项注册监听器
item[i].addActionListener(this);
//将菜单项数组中前3个菜单项添加进"弹出消息"菜单中
//if(i<3) jm.add(item[i]);
}
//将弹出消息菜单与退出程序菜单项添加进弹出菜单
//popup.add(jm);
popup.add(item[5]);
popup.add(item[4]);
popup.add(item[3]);
// 判断当前操作系统是否支持系统托盘
if (SystemTray.isSupported())
{
//通过静态方法getSystemTray()得到系统托盘
tray = SystemTray.getSystemTray();
//加载图象
Image image = Toolkit.getDefaultToolkit().getImage("d:/icon.jpg");
//创建TrayIcon对象得到托盘图标
trayIcon=new TrayIcon(image,"提示信息",popup);
//设置托盘图标将自动设置尺寸
trayIcon.setImageAutoSize(true);
try
{//将托盘图标设置到系统托盘中
tray.add(trayIcon);
}
catch(AWTException e)
{
e.printStackTrace();
}
//为托盘图标注册监听器
trayIcon.addActionListener(this);
}
//设置窗体关闭按扭所执行的动作
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//将窗体隐藏
SmallBrowser.this.hide();
}
});
//设置窗体标题、大小位置以及可见性
//this.add($jpanel);
Container $Container = this.getContentPane();
$Container.add($SWTPane,BorderLayout.CENTER);
//$jpanel.add($Container);
//this.add($jpanel);
this.setTitle("jplogic迷你浏览器");
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//this.setBounds(100,100,200,100);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==item[0])
{//点击警告消息菜单项执行的动作
//弹出警告消息提示框
trayIcon.displayMessage("警告","这是警告消息",TrayIcon.MessageType.WARNING);
}
else if(e.getSource()==item[1])
{//点击信息消息菜单项执行的动作
//弹出信息消息提示框
trayIcon.displayMessage("信息","这是信息消息",TrayIcon.MessageType.INFO);
}
else if(e.getSource()==item[2])
{//点击错误消息菜单项执行的动作
//弹出错误消息提示框
trayIcon.displayMessage("错误","这是错误消息",TrayIcon.MessageType.ERROR);
}
else if(e.getSource()==item[3])
{//点击退出程序菜单项执行的动作
//结束程序安全退出
System.exit(0);
}
else if(e.getSource()==item[4])
{
//显示窗口
this.show(true);
//System.exit(0);
}
else if(e.getSource()==item[5])
{
//重新访问
this.show(true);
//System.exit(0);
}
else if(e.getSource()==trayIcon)
{//双击托盘图标执行的代码
//将窗体按原来的方式显示出来
this.show(true);
}
}
public static void main(String args[])
{
new SmallBrowser();
}
}
package mini_browser;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Panel;
//import javax.swing.JFrame;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTPane extends Panel
{
private static final long serialVersionUID = 1L;
DisplayThread displayThread;
private Canvas canvas;
public SWTPane()
{
displayThread=new DisplayThread();
displayThread.start();
canvas = new Canvas();
setLayout( new BorderLayout() );
add( canvas, BorderLayout.CENTER );
}
public void addNotify()
{
super.addNotify();
Display dis=displayThread.getDisplay();
dis.syncExec
(
new Runnable()
{
public void run()
{
Shell shell = SWT_AWT.new_Shell(displayThread.getDisplay(),canvas);
shell.setLayout( new FillLayout());
final Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(BorderLayout.CENTER);
browser.setUrl("http://localhost:8080/jplogic");
}
}
);
}
}