package 计算器;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
public class Application extends JFrame {
Calculator panel1;
JButton[] button = new JButton[16];
JPanel panel2;
JPanel panel3;
JButton button3;
String[] s1 = {"0","1","2","3","4","5","6"
,"7","8","9","+","-","X","÷",".","="
};
public static void main(String[] args){
Application Ap = new Application();
}
public Application() {
setSize(300,300);
setLocation(100,100);
setTitle("计算器");
for(int i = 0;i < 16;i ++) {
button[i] = new JButton(s1[i]);
button[i].setFont(new Font("华文琥珀",Font.PLAIN,26));
}
panel1 = new Calculator();
panel3 = new JPanel();
button3 = new JButton("清空");
//button3.setBackground(Color.lightGray);
button3.setFont(new Font("华文琥珀",Font.PLAIN,26));
button3.addActionListener(panel1);
button3.setActionCommand("清空");
add(panel1,BorderLayout.NORTH);
panel3.setLayout(new BorderLayout());
panel3.add(button3,BorderLayout.CENTER);
add(panel3,BorderLayout.SOUTH);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,4));
for(int i = 0;i < 16;i++) {
panel2.add(button[i]);
}
add(panel2,BorderLayout.CENTER);
for(int i = 0;i < 16;i++) {
button[i].addActionListener(panel1);
}
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
public class Application extends JFrame {
Calculator panel1;
JButton[] button = new JButton[16];
JPanel panel2;
JPanel panel3;
JButton button3;
String[] s1 = {"0","1","2","3","4","5","6"
,"7","8","9","+","-","X","÷",".","="
};
public static void main(String[] args){
Application Ap = new Application();
}
public Application() {
setSize(300,300);
setLocation(100,100);
setTitle("计算器");
for(int i = 0;i < 16;i ++) {
button[i] = new JButton(s1[i]);
button[i].setFont(new Font("华文琥珀",Font.PLAIN,26));
}
panel1 = new Calculator();
panel3 = new JPanel();
button3 = new JButton("清空");
//button3.setBackground(Color.lightGray);
button3.setFont(new Font("华文琥珀",Font.PLAIN,26));
button3.addActionListener(panel1);
button3.setActionCommand("清空");
add(panel1,BorderLayout.NORTH);
panel3.setLayout(new BorderLayout());
panel3.add(button3,BorderLayout.CENTER);
add(panel3,BorderLayout.SOUTH);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(4,4));
for(int i = 0;i < 16;i++) {
panel2.add(button[i]);
}
add(panel2,BorderLayout.CENTER);
for(int i = 0;i < 16;i++) {
button[i].addActionListener(panel1);
}
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class Calculator extends JPanel implements ActionListener{
JTextField text = new JTextField(33);
String s;
public Calculator() {
this.add(text);
//text.setSize(20,43);
text.setFont(new Font("楷体",Font.PLAIN,16));
text.setEditable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
String ch1 = text.getText();
String ch2 = e.getActionCommand();
text.setText(ch1+ch2);
if(e.getActionCommand().equals("=")) {
String ss = text.getText();
int length = ss.length();
s = ss.substring(0,length-1);
//提取数字
String[] s1 = s.split("[^0123456789.]+");
int m = s1.length;
//提取运算符
String[] s2 = s.split("[1234567890.]+");
int n = s1.length;
double num;
int count1 = m;
int count2 = 0;
//System.out.print(n);
for(int i = 1; i < n; i ++) {
if(s2[i].equals("X")) {
num = Double.parseDouble(s1[i-1])*Double.parseDouble(s1[i]);
s1[i-1] = String.valueOf(num);
count1 --;
count2 ++;
for(int x = i ; x < m - 1; x ++) {
s1[x] = s1[x+1];
}
for(int y = i; y < n - 1; y ++) {
s2[y] = s2[y+1];
}
n--;
i--;
}
if(s2[i].equals("÷")) {
num = Double.parseDouble(s1[i-1])/Double.parseDouble(s1[i]);
s1[i-1] = String.valueOf(num);
count1 --;
count2 ++;
for(int x = i; x < m - 1; x ++) {
s1[x] = s1[x+1];
}
for(int y = i; y < n - 1; y ++) {
s2[y] = s2[y+1];
}
n--;
i--;
}
}
double sum = Double.parseDouble(s1[0]);
for(int i = 1; i < n; i ++) {
if(s2[i].equals("+")) {
sum+=Double.parseDouble(s1[i]);
}
else {
sum-=Double.parseDouble(s1[i]);
}
}
text.setText(String.valueOf(sum));
}
if(e.getActionCommand().equals("清空")) {
text.setText("");
}
}
}
class Calculator extends JPanel implements ActionListener{
JTextField text = new JTextField(33);
String s;
public Calculator() {
this.add(text);
//text.setSize(20,43);
text.setFont(new Font("楷体",Font.PLAIN,16));
text.setEditable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
String ch1 = text.getText();
String ch2 = e.getActionCommand();
text.setText(ch1+ch2);
if(e.getActionCommand().equals("=")) {
String ss = text.getText();
int length = ss.length();
s = ss.substring(0,length-1);
//提取数字
String[] s1 = s.split("[^0123456789.]+");
int m = s1.length;
//提取运算符
String[] s2 = s.split("[1234567890.]+");
int n = s1.length;
double num;
int count1 = m;
int count2 = 0;
//System.out.print(n);
for(int i = 1; i < n; i ++) {
if(s2[i].equals("X")) {
num = Double.parseDouble(s1[i-1])*Double.parseDouble(s1[i]);
s1[i-1] = String.valueOf(num);
count1 --;
count2 ++;
for(int x = i ; x < m - 1; x ++) {
s1[x] = s1[x+1];
}
for(int y = i; y < n - 1; y ++) {
s2[y] = s2[y+1];
}
n--;
i--;
}
if(s2[i].equals("÷")) {
num = Double.parseDouble(s1[i-1])/Double.parseDouble(s1[i]);
s1[i-1] = String.valueOf(num);
count1 --;
count2 ++;
for(int x = i; x < m - 1; x ++) {
s1[x] = s1[x+1];
}
for(int y = i; y < n - 1; y ++) {
s2[y] = s2[y+1];
}
n--;
i--;
}
}
double sum = Double.parseDouble(s1[0]);
for(int i = 1; i < n; i ++) {
if(s2[i].equals("+")) {
sum+=Double.parseDouble(s1[i]);
}
else {
sum-=Double.parseDouble(s1[i]);
}
}
text.setText(String.valueOf(sum));
}
if(e.getActionCommand().equals("清空")) {
text.setText("");
}
}
}