parent
b4ce2fa0af
commit
5db1a7793e
@ -1,156 +0,0 @@
|
|||||||
package Calculatorver4;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
public class CalculatorJFrame extends JFrame {
|
|
||||||
JLabel j1 = new JLabel("简易计算器");
|
|
||||||
JLabel j2 = new JLabel("运算数一");
|
|
||||||
JLabel j3 = new JLabel("运算数二");
|
|
||||||
JLabel j4 = new JLabel("运算结果");
|
|
||||||
JLabel l1 = new JLabel(" ".repeat(1000));
|
|
||||||
JLabel l2 = new JLabel(" ".repeat(1000));
|
|
||||||
JLabel l3 = new JLabel(" ".repeat(1000));
|
|
||||||
JLabel l4 = new JLabel(" ".repeat(1000));
|
|
||||||
JLabel l5 = new JLabel(" ".repeat(1000));
|
|
||||||
JLabel l6 = new JLabel(" ".repeat(1000));
|
|
||||||
JTextField t1 = new JTextField(10);
|
|
||||||
JTextField t2 = new JTextField(10);
|
|
||||||
JTextField t3 = new JTextField(10);
|
|
||||||
JTextField t4 = new JTextField(30);
|
|
||||||
JButton b1 = new JButton("相加");
|
|
||||||
JButton b2 = new JButton("相减");
|
|
||||||
JButton b3 = new JButton("全部清零");
|
|
||||||
public CalculatorJFrame() {
|
|
||||||
initJFrame();
|
|
||||||
|
|
||||||
loadInterFace();
|
|
||||||
|
|
||||||
this.setVisible(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadInterFace() {
|
|
||||||
|
|
||||||
this.getContentPane().add(j1);
|
|
||||||
this.getContentPane().add(l1);
|
|
||||||
this.getContentPane().add(j2);
|
|
||||||
this.getContentPane().add(t1);
|
|
||||||
this.getContentPane().add(l2);
|
|
||||||
this.getContentPane().add(j3);
|
|
||||||
this.getContentPane().add(t2);
|
|
||||||
this.getContentPane().add(l3);
|
|
||||||
this.getContentPane().add(j4);
|
|
||||||
this.getContentPane().add(t3);
|
|
||||||
this.getContentPane().add(l4);
|
|
||||||
this.getContentPane().add(b1);
|
|
||||||
this.getContentPane().add(b2);
|
|
||||||
|
|
||||||
this.getContentPane().add(l5);
|
|
||||||
this.getContentPane().add(b3);
|
|
||||||
this.getContentPane().add(l6);
|
|
||||||
this.getContentPane().add(t4);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
b1.addActionListener(new MyActionListener());
|
|
||||||
b2.addActionListener(new MyActionListener());
|
|
||||||
b3.addActionListener(new MyActionListener());
|
|
||||||
|
|
||||||
// t1.addActionListener(new TextActionListener(t1));
|
|
||||||
// t2.addActionListener(new TextActionListener(t2));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initJFrame() {
|
|
||||||
this.setSize(400,400);
|
|
||||||
this.setTitle("简易计算器");
|
|
||||||
this.setLocationRelativeTo(null);
|
|
||||||
this.setDefaultCloseOperation(3);
|
|
||||||
this.setLayout(new FlowLayout());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private class MyActionListener implements ActionListener{
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if(e.getSource() != b3) {
|
|
||||||
if(t1.getText().equals("") || t2.getText().equals("")) {
|
|
||||||
t4.setText("");
|
|
||||||
t4.setText("注意:运算数一和运算数二不能为空!");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
t4.setText("");
|
|
||||||
if(e.getSource() == b1) {
|
|
||||||
if(check(t1) + check(t2)==2){
|
|
||||||
String sa = t1.getText();
|
|
||||||
Double a2 = Double.parseDouble(sa);
|
|
||||||
String sb = t2.getText();
|
|
||||||
Double b = Double.parseDouble(sb);
|
|
||||||
Double c = a2+b;
|
|
||||||
String sc = Double.toString(c);
|
|
||||||
t3.setText(sc);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else if(e.getSource() == b2) {
|
|
||||||
if(check(t1) + check(t2)==2) {
|
|
||||||
String sa = t1.getText();
|
|
||||||
Double a = Double.parseDouble(sa);
|
|
||||||
String sb = t2.getText();
|
|
||||||
Double b = Double.parseDouble(sb);
|
|
||||||
Double c = a - b;
|
|
||||||
String sc = Double.toString(c);
|
|
||||||
t3.setText(sc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
t1.setText("");
|
|
||||||
t2.setText("");
|
|
||||||
t3.setText("");
|
|
||||||
t4.setText("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public int check(JTextField tf) {
|
|
||||||
JTextField t = t1;
|
|
||||||
t4.setText("");
|
|
||||||
String s = t.getText();
|
|
||||||
boolean flag = true;
|
|
||||||
boolean l=true,r=true;
|
|
||||||
int cnt=0;
|
|
||||||
char[] a = s.toCharArray();
|
|
||||||
if(a[0] == '.') l = false;
|
|
||||||
if(a[a.length-1] == '.') r = false;
|
|
||||||
for(int i=0;i<a.length;i++) {
|
|
||||||
if(a[i]=='.') cnt+=1;
|
|
||||||
if( (a[i]<'0' || a[i]>'9') && a[i]!='.' ) {
|
|
||||||
flag = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!flag) {
|
|
||||||
t4.setText("");
|
|
||||||
t4.setText("注意:运算数中不能含有除了数字和小数点之外的非法字符!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if(!l) {
|
|
||||||
t4.setText("");
|
|
||||||
t4.setText("注意:运算数的开头不能是小数点!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if(!r) {
|
|
||||||
t4.setText("");
|
|
||||||
t4.setText("注意:运算数的结尾不能是小数点!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if(cnt>1) {
|
|
||||||
t4.setText("");
|
|
||||||
t4.setText("注意:运算数中小数点超过一个!");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue