[Java] 纯文本查看 复制代码
package cc;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cc {
static ButtonFrame f = new ButtonFrame();
public static void main(String[] args) {
//JOptionPane.showMessageDialog(null, "Code By Sreac.L","About", 1);
f.setTitle("计算器");
f.setBounds(300, 200, 250, 300);
f.setVisible(true);
}
}
class ButtonFrame extends JFrame implements ActionListener {
static TextField t, t1;
static Label l;
float temp1 = 0, r = 0;
String temp2 = "", ysf = "";
public ButtonFrame() {
setLayout(null);
// JTextArea t = new JTextArea();
l = new Label();
l.setLocation(5, 222);
l.setSize(230, 50);
add(l);
t = new TextField("计算、结果_文本框,支持连续四则运算", 50);
t.setBounds(5, 2, 224, 35);
add(t);
t1 = new TextField("运算式预览_文本框,不可编辑", 50);
t1.setBounds(5, 40, 224, 30);
add(t1);
t1.setEditable(false);
Button yu = new Button("%");
yu.setBounds(165, 155, 50, 30);
add(yu);
Button gen = new Button("√");
gen.setBounds(165, 190, 50, 30);
add(gen);
Button chu = new Button("÷");
chu.setBounds(125, 85, 30, 30);
add(chu);
Button cheng = new Button("×");
cheng.setBounds(125, 120, 30, 30);
add(cheng);
Button jia = new Button("+");
jia.setBounds(125, 155, 30, 30);
add(jia);
Button jian = new Button("-");
jian.setBounds(125, 190, 30, 30);
add(jian);
Button q = new Button("清除");
q.setBounds(165, 85, 50, 30);
add(q);
Button b = new Button("Back");
b.setBounds(165, 120, 50, 30);
add(b);
Button b7 = new Button("7");
b7.setBounds(5, 85, 30, 30);
add(b7);
Button b8 = new Button("8");
b8.setBounds(45, 85, 30, 30);
add(b8);
Button b9 = new Button("9");
b9.setBounds(85, 85, 30, 30);
add(b9);
Button b4 = new Button("4");
b4.setBounds(5, 120, 30, 30);
add(b4);
Button b5 = new Button("5");
b5.setBounds(45, 120, 30, 30);
add(b5);
Button b6 = new Button("6");
b6.setBounds(85, 120, 30, 30);
add(b6);
Button b1 = new Button("1");
b1.setBounds(5, 155, 30, 30);
add(b1);
Button b2 = new Button("2");
b2.setBounds(45, 155, 30, 30);
add(b2);
Button b3 = new Button("3");
b3.setBounds(85, 155, 30, 30);
add(b3);
Button bd = new Button(".");
bd.setBounds(5, 190, 30, 30);
add(bd);
Button b0 = new Button("0");
b0.setBounds(45, 190, 30, 30);
add(b0);
Button dy = new Button("=");
dy.setBounds(85, 190, 30, 30);
add(dy);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
dy.addActionListener(this);
bd.addActionListener(this);
q.addActionListener(this);
b.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
jian.addActionListener(this);
jia.addActionListener(this);
yu.addActionListener(this);
gen.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (t.getText().equals("计算、结果_文本框,支持连续四则运算"))
t.setText("");
if (e.getActionCommand().equals("清除")) {//所有变量归0
t.setText("");
t1.setText("");
ysf = "";
l.setText("");
temp1 = 0;
}
if (e.getActionCommand().equals("Back")) {
StringBuffer s = new StringBuffer(t.getText());
if (s.length() != 0)
t.setText(s.delete(s.length() - 1, s.length()) + "");//删除最后一个字符
}
if (e.getActionCommand().equals("1")) {
t.setText(t.getText() + "1");
l.setText(l.getText() + "1");
}
if (e.getActionCommand().equals("2")) {
t.setText(t.getText() + "2");
l.setText(l.getText() + "2");
}
if (e.getActionCommand().equals("3")) {
t.setText(t.getText() + "3");
l.setText(l.getText() + "3");
}
if (e.getActionCommand().equals("4")) {
t.setText(t.getText() + "4");
l.setText(l.getText() + "4");
}
if (e.getActionCommand().equals("5")) {
t.setText(t.getText() + "5");
l.setText(l.getText() + "5");
}
if (e.getActionCommand().equals("6")) {
t.setText(t.getText() + "6");
l.setText(l.getText() + "6");
}
if (e.getActionCommand().equals("7")) {
t.setText(t.getText() + "7");
l.setText(l.getText() + "7");
}
if (e.getActionCommand().equals("8")) {
t.setText(t.getText() + "8");
l.setText(l.getText() + "8");
}
if (e.getActionCommand().equals("9")) {
t.setText(t.getText() + "9");
l.setText(l.getText() + "9");
}
if (e.getActionCommand().equals("0")) {
t.setText(t.getText() + "0");
l.setText(l.getText() + "0");
}
if (e.getActionCommand().equals(".")) {
t.setText(t.getText() + ".");
l.setText(l.getText() + ".");
}
if (e.getActionCommand().equals("+")) {
//不使用正则,另类连续四则运算思路:每次点击运算符先计算出之前所输数值,再用此结果再计算
if (ysf != "") {//判断是否为连续计算
go();//先计算出前一次结果
temp1 = r;//把结果赋给临时变量1
t.setText("");
t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");
} else {
temp1 = Float.parseFloat(t.getText());
t.setText("");
}
ysf = "+";
l.setText(l.getText() + "+");
}
if (e.getActionCommand().equals("-")) {
if (ysf != "") {
go();
temp1 = r;
t.setText("");
t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");
} else {
temp1 = Float.parseFloat(t.getText());
t.setText("");
}
ysf = "-";
l.setText(l.getText() + "-");
}
if (e.getActionCommand().equals("÷")) {
if (ysf != "") {
go();
temp1 = r;
t.setText("");
t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");
} else {
temp1 = Float.parseFloat(t.getText());
t.setText("");
}
ysf = "÷";
l.setText(l.getText() + "÷");
}
if (e.getActionCommand().equals("×")) {
if (ysf != "") {
go();
temp1 = r;
t.setText("");
t1.setText("您可以继续输出数值进行计算:" + temp1 + ysf + "__");
} else {
temp1 = Float.parseFloat(t.getText());
t.setText("");
}
ysf = "×";
l.setText(l.getText() + "×");
}
if (e.getActionCommand().equals("√")) {
t1.setText("√" + t.getText());
t.setText(Math.sqrt(Double.parseDouble(t.getText())) + "");
}
if (e.getActionCommand().equals("%")) {
temp1 = Float.parseFloat(t.getText());
t.setText("");
ysf = "%";
}
if (e.getActionCommand().equals("=")) {
go();
t.setText(r + "");
t1.setText(temp1 + ysf + temp2 + "=" + t.getText());
ysf = "";
l.setText(l.getText() + "="+r);
}
}
void go() {//计算结果
temp2 = t.getText();
if (ysf.equals("+"))
r = temp1 + Float.parseFloat(temp2);
if (ysf.equals("-"))
r = temp1 - Float.parseFloat(temp2);
if (ysf.equals("×"))
r = temp1 * Float.parseFloat(temp2);
if (ysf.equals("÷"))
r = temp1 / Float.parseFloat(temp2);
if (ysf.equals("%"))
r = temp1 % Float.parseFloat(temp2);
}
}