由于上学的安卓结课任务没有认真完成(做一个计算器),所以在java学习过程中我想做一个计算器。我完成的功能十分简单,只有简单的加减乘除,欢迎各位指出问题一起来改进。 设计思路很简单:创建面板,添加按钮,实现按钮即可。至此,我还是停留在基础的Swing程序设计中,再加上上次的五子棋,我对这一方面有了更深入的了解,掌握了对其简单的应用,在未来我也会遇到更多的问题,我也会学到更多。
下面是计算器的源代码,写的不规范请谅解,也欢迎指出! <img src="https://img-blog.csdn.net/20150330210642879?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHZsaXRj/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="450" height="250" alt="" />
</pre><pre name="code" class="java">
<pre name="code" class="java">import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener { //计算器类
boolean a=true;
JTextArea dispresult=new JTextArea("0.");
JButton buttons[]=new JButton[16];
JButton reset = new JButton("CE");
double result=0;
char symbol='\0';
public Calculator(){ //构造一个计算器
dispresult.setEditable(false);
Font font=new Font("Default", Font.PLAIN, 30);
dispresult.setFont(font);
JPanel resultField=new JPanel(new BorderLayout());
JPanel button=new JPanel();
String s[]={ "7", "8", "9", "/", "4", "5", "6", "*", "1",
"2", "3", "-", ".", "0", "=", "+" };
for(int i=0;i<s.length;i++){
buttons[i] = new JButton(s[i]);
button.add(buttons[i]); //将按钮添加到button面板
}
for(int i=0;i<s.length;i++){ //添加监听器
buttons[i].addActionListener(this);
}
reset.addActionListener(this);
resultField.add("Center",dispresult);
resultField.add("East",reset);
button.setLayout(new GridLayout(4,4,3,3)); //网格布局
getContentPane().add(button,BorderLayout.SOUTH);
getContentPane().add(resultField);
setBounds(200,200,500,200);
setVisible(true);
}
public void pressNumber(String s){ //文本域显示按下的数字
if(a){
dispresult.setText(s);
a=false;
}
else dispresult.append(s);
}
public static void main(String[] args){
new Calculator();
}
@Override
public void actionPerformed(ActionEvent e) { //按钮接口的实现
// TODO Auto-generated method stub
Object target=e.getSource();
if(target==reset){
dispresult.setText("0.");
}
if(target==buttons[0]){
pressNumber("7");
}
if(target==buttons[1]){
pressNumber("8");
}
if(target==buttons[2]){
pressNumber("9");
}
if(target==buttons[3]){
symbol='/';
a=true;
result=Double.parseDouble(dispresult.getText());
}
if(target==buttons[4]){
pressNumber("4");
}
if(target==buttons[5]){
pressNumber("5");
}
if(target==buttons[6]){
pressNumber("6");
}
if(target==buttons[7]){
symbol='*';
a=true;
result=Double.parseDouble(dispresult.getText());
}
if(target==buttons[8]){
pressNumber("1");
}
if(target==buttons[9]){
pressNumber("2");
}
if(target==buttons[10]){
pressNumber("3");
}
if(target==buttons[11]){
symbol='-';
a=true;
result=Double.parseDouble(dispresult.getText());
}
if(target==buttons[12]){
pressNumber(".");
}
if(target==buttons[13]){
pressNumber("0");
}
if(target==buttons[14]){
if(symbol=='+'){
result+=Double.parseDouble(dispresult.getText());
dispresult.setText("");
pressNumber(String.valueOf(result));
a=true;
}
else if(symbol=='-'){
result-=Double.parseDouble(dispresult.getText());
dispresult.setText("");
pressNumber(String.valueOf(result));
a=true;
}
else if(symbol=='*'){
result*=Double.parseDouble(dispresult.getText());
dispresult.setText("");
pressNumber(String.valueOf(result));
a=true;
}
else if(symbol=='/'){
if(Double.parseDouble(dispresult.getText())==0){
dispresult.setText("error");
a=true;
}
result/=Double.parseDouble(dispresult.getText());
dispresult.setText("");
pressNumber(String.valueOf(result));
a=true;
}
}
if(target==buttons[15]){
symbol='+';
a=true;
result=Double.parseDouble(dispresult.getText());
}
}
}