学校要求的界面,没有等号我不是很理解,本计算器在文本框上打回车代替等号
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//计算原理,将操作整合成字符串,先以加减分割字符串,对带有乘除的字符串先进行运算,在按加减对运算结果运算
class computer extends JFrame implements ActionListener, KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
String strin = "";
JTextField t1 = new JTextField();
char[] z = new char[100];
int number = 0;
int panduan = 0;
computer() {
this.setTitle("我的计算器1.0");
this.setSize(270, 215);
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
JLabel L1 = new JLabel("计算结果:");
t1.addKeyListener(this);
p1.add(L1);
p1.add(t1);
add(p1, BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4, 3));
JPanel p4 = new JPanel();
p4.setLayout(new FlowLayout());
JButton[] x = new JButton[12];
x[0] = new JButton("1");
x[1] = new JButton("2");
x[2] = new JButton("3");
x[3] = new JButton("4");
x[4] = new JButton("5");
x[5] = new JButton("6");
x[6] = new JButton("7");
x[7] = new JButton("8");
x[8] = new JButton("9");
x[9] = new JButton("0");
x[10] = new JButton("清零");
x[11] = new JButton("平方");
for (int a = 0; a < 12; a++)
p2.add(x[a]);
p4.add(p2);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(4, 1));
JButton[] y = new JButton[4];
y[0] = new JButton("+");
y[1] = new JButton("-");
y[2] = new JButton("*");
y[3] = new JButton("/");
for (int a = 0; a < 4; a++)
p3.add(y[a]);
p4.add(p3);
add(p4, BorderLayout.CENTER);
JLabel l2 = new JLabel("版权所有:zzuli", SwingConstants.RIGHT);
add(l2, BorderLayout.SOUTH);
for (int i = 0; i < 12; i++) {
x[i].addActionListener(this);
}
for (int i = 0; i < 4; i++) {
y[i].addActionListener(this);
}
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String lab = e.getActionCommand();
if (lab.equals("1")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "1";
t1.setText(strin);
} else if (lab.equals("2")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "2";
t1.setText(strin);
} else if (lab.equals("3")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "3";
t1.setText(strin);
} else if (lab.equals("4")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "4";
t1.setText(strin);
} else if (lab.equals("5")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "5";
t1.setText(strin);
} else if (lab.equals("6")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "6";
t1.setText(strin);
} else if (lab.equals("7")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "7";
t1.setText(strin);
} else if (lab.equals("8")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "8";
t1.setText(strin);
} else if (lab.equals("9")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "9";
t1.setText(strin);
} else if (lab.equals("0")) {
if(panduan==1)
{
strin="";
panduan=0;
}
strin = strin + "0";
t1.setText(strin);
} else if (lab.equals("清零")) {
strin = "";
number = 0;
t1.setText(strin);
} else if (lab.equals("平方")) {
if(panduan==1)
{
panduan=0;
}
try {
String p="";
int a=strin.length()-1;
for(;a>=0;a--)
{
if(strin.charAt(a)=='+'||strin.charAt(a)=='-'||strin.charAt(a)=='*'||strin.charAt(a)=='/')
break;
}
p=strin.substring(a+1);
Double f= Double.parseDouble(p);
f=f*f;
if(a==0&&(strin.charAt(a)=='+'||strin.charAt(a)=='-'))
strin=strin.substring(1, a+1)+f;
else
strin=strin.substring(0, a+1)+f;
t1.setText(strin);
}catch (Exception q) {
t1.setText("格式错误");
strin="";
}
} else if (lab.equals("+")) {
if(panduan==1)
{
panduan=0;
}
strin = strin + "+";
z[number++] = '+';
t1.setText(strin);
} else if (lab.equals("-")) {
if(panduan==1)
{
panduan=0;
}
strin = strin + "-";
z[number++] = '-';
t1.setText(strin);
} else if (lab.equals("*")) {
if(panduan==1)
{
panduan=0;
}
strin = strin + "*";
t1.setText(strin);
} else if (lab.equals("/")) {
if(panduan==1)
{
panduan=0;
}
strin = strin + "/";
t1.setText(strin);
}
}
public void keyTyped(KeyEvent e) {
if ((char) e.getKeyChar() == KeyEvent.VK_ENTER) {
String str1 = strin;
String[] x = str1.split("\\+|-");
try {
judge1(str1);
double[] y = new double[x.length];
for (int a = 0; a < x.length; a++) {
y[a] = judge(x[a]);
}
if (x.length == number) {
if (z[0] == '-')
y[0] = -y[0];
for (int a = 1; a < x.length; a++) {
if (z[a] == '+')
y[0] = y[0] + y[a];
else
y[0] = y[0] - y[a];
}
} else {
for (int a = 1; a < x.length; a++) {
if (z[a - 1] == '+')
y[0] = y[0] + y[a];
else
y[0] = y[0] - y[a];
}
}
strin="" + y[0];
t1.setText(strin);
} catch (Exception q) {
t1.setText("格式错误");
strin="";
} finally {
number=0;
panduan=1;
}
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
double judge(String x) {
if(x.equals(""))
return 0;
String[] y = x.split("\\*|/");
double zo=Double.parseDouble(y[0]);
int ci=1;
for (int a = 0; a < x.length(); a++) {
if (x.charAt(a) == '*') {
zo=zo* Double.parseDouble(y[ci++]);
} else if (x.charAt(a) == '/') {
zo=zo/ Double.parseDouble(y[ci++]);
}
}
return zo;
}
void judge1(String x) throws Exception{
if(x.endsWith("+")||x.endsWith("-")||x.endsWith("*")||x.endsWith("/"))
throw new Exception();
}
}
public class hello {
public static void main(String[] args) {
new computer();
}
}