java swing怎么嵌入js
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
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;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.mohe.utils.system.L;
/**
* 主窗体
*
* @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;
public int pageFlag = 1;
/**
* 构造器
*/
public MainFrame() {
initData();
initGUI();
addListance();
}
private void initData() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
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.local + Config.index);
webBrowser.setBarsVisible(false);
webBrowser.setMenuBarVisible(false);
webBrowser.setLocationBarVisible(false);
webBrowser.setButtonBarVisible(false);
webBrowser.setStatusBarVisible(false);
ImageIcon icon = new ImageIcon(System.getProperty(“user.dir”) + “\\城市.gif”);
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();
L.info(“___________ windowWillOpen”);
}
@Override
public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ windowOpening”);
}
@Override
public void windowClosing(WebBrowserEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ windowClosing”);
}
@Override
public void titleChanged(WebBrowserEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ titleChanged”);
}
@Override
public void statusChanged(WebBrowserEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ statusChanged”);
}
@Override
public void locationChanging(WebBrowserNavigationEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ locationChanging”);
}
@Override
public void locationChanged(WebBrowserNavigationEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ locationChanged”);
// 判断当前页面等级
String locationURL = arg0.getNewResourceLocation();
int index = locationURL.indexOf(“pageFlag=”);
if (index != -1) {
pageFlag = Integer.parseInt(locationURL.substring(index + 9, index + 10));
} else {
pageFlag = 1;
}
System.out.println(“____________________ pageFlag:” + pageFlag);
// 判断下载文件进行打印
index = locationURL.indexOf(“fileName=”);
System.out.println(“____________________ file Index:” + index);
if (index != -1) {
String fs = locationURL.substring(index + 9);
L.info(“______________________ fileName = ” + fs);
String[] fns = fs.split(“,”);
try {
ListFile flist = downNetFile(Arrays.asList(fns));
doPrintDoc(flist);
} catch (IOException e) {
e.printStackTrace();
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
webBrowser.setVisible(false);
webBrowser.setVisible(true);
webBrowser.updateUI();
webBrowser.repaint();
panel.updateUI();
panel.repaint();
panel.validate();
}
});
}
@Override
public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ locationChangeCanceled”);
}
@Override
public void loadingProgressChanged(WebBrowserEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ loadingProgressChanged”);
}
@Override
public void commandReceived(WebBrowserCommandEvent arg0) {
lastTime = new Date().getTime();
L.info(“___________ commandReceived”);
}
});
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);
}
});
}
怎么把js文件添加到java界面里面
1.取得脚本解释器的管理器Manager
2.从管理器中取得js的解释器实例ScriptEngine
3.取得存储javascript变量的Bindings实例
4.把一个java.io.Reader流及Bindings传递给ScriptEngine的eval方法返回脚本运行结果,如果执行中发生错误,会抛出ScriptException异常。
例:运行javascript脚本的一个java程序
throwsIOException{ScriptEngineManagerscriptManager=newScriptEngineManager;//从管理器中获取js的解释器//定义我们要运行的脚本文件Stringfilename=null;//通过解释器来获得存储javascript变量的Bindings的实例,使它们提供给脚本。Bindingsbindings=js.createBindings;//在map中获取和设置值的方法publicObjectget;//使用我们的name/value对(即javascript变量)Bindingsbindings=newSimpleBindings(defaults);//创建一个变量,用于存放脚本执行的内容ScriptContextcontext=newSimpleScriptContext();//设置那些Bindings在Context中,使它们可读。但这样的变量定义的脚本不要放入我们的Map中context.setBindings(bindings,ScriptContext.GLOBAL_SCOPE);BufferedReaderin=newBufferedReader(newFileReader(filename));Stringline;while((line=in.readLine())!=null){line=line.trim();if(line.length()==0)continue;//跳过空行if(line.charAt(0)==‘#‘)continue;//跳过命令intpos=line.indexOf(“:”);if(pos==-1){thrownewIllegalArgumentException(“syntax:”+line);}Stringname=line.substring(0,pos).trim();Stringvalue=line.substring(pos+1).trim();charfirstchar=value.charAt(0);intlen=value.length();charlastchar=value.charAt(len-1);if(firstchar==‘”‘lastchar==‘”‘){//双引号引用的值为字符串defaults.put(name,value.substring(1,len-1));}elseif(Character.isDigit(firstchar)){//如果开始是一个数字try{doubled=Double.parseDouble(value);defaults.put(name,value);}catch(NumberFormatExceptione){//没有数字,是一个stringdefaults.put(name,value);}}elseif(“true”.equals(value)){//处理布尔值defaults.put(name,Boolean.TRUE);}elseif(“false”.equals(value)){defaults.put(name,Boolean.FALSE);}elseif(“null”.equals(value)){//处理null值defaults.put(name,null);}elseif(firstchar==‘{‘lastchar==‘}‘){//如果值是在一对大括号之内,则执行javascript代码Stringscript=value.substring(1,len-1);Objectresult=engine.eval(script,context);defaults.put(name,result);}else{//其它情况,刚好是一个字符串defaults.put(name,value);}}}//一个简单的类的测试程序publicstaticvoidmain(String[]args)throwsIOException,ScriptException{Configurationdefaults=newConfiguration();defaults.load(args[0]);Set
java中怎么插入js函数
js是前台页面应用技术,java是后台应用技术,js后台是用不上的,如果需要,可以jsp在前台混插使用。
Java是不可以插入js的,但是可以输出js,一般有以下两种方法:
1.通过request跳转的方式,在中间弹出js代码。
2.可以通过out.print()打印的方式把整个页面打印出来,这中间虽然是Java中输出,但是也是输出的页面。
大体这么多,也可能有我不知道的方法,如果觉的可以,给分,谢谢。
在JSP页面中加入JavaScript小程序的方法分别有哪两种?其语法格式为?
在页面中引用JavaScript代码目前有两种方法
第一种是外联,在页面中加入外联链接:
第二种是写在页面内部,理论上可以写在页面任何地方//JS代码。
JSP(全称JavaServerPages)是由SunMicrosystems公司主导创建的一种动态网页技术标准。JSP部署于网络服务器上,可以响应客户端发送的请求,并根据请求内容动态地生成HTML、XML或其他格式文档的Web网页,然后返回给请求者。JSP技术以Java语言作为脚本语言,为用户的HTTP请求提供服务,并能与服务器上的其它Java程序共同处理复杂的业务需求。
java调用js代码性能问题
java调用js代码性能问题如下:
1、转换成本:在Java和JS之间进行类型转换的过程中,会产生额外的开销和时间成本。
2、内存开销:JavaScriptEngine在执行JS代码时,需要占用一定的内存空间,如果JS代码较多或执行时间较长,会占用大量的内存资源。
3、安全问题:由于JS代码是由外部输入的,因此在执行JS代码时需要特别注意安全问题,防止JS代码中存在恶意代码,造成安全漏洞。
如何把java代码嵌入到javascript中
如果页面是JSP页面,则可以直接嵌套写,如:
javascriptvar jsstr = “abc”;/javascript
% String str = new String(” % javascriptjsstr/javascript% “; %
如果是HTML页面,则只有用DWR调用JAVA方法进行值的传递。