JAVA设计计算器——加减乘除

来源:互联网 发布:淘宝发货默认快递公司 编辑:程序博客网 时间:2024/06/08 04:06

功能比较简单,未能实现科学计算器代码

import java.awt.BorderLayout;


public class GuiTest extends JFrame {


private JPanel contentPane;
private JTextField textField;
private JButton button_4;
private JButton button_6;
private JButton button_7;
private JButton button;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_10;
private JButton button_11;
private JButton button_12;
private JButton button_13;
private JButton button_14;
private JButton button_15;
private JButton button_16;
private JButton button_17;
private JButton button_18;
private JButton btnAc;
private JButton btnNewButton;
private JOptionPane jOptionPane;

int num1 = 0;
int num2 = 0;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiTest frame = new GuiTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public GuiTest() {
setTitle("\u8BA1\u7B97\u5668");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 297, 377);
contentPane = new JPanel();
contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
setContentPane(contentPane);

textField = new JTextField();
textField.setBackground(UIManager.getColor("Button.background"));
textField.setColumns(10);


button_4 = new JButton("0");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

textField.setText(textField.getText()+"0");
}
});
button_4.setForeground(Color.BLACK);
button_4.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_4.setBackground(Color.ORANGE);

button_6 = new JButton(".");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+".");
}
});
button_6.setForeground(Color.BLACK);
button_6.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_6.setBackground(Color.ORANGE);

button_7 = new JButton("/");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"/");
}
});
button_7.setForeground(Color.BLACK);
button_7.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_7.setBackground(Color.ORANGE);

button = new JButton("3");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"3");
}
});
button.setForeground(Color.BLACK);
button.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button.setBackground(Color.ORANGE);

button_1 = new JButton("2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"2");
num1 = 2;
num2 = 2;
}
});
button_1.setForeground(Color.BLACK);
button_1.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_1.setBackground(Color.ORANGE);

button_2 = new JButton("1");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+"1");
num1 = 1;
num2 = 2;
}
});
button_2.setForeground(Color.BLACK);
button_2.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_2.setBackground(Color.ORANGE);

button_3 = new JButton("*");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"*");
}
});
button_3.setForeground(Color.BLACK);
button_3.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_3.setBackground(Color.ORANGE);

button_10 = new JButton("6");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

textField.setText(textField.getText()+"6");
}
});
button_10.setForeground(Color.BLACK);
button_10.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_10.setBackground(Color.ORANGE);

button_11 = new JButton("5");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText()+"5");
}
});
button_11.setForeground(Color.BLACK);
button_11.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_11.setBackground(Color.ORANGE);

button_12 = new JButton("4");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText()+"4");
}
});
button_12.setForeground(Color.BLACK);
button_12.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_12.setBackground(Color.ORANGE);

button_13 = new JButton("-");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"-");
}
});
button_13.setForeground(Color.BLACK);
button_13.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_13.setBackground(Color.ORANGE);

button_14 = new JButton("\u2190");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

String strBackspace = textField.getText();
int indexBackspace = strBackspace.length();

if(indexBackspace == 0){

textField.setText(null);

}else{

String strBackspace1 = strBackspace.substring(0, indexBackspace-1);
textField.setText(strBackspace1);

}
}
});
button_14.setForeground(Color.BLACK);
button_14.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_14.setBackground(Color.ORANGE);

button_15 = new JButton("7");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

textField.setText(textField.getText()+"7");
}
});
button_15.setForeground(Color.BLACK);
button_15.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_15.setBackground(Color.ORANGE);

button_16 = new JButton("8");
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

textField.setText(textField.getText()+"8");
}
});
button_16.setForeground(Color.BLACK);
button_16.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_16.setBackground(Color.ORANGE);

button_17 = new JButton("9");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"9");
}
});
button_17.setForeground(Color.BLACK);
button_17.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_17.setBackground(Color.ORANGE);

button_18 = new JButton("+");
button_18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(textField.getText()+"+");
}
});
button_18.setForeground(Color.BLACK);
button_18.setFont(new Font("微软雅黑", Font.PLAIN, 8));
button_18.setBackground(Color.ORANGE);

btnAc = new JButton("AC");
btnAc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

textField.setText(textField.getText().replace(textField.getText(), "0"));

}
});
btnAc.setForeground(Color.BLACK);
btnAc.setFont(new Font("微软雅黑", Font.PLAIN, 8));
btnAc.setBackground(Color.ORANGE);

btnNewButton = new JButton("=");
btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 8));
btnNewButton.setBackground(Color.ORANGE);
btnNewButton.setForeground(Color.BLACK);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(10)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 259, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(12)
.addComponent(button_15)
.addGap(18)
.addComponent(button_16)
.addGap(18)
.addComponent(button_17)
.addGap(18)
.addComponent(button_18, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
.addGap(12)
.addComponent(btnAc))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(12)
.addComponent(button_10)
.addGap(18)
.addComponent(button_11)
.addGap(18)
.addComponent(button_12)
.addGap(18)
.addComponent(button_13, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(button_14, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(12)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button)
.addGap(18)
.addComponent(button_1))
.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_2)
.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_3)
.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
.addGap(10)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(24)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addGap(30)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_15, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_16, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_17, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_18, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(btnAc, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button_10, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_11, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_12, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_13, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_14, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(button, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)))
);
contentPane.setLayout(gl_contentPane);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {



//获取text字符串
String str = textField.getText();
int endIndex = str.length();



byte[] b1 = new byte[endIndex];
b1= str.getBytes();
int b1len = b1.length;
int b1str = 0; 

for(;b1str<b1len;b1str++){

if(b1[b1str] == '+'){

int index1 = str.indexOf("+");
String str1 = str.substring(0, index1);
//获取第二个数字
String str2 = str.substring(index1+1, endIndex);
if(str1.equals(null)||str2.equals(null)){

textField.setText(null);

}else{

int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
//加法计算
int sum = a + b;
//System.out.println(a+b);
//输出结果
textField.setText(Integer.toString(sum));
}


}
if(b1[b1str] == '-'){

int index3 = str.indexOf("-");
String str3 = str.substring(0, index3);
int c = Integer.parseInt(str3);

String str4 = str.substring(index3+1, endIndex);
int d = Integer.parseInt(str4);

int min = c - d;
//System.out.println(a+b);

textField.setText(Integer.toString(min));


}
if(b1[b1str] == '*'){

int index3 = str.indexOf("*");
String str3 = str.substring(0, index3);
int c = Integer.parseInt(str3);

String str4 = str.substring(index3+1, endIndex);
int d = Integer.parseInt(str4);

int plus = c * d;


textField.setText(Integer.toString(plus));


}
if(b1[b1str] == '/'){

int index3 = str.indexOf("/");
String str3 = str.substring(0, index3);
int c = Integer.parseInt(str3);

String str4 = str.substring(index3+1, endIndex);
int d = Integer.parseInt(str4);

int div = c / d;


textField.setText(Integer.toString(div));


}
// else if
}

// int i1 = 2;
// //运算分支选择
// switch(i1){
//
// //加法分支
// case 1:
// //获取第一个数字
// int index1 = str.indexOf("+");
// String str1 = str.substring(0, index1);
// int a = Integer.parseInt(str1);
// //获取第二个数字
// String str2 = str.substring(index1+1, endIndex);
// int b = Integer.parseInt(str2);
// //加法计算
// int sum = a + b;
// //System.out.println(a+b);
// //输出结果
// textField.setText(Integer.toString(sum));
//
// break;
// case 2:
// int index3 = str.indexOf("-");
// String str3 = str.substring(0, index3);
// int c = Integer.parseInt(str3);
//
// String str4 = str.substring(index3+1, endIndex);
// int d = Integer.parseInt(str4);
//
// int div = c - d;
// //System.out.println(a+b);
//
// textField.setText(Integer.toString(div));
//
// break;
//
//
//
// }
//


// byte[] b = new byte[str.length()];
// b = str.getBytes();
// int blen = b.length;
//
// int i = 0;
// for(i;i<blen;i++){
//
// if(b[i] == ){}
//
//
//
// }



//
// JOptionPane.showConfirmDialog(null, 
// "choose one", "choose one", JOptionPane.YES_NO_OPTION);
//


}
});
contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{button_15, textField, button_16, button_17, button_18, btnAc, button_10, button_11, button_12, button_13, button_14, button, button_1, button_2, button_3, btnNewButton, button_4, button_6, button_7}));
}
}