JTextField text = new JTextField();
JPanel p1,p2;//上下两个面板p1和p2
JButton b[] = new JButton[16];
String s;//从文本框读的字符串
p1.setLayout(new BorderLayout());
text = new JTextField();
//重新定义长度和宽度
text.setPreferredSize(new Dimension(30,30));
p1.add(text);
this.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(3,5,3,3));//网格式布局,三行五列
String str [] = {"0","1","2","3","4","5","6","7",
"8","9","+","-","*","/",".","="};
ActionListener command=new command();
for(int i = 0;i<str.length;i++)
{
b[i] = new JButton(str[i]);
b[i].addActionListener(command);//监听
// b.setSize(50, 20);
p2.add(b[i]);
}
this.add(p2);
class command implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s = e.getActionCommand();//获取每次输入的内容
if(s.equals("="))
{
s=text.getText();//获取文本框的内容
//直接使用第三方库import javax.script.*,下面有解释
ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
try {
//System.out.print(js.eval(s));
//将Int转为string
//text.setText(js.eval(s)+""),num + ""型转换
//int.toString()型转换
text.setText(js.eval(s).toString());
} catch (ScriptException e1) {
e1.printStackTrace();
}
}
//如果还没有输入等号,在文本框显示内容即可
else{
text.setText(text.getText()+s);
}
}
运行结果:
import javax.script.*;
public class day_2 {
public static void main(String []args) {
String s1="24+23";
String s2="24-2";
String s3="24*2";
String s4="24/2";
ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
try {
System.out.println(s1+"="+js.eval(s1));
System.out.println(s2+"="+js.eval(s2));
System.out.println(s3+"="+js.eval(s3));
System.out.println(s4+"="+js.eval(s4));
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
可运行源代码:
import java.awt.*;
import java.awt.event.*;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import javax.swing.border.Border;
import org.eclipse.swt.widgets.Layout;
public class day_1 extends JFrame {
JTextField text = new JTextField();
JPanel p1,p2;
JButton b[] = new JButton[16];
String s;
day_1(){
super("check !");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.setLayout(new BorderLayout());
text = new JTextField();//此处定义不用JTextField
//否则引发空指针异常
text.setPreferredSize(new Dimension(30,30));
p2.setLayout(new GridLayout(3,5,3,3));
String str [] = {"0","1","2","3","4","5","6","7",
"8","9","+","-","*","/",".","="};
ActionListener command=new command();
for(int i = 0;i<str.length;i++)
{
b[i] = new JButton(str[i]);
b[i].addActionListener(command);
// b.setSize(50, 20);
p2.add(b[i]);
}
p1.add(text);
this.add(p1,BorderLayout.NORTH);
this.add(p2);
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
class command implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s = e.getActionCommand();
if(s.equals("="))
{
s=text.getText();
ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
try {
// System.out.print(js.eval(s));
//将Int转为string
// text.setText(js.eval(s)+"");
text.setText(js.eval(s).toString());
} catch (ScriptException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else{
text.setText(text.getText()+s);
}
}
}
public static void main(String []args) {
new day_1();
}
}