[Java] 纯文本查看 复制代码
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JiSuanQi {
private JFrame frame;
private Container con;
private JPanel panel1, panel2, panel3, panel4;
private JButton[] butt;
private JButton but2, but3, but4, but5, but6, but8, but9, but10, but11, but12;
private JTextField ll;
// 构造器
public JiSuanQi() {
frame = new JFrame("SVIP专用计算器");
frame.setSize(500, 600);
frame.setLocation(600, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();
init();
frame.setVisible(true);
}
//模块以及计算方法
@SuppressWarnings("unused")
void init() {
// 上面的模块
frame.setLayout(new BorderLayout());
panel1 = new JPanel();
con.add(panel1, BorderLayout.NORTH);
ll = new JTextField(30);
ll.setFont(new Font("宋体", Font.PLAIN, 30));
panel1.add(ll);
// 中间的模块
// 九键
butt = new JButton[9];
panel2 = new JPanel();
panel2.setLayout(new GridLayout(3, 3));
con.add(panel2, BorderLayout.CENTER);
for (int i = 0; i < butt.length; i++) {
butt[i] = new JButton((i + 1) + "");
Font f = new Font("宋体", Font.BOLD, 40);
butt[i].setFont(f);
panel2.add(butt[i]);
}
// 1到9 每个键位设置
butt[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 1 + "");
}
});
butt[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 2 + "");
}
});
butt[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 3 + "");
}
});
butt[3].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 4 + "");
}
});
butt[4].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 5 + "");
}
});
butt[5].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 6 + "");
}
});
butt[6].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 7 + "");
}
});
butt[7].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 8 + "");
}
});
butt[8].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + 9 + "");
}
});
// 右边的模块
panel3 = new JPanel();
panel3.setLayout(new GridLayout(3, 1));
con.add(panel3, BorderLayout.EAST);
but2 = new JButton("*");
Font f = new Font("宋体", Font.BOLD, 40);
but2.setFont(f);
panel3.add(but2);
but2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + "*");
}
});
but3 = new JButton("/");
Font q = new Font("宋体", Font.BOLD, 40);
but3.setFont(f);
panel3.add(but3);
but3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + "/");
}
});
but4 = new JButton("C");
Font w = new Font("宋体", Font.BOLD, 40);
but4.setFont(f);
panel3.add(but4);
but4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText("");
}
});
panel3.setPreferredSize(new Dimension(120, 0));
// 下边的模块
panel4 = new JPanel();
panel4.setLayout(new GridLayout(2, 4));
con.add(panel4, BorderLayout.SOUTH);
but5 = new JButton("0");
// 根据指定字体名称、样式和磅值大小,创建一个新 Font。
Font e = new Font("宋体", Font.BOLD, 40);
but5.setFont(f);
panel4.add(but5);
but5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + "0");
}
});
but6 = new JButton("+");
Font r = new Font("宋体", Font.BOLD, 40);
but6.setFont(f);
panel4.add(but6);
but6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + "+");
}
});
but8 = new JButton("-");
Font t = new Font("宋体", Font.BOLD, 40);
but8.setFont(f);
panel4.add(but8);
but8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + "-");
}
});
// substring(0,length-1)删除字符串最后一位
but12 = new JButton("b");
Font y = new Font("宋体", Font.BOLD, 40);
but12.setFont(f);
panel4.add(but12);
but12.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
ll.setText(ll.getText().substring(0, ll.getText().length() - 1));
} catch (Exception a) {
ll.setText("");
}
}
});
but9 = new JButton(".");
Font a = new Font("宋体", Font.BOLD, 40);
but9.setFont(f);
panel4.add(but9);
but9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + ".");
}
});
but10 = new JButton("(");
Font s = new Font("宋体", Font.BOLD, 40);
but10.setFont(f);
panel4.add(but10);
but10.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + "(");
}
});
but11 = new JButton(")");
Font d = new Font("宋体", Font.BOLD, 40);
but11.setFont(f);
panel4.add(but11);
but11.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ll.setText(ll.getText() + ")");
}
});
// =计算数据结果以及处理异常
but8 = new JButton("=");
Font z = new Font("宋体", Font.BOLD, 40);
but8.setFont(f);
panel4.add(but8);
but8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("js");
Bindings bindings = engine.createBindings();
String expression = (ll.getText());
bindings.put("expression", expression);
Integer value = 1;
Double v = 1.0;
try {
int a = (int) engine.eval("eval(expression)", bindings);
ll.setText("" + a);
} catch (Exception e1) {
try {
Double v1 = (Double) engine.eval("eval(expression)", bindings);
ll.setText("" + v1);
} catch (Exception e11) {
ll.setText("输入有误!请重新输入!");
}
}
}
});
panel4.setPreferredSize(new Dimension(0, 200));
}
public static void main(String[] args) {
new JiSuanQi();
}
}