你可以把要输入的计算表达式用一个字符串记录下来,然后进行分割、数据类型转换。
我以前做过一个简单的计算器,代码如下:import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class calculatorTest extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JButton button_08;
private JButton button_09;
private JButton button_04;
private JButton button_05;
private JButton button_06;
private JButton button_01;
private JButton button_02;
private JButton button_03;
private JButton add;
private JButton subtract;
private JButton multiply;
private JButton button_11;
private JButton division;
private JButton result;
private JButton btnC;
private StringBuffer buffer1=new StringBuffer();//用于存储用户输入的计算表达式
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
calculatorTest frame = new calculatorTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public calculatorTest() {
setResizable(false);
setTitle("\u8BA1\u7B97\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 314, 325);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setEditable(false);
textField.setFont(new Font("宋体", Font.BOLD, 25));
textField.setBounds(0, 0, 308, 42);
contentPane.add(textField);
textField.setColumns(10);
JButton button_07 = new JButton("7");
button_07.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(7);//向记录运算表达式的字符串增加相应的字符
textField.setText(buffer1.toString());//向文本框表达所输入的内容
}
});
button_07.setFont(new Font("宋体", Font.BOLD, 12));
button_07.setBounds(10, 52, 60, 40);
contentPane.add(button_07);
button_08 = new JButton("8");
button_08.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(8);
textField.setText(buffer1.toString());
}
});
button_08.setFont(new Font("宋体", Font.BOLD, 12));
button_08.setBounds(80, 52, 60, 40);
contentPane.add(button_08);
button_09 = new JButton("9");
button_09.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(9);
textField.setText(buffer1.toString());
}
});
button_09.setFont(new Font("宋体", Font.BOLD, 12));
button_09.setBounds(160, 52, 60, 40);
contentPane.add(button_09);
button_04 = new JButton("4");
button_04.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(4);
textField.setText(buffer1.toString());
}
});
button_04.setFont(new Font("宋体", Font.BOLD, 12));
button_04.setBounds(10, 117, 60, 40);
contentPane.add(button_04);
button_05 = new JButton("5");
button_05.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(5);
textField.setText(buffer1.toString());
}
});
button_05.setFont(new Font("宋体", Font.BOLD, 12));
button_05.setBounds(80, 117, 60, 40);
contentPane.add(button_05);
button_06 = new JButton("6");
button_06.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(6);
textField.setText(buffer1.toString());
}
});
button_06.setFont(new Font("宋体", Font.BOLD, 12));
button_06.setBounds(160, 117, 60, 40);
contentPane.add(button_06);
button_01 = new JButton("1");
button_01.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(1);
textField.setText(buffer1.toString());
}
});
button_01.setFont(new Font("宋体", Font.BOLD, 12));
button_01.setBounds(10, 179, 60, 40);
contentPane.add(button_01);
button_02 = new JButton("2");
button_02.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(2);
textField.setText(buffer1.toString());
}
});
button_02.setFont(new Font("宋体", Font.BOLD, 12));
button_02.setBounds(80, 179, 60, 40);
contentPane.add(button_02);
button_03 = new JButton("3");
button_03.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(3);
textField.setText(buffer1.toString());
}
});
button_03.setFont(new Font("宋体", Font.BOLD, 12));
button_03.setBounds(160, 179, 60, 40);
contentPane.add(button_03);
add = new JButton("+");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(buffer1.toString());//将运算表达式转换成String类型
//对所输入的运算字符进行判断
if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
buffer1.append("+");
textField.setText(buffer1.toString());
}
else{}
}
});
add.setFont(new Font("宋体", Font.BOLD, 12));
add.setBounds(238, 52, 60, 40);
contentPane.add(add);
subtract = new JButton("-");
subtract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(buffer1.toString());
if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
buffer1.append("-");
textField.setText(buffer1.toString());
}
else{}
}
});
subtract.setFont(new Font("宋体", Font.BOLD, 12));
subtract.setBounds(238, 117, 60, 40);
contentPane.add(subtract);
multiply = new JButton("*");
multiply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(buffer1.toString());
if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
buffer1.append("*");
textField.setText(buffer1.toString());
}
else{}
}
});
multiply.setFont(new Font("宋体", Font.BOLD, 12));
multiply.setBounds(238, 179, 60, 40);
contentPane.add(multiply);
button_11 = new JButton("0");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buffer1.append(0);
textField.setText(buffer1.toString());
}
});
button_11.setFont(new Font("宋体", Font.BOLD, 12));
button_11.setBounds(10, 239, 60, 40);
contentPane.add(button_11);
division = new JButton("/");
division.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(buffer1.toString());
if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
buffer1.append("/");
textField.setText(buffer1.toString());
}
else{}
}
});
division.setFont(new Font("宋体", Font.BOLD, 12));
division.setBounds(238, 239, 60, 40);
contentPane.add(division);
result = new JButton("=");
result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=new String(buffer1.toString());
//对所输入的运算字符进行判断
if(buffer1.length()!=0&&(s.indexOf('+')==0||s.indexOf('-')==0||s.indexOf('*')==0||s.indexOf('/')==0||s.indexOf('=')<0)){
buffer1.append("=");
textField.setText(buffer1.toString());
String str=new String(buffer1.toString());
String str1[]=new String[2];
String str2[]=new String[2];
//对运算符进行判断,进行相应的运算
if(str.indexOf('+')>0){
//对运算符前面字符串和运算符后面的字符串进行分割
str1=str.split("\\+");
//保存运算符前面的内容并转换数据类型
double number1=Double.parseDouble(str1[0]);
str2=str1[1].split("\\=");
//对分割后的字符串将等于号运算符前面字符串和等于号运算符后面的字符串进行分割并进行数据类型转换
double number2=Double.parseDouble(str2[0]);
double number=number1+number2;
textField.setText(str+number);
//删除运算表达式的内容,以便下次运算
buffer1.delete(0, (buffer1.length()));
}
if(str.indexOf('-')>0){
str1=str.split("\\-");
double number1=Double.parseDouble(str1[0]);
str2=str1[1].split("\\=");
double number2=Double.parseDouble(str2[0]);
double number=number1-number2;
textField.setText(str+number);
buffer1.delete(0, (buffer1.length()));
}
if(str.indexOf('*')>0){
str1=str.split("\\*");
double number1=Double.parseDouble(str1[0]);
str2=str1[1].split("\\=");
double number2=Double.parseDouble(str2[0]);
double number=number1*number2;
textField.setText(str+number);
buffer1.delete(0, (buffer1.length()));
}
if(str.indexOf('/')>0){
str1=str.split("\\/");
double number1=Double.parseDouble(str1[0]);
str2=str1[1].split("\\=");
double number2=Double.parseDouble(str2[0]);
double number=number1/number2;
textField.setText(str+number);
buffer1.delete(0, (buffer1.length()));
}
}
else{}
}
});
result.setFont(new Font("宋体", Font.BOLD, 12));
result.setBounds(80, 239, 60, 40);
contentPane.add(result);
btnC = new JButton("C");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(buffer1.length()>0){
buffer1.deleteCharAt(buffer1.length()-1);
textField.setText(buffer1.toString());
}
else{
}
}
});
btnC.setFont(new Font("宋体", Font.BOLD, 12));
btnC.setBounds(160, 239, 60, 40);
contentPane.add(btnC);
}
}