
import java.io.*
import javax.swing.*
import java.awt.*
import java.awt.event.*
import java.sql.*
import java.net.*
import java.awt.color.*
public class Login extends JFrame implements ActionListener
{
JLabel username
JLabel userpwd
JLabel type
JTextField use
JPasswordField pwd
JButton login
JButton canel
JComboBox js
JPanel pane
public Login(String str){
super(str)
this.setTitle("用户登录界面")
username=new JLabel("帐号:")
userpwd=new JLabel("密码:")
use=new JTextField("")
pwd=new JPasswordField('*')
login=new JButton("登录")
canel=new JButton("取消")
pane=new JPanel()
type=new JLabel("账户类型:")
String strName[]={"管理员","普通用户","会员"
}
js=new JComboBox(strName)
js.setSelectedIndex(2)
js.setMaximumRowCount(3)
pane.add(js)
pane.setLayout(null)
pane.add(username)
pane.add(use)
pane.add(userpwd)
pane.add(pwd)
pane.add(canel)
pane.add(login)
pane.add(type)
js.setBounds(70,90,150,20)
username.setBounds(15,30,150,20)
use.setBounds(65,30,150,20)
userpwd.setBounds(15,60,150,20)
type.setBounds(15,90,150,20)
pwd.setBounds(65,60,150,20)
login.setBounds(10,120,60,20)
canel.setBounds(160,120,60,20)
this.getContentPane().add(pane).setBackground(Color.white)
this.getContentPane().add(pane)
login.addActionListener(this)
canel.addActionListener(this)
this.setResizable(false)
this.setSize(245,200)
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
this.setVisible(true)
}
public void actionPerformed(ActionEvent e){//2
JButton jb=(JButton)e.getSource()
if(jb==login){
String name=use.getText()
String Pwd=pwd.getText()
int count=6
int ss=12
if(pwd.getText().equals("")&&use.getText().equals("")||use.getText().length()>=count&&pwd.getText().length()>=count||use.getText().length()<=ss&&pwd.getText().length()<=ss)
{
JOptionPane.showMessageDialog(null,"用户名或者密码为空或者长度错误","错误消息",JOptionPane.ERROR_MESSAGE)
}
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
Connection con =DriverManager.getConnection("jdbc:odbc:Library","sa","")
Statement st=con.createStatement()
ResultSet rs=st.executeQuery("select * from Users where username='"+name+"' and password='"+Pwd+"'")
if(rs.next())
{
JOptionPane.showMessageDialog(null,"登录成功,欢迎使用")
this.setVisible(false)
}
else
JOptionPane.showMessageDialog(null,"登录失败","错误消息",JOptionPane.ERROR_MESSAGE)
con.close()
}catch(Exception ex){
ex.printStackTrace()
}
}else if(jb==canel){
this.setVisible(false)
}
}
public static void main(String args[]){
new Login("")
}
}
<!DOCTYPE html><html>
<head>
<title> New Document </title>
<meta name="content-type" content="text/html charset=UTF-8">
</head>
<body>
<form id="f1" method="post" action="">
姓名:<input type="text" name="name" /><br>
年龄:<input type="text" name="age" /><br>
职业:<input type="text" name="zy" /><br>
<input value="提交" type="submit" onclick="check(this.form)"/>
</form>
</body>
</html>
<script type="text/javascript">
function check(form) {
if(f1.age.value < 16 || f1.age.value > 40) {
alert("年龄不在有效范围")
form.userId.focus()
return false
}
document.myform.submit()
}
</script>
使用Java8,Nashorn大大提高了JavaScript 引擎引入,以取代现有的Nashorn Java脚本引擎。Nashorn提供2至10倍更好的性能,因为它直接编译代码在存储器,并传递到字节码JVM.Nashorn使用invokedynamics函数,在Java7引入以提高性能。jjs
对于Nashorn引擎,JAVA8引入了一个新的命令行工具,JJS到控制台执行Java脚本代码。
解读js文件
创建并保存sample.js在 C:>JAVA 文件夹。
sample.jsprint('Hello World!')
打开控制台并使用下面的命令。
C:\JAVA>jjs sample.js
看到结果
Hello World!
JJS在交互模式
打开控制台并使用下面的命令
C:\JAVA>jjs
jjs>print("Hello, World!")
Hello, World!
jjs>quit()
>>
传递参数
打开控制台并使用下面的命令。
C:\JAVA>jjs -- a b c
jjs>print('letters: ' +arguments.join(", "))
letters: a, b, c
jjs>
在JAVA调用JavaScript
使用ScriptEngineManager,JavaScript代码用Java编写可以被调用。
示例
选择使用任何编辑器创建以下java程序在 C:/>JAVA
Java8Tester.javaimport javax.script.ScriptEngineManager
import javax.script.ScriptEngine
import javax.script.ScriptException
public class Java8Tester {
public static void main(String args[]){
ScriptEngineManager scriptEngineManager = new ScriptEngineManager()
ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn")
String name = "Mahesh"
Integer result = null
try {
nashorn.eval("print('" + name + "')")
result = (Integer) nashorn.eval("10 + 2")
}catch(ScriptException e){
System.out.println("Error executing script: "+ e.getMessage())
}
System.out.println(result.toString())
}
}
验证结果
使用javac编译器编译如下类
C:\JAVA>javac Java8Tester.java
现在运行Java8Tester看到的结果
C:\JAVA>java Java8Tester
看到结果
Mahesh
12
从JavaScript调用Java
下面的例子将展示如何导入和使用Java类的Java脚本。
创建并保存 sample.js 在 c: >JAVA 文件夹.
sample.jsvar BigDecimal = Java.type('java.math.BigDecimal')
function calculate(amount, percentage) {
var result = new BigDecimal(amount).multiply(
new BigDecimal(percentage)).divide(
new BigDecimal("100"), 2, BigDecimal.ROUND_HALF_EVEN)
return result.toPlainString()
}
var result = calculate(,13.9)
print(result)
打开控制台并使用下面的命令。
C:\JAVA>jjs sample.js
看到结果
.20
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)