关于java Applet小程序
使用JavaApplet小程序,创建一个图型用户界面,1.能进行两个整型数为算子的加+、减-、乘*、除/、和取模%的运算,2.显示计算结果,3.能捕捉输入异常,并用JOp...
使用Java Applet小程序,创建一个图型用户界面,
1. 能进行两个整型数为算子的加 +、减 -、乘 *、除 /、和 取模 % 的运算,
2. 显示计算结果,
3. 能捕捉输入异常,并用 JOptionPane 类的 对话窗口显示异常
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
输入第一个整型
输入第二个整型
计算结果
选择运算符 加 减 乘 除 模
数字字符串异常时的提示语句为必须输入数字字符串。必须输入两个整形数 展开
1. 能进行两个整型数为算子的加 +、减 -、乘 *、除 /、和 取模 % 的运算,
2. 显示计算结果,
3. 能捕捉输入异常,并用 JOptionPane 类的 对话窗口显示异常
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
输入第一个整型
输入第二个整型
计算结果
选择运算符 加 减 乘 除 模
数字字符串异常时的提示语句为必须输入数字字符串。必须输入两个整形数 展开
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator()
{
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
如果你想加sin cos tan的话就建它们的按钮 在actionPerformed方法中的if-else语句中加else if (btn == sin){operate('S');operate('=');equaled = true;}然后在operate方法加对应的语句就行了.记忆M+, M-没时间搞 这个应该比较简单容易明白吧.
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator()
{
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
如果你想加sin cos tan的话就建它们的按钮 在actionPerformed方法中的if-else语句中加else if (btn == sin){operate('S');operate('=');equaled = true;}然后在operate方法加对应的语句就行了.记忆M+, M-没时间搞 这个应该比较简单容易明白吧.
展开全部
有个awt的,修改一下就行了,跟Applet差不多
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
public class Calculator extends Frame implements ActionListener //计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint,positiveminus,backspace,reciprocal,equal,clear; //声明成员变量
//小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[]={"+","-","*","/"};
LinkedList linklist;
boolean pressequal=false;
public Calculator() //构造方法
{
super("计算器");
linklist=new LinkedList();
numberButton=new NumberButton[10];
for(int i=0;i<=9;i++)
{
numberButton[i]=new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton=new OperatorButton[4];
for(int i=0;i<4;i++)
{
operatorButton[i]=new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint=new Button(".");
positiveminus=new Button("+/-");
backspace=new Button("CE");
reciprocal=new Button("1/x");
equal=new Button("=");
clear=new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result=new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman",Font.PLAIN,14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel=new Panel();
panel.setLayout(new GridLayout(4,5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result,"North");
add(panel,"Center");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(270,200);
setLocation(300,230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
{
if(e.getSource() instanceof NumberButton) //数字按钮
{
NumberButton b=(NumberButton)e.getSource();
if(linklist.size()==0)
{
int number=b.getNumber();
linklist.add(""+number);
result.setText(""+number);
pressequal=false;
}
else if(linklist.size()==1&&pressequal==false)
{
int number=b.getNumber();
String num=(String)linklist.getFirst();
String s=num.concat(""+number);
linklist.set(0,s);
result.setText(s);
}
else if(linklist.size()==1&&pressequal==true)
{
int number=b.getNumber();
linklist.removeFirst();
linklist.add(""+number);
pressequal=false;
result.setText(""+number);
}
else if(linklist.size()==2)
{
int number=b.getNumber();
linklist.add(""+number);
result.setText(""+number);
}
else if(linklist.size()==3)
{
int number=b.getNumber();
String num=(String)linklist.getLast();
String s=num.concat(""+number);
linklist.set(2,s);
result.setText(s);
}
}
else if(e.getSource() instanceof OperatorButton) //操作按钮
{
OperatorButton b=(OperatorButton)e.getSource();
if(linklist.size()==1)
{
String fuhao=b.getOperator();
linklist.add(fuhao);
}
else if(linklist.size()==2)
{
String fuhao=b.getOperator();
linklist.set(1,fuhao);
}
else if(linklist.size()==3)
{
String fuhao=b.getOperator();
String number1=(String)linklist.getFirst();
String number2=(String)linklist.getLast();
String operator=(String)linklist.get(1);
try
{
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(operator.equals("+"))
{
n=n1+n2;
}
else if(operator.equals("-"))
{
n=n1-n2;
}
else if(operator.equals("*"))
{
n=n1*n2;
}
else if(operator.equals("/"))
{
n=n1/n2;
}
linklist.clear();
linklist.add(""+n);
linklist.add(fuhao);
result.setText(""+n);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==equal) //等号按钮
{
pressequal=true;
if(linklist.size()==1||linklist.size()==2)
{
String num=(String)linklist.getFirst();
result.setText(""+num);
}
else if(linklist.size()==3)
{
String number1=(String)linklist.getFirst();
String number2=(String)linklist.getLast();
String operator=(String)linklist.get(1);
try
{
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(operator.equals("+"))
{
n=n1+n2;
}
else if(operator.equals("-"))
{
n=n1-n2;
}
else if(operator.equals("*"))
{
n=n1*n2;
}
else if(operator.equals("/"))
{
n=n1/n2;
}
result.setText(""+n);
linklist.set(0,""+n);
linklist.removeLast();
linklist.removeLast();
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==radixpoint) //小数点按钮
{
if(linklist.size()==0)
{
pressequal=false;
}
else if(linklist.size()==1)
{
String dot=radixpoint.getLabel();
String num=(String)linklist.getFirst();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
linklist.set(0,s);
}
else
{
s=num;
}
linklist.set(0,s);
result.setText(s);
}
else if(linklist.size()==3)
{
String dot=radixpoint.getLabel();
String num=(String)linklist.getLast();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
linklist.set(2,s);
}
else
{
s=num;
}
result.setText(s);
}
}
else if(e.getSource()==backspace) //退格按钮
{
if(linklist.size()==1)
{
String num=(String)linklist.getFirst();
if(num.length()>=1)
{
num=num.substring(0,num.length()-1);
linklist.set(0,num);
result.setText(num);
}
else
{
linklist.removeLast();
result.setText("0");
}
}
else if(linklist.size()==3)
{
String num=(String)linklist.getLast();
if(num.length()>=1)
{
num=num.substring(0,num.length()-1);
linklist.set(2,num);
result.setText(num);
}
else
{
linklist.removeLast();
result.setText("0");
}
}
}
else if(e.getSource()==positiveminus) //正负号按钮
{
if(linklist.size()==1)
{
String number1=(String)linklist.getFirst();
try
{
double d=Double.parseDouble(number1);
d=-1*d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
else if(linklist.size()==3)
{
String number2=(String)linklist.getLast();
try
{
double d=Double.parseDouble(number2);
d=-1*d;
String str=String.valueOf(d);
linklist.set(2,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==reciprocal) //求倒数按钮
{
if(linklist.size()==1||linklist.size()==2)
{
String number1=(String)linklist.getFirst();
try
{
double d=Double.parseDouble(number1);
d=1.0/d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
else if(linklist.size()==3)
{
String number2=(String)linklist.getLast();
try
{
double d=Double.parseDouble(number2);
d=1.0/d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==clear) //清零按钮
{
pressequal=false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[])
{
new Calculator();
}
}
class NumberButton extends Button //数字按钮类
{
int number;
public NumberButton(int number) //构造方法
{
super(""+number);
this.number=number;
setForeground(Color.blue);
}
public int getNumber()
{
return number;
}
}
class OperatorButton extends Button //运算符号按钮类
{
String operator;
public OperatorButton(String operator) //构造方法
{
super(operator);
this.operator=operator;
setForeground(Color.red);
}
public String getOperator()
{
return operator;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
public class Calculator extends Frame implements ActionListener //计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint,positiveminus,backspace,reciprocal,equal,clear; //声明成员变量
//小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[]={"+","-","*","/"};
LinkedList linklist;
boolean pressequal=false;
public Calculator() //构造方法
{
super("计算器");
linklist=new LinkedList();
numberButton=new NumberButton[10];
for(int i=0;i<=9;i++)
{
numberButton[i]=new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton=new OperatorButton[4];
for(int i=0;i<4;i++)
{
operatorButton[i]=new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint=new Button(".");
positiveminus=new Button("+/-");
backspace=new Button("CE");
reciprocal=new Button("1/x");
equal=new Button("=");
clear=new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result=new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman",Font.PLAIN,14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel=new Panel();
panel.setLayout(new GridLayout(4,5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result,"North");
add(panel,"Center");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(270,200);
setLocation(300,230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
{
if(e.getSource() instanceof NumberButton) //数字按钮
{
NumberButton b=(NumberButton)e.getSource();
if(linklist.size()==0)
{
int number=b.getNumber();
linklist.add(""+number);
result.setText(""+number);
pressequal=false;
}
else if(linklist.size()==1&&pressequal==false)
{
int number=b.getNumber();
String num=(String)linklist.getFirst();
String s=num.concat(""+number);
linklist.set(0,s);
result.setText(s);
}
else if(linklist.size()==1&&pressequal==true)
{
int number=b.getNumber();
linklist.removeFirst();
linklist.add(""+number);
pressequal=false;
result.setText(""+number);
}
else if(linklist.size()==2)
{
int number=b.getNumber();
linklist.add(""+number);
result.setText(""+number);
}
else if(linklist.size()==3)
{
int number=b.getNumber();
String num=(String)linklist.getLast();
String s=num.concat(""+number);
linklist.set(2,s);
result.setText(s);
}
}
else if(e.getSource() instanceof OperatorButton) //操作按钮
{
OperatorButton b=(OperatorButton)e.getSource();
if(linklist.size()==1)
{
String fuhao=b.getOperator();
linklist.add(fuhao);
}
else if(linklist.size()==2)
{
String fuhao=b.getOperator();
linklist.set(1,fuhao);
}
else if(linklist.size()==3)
{
String fuhao=b.getOperator();
String number1=(String)linklist.getFirst();
String number2=(String)linklist.getLast();
String operator=(String)linklist.get(1);
try
{
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(operator.equals("+"))
{
n=n1+n2;
}
else if(operator.equals("-"))
{
n=n1-n2;
}
else if(operator.equals("*"))
{
n=n1*n2;
}
else if(operator.equals("/"))
{
n=n1/n2;
}
linklist.clear();
linklist.add(""+n);
linklist.add(fuhao);
result.setText(""+n);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==equal) //等号按钮
{
pressequal=true;
if(linklist.size()==1||linklist.size()==2)
{
String num=(String)linklist.getFirst();
result.setText(""+num);
}
else if(linklist.size()==3)
{
String number1=(String)linklist.getFirst();
String number2=(String)linklist.getLast();
String operator=(String)linklist.get(1);
try
{
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(operator.equals("+"))
{
n=n1+n2;
}
else if(operator.equals("-"))
{
n=n1-n2;
}
else if(operator.equals("*"))
{
n=n1*n2;
}
else if(operator.equals("/"))
{
n=n1/n2;
}
result.setText(""+n);
linklist.set(0,""+n);
linklist.removeLast();
linklist.removeLast();
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==radixpoint) //小数点按钮
{
if(linklist.size()==0)
{
pressequal=false;
}
else if(linklist.size()==1)
{
String dot=radixpoint.getLabel();
String num=(String)linklist.getFirst();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
linklist.set(0,s);
}
else
{
s=num;
}
linklist.set(0,s);
result.setText(s);
}
else if(linklist.size()==3)
{
String dot=radixpoint.getLabel();
String num=(String)linklist.getLast();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
linklist.set(2,s);
}
else
{
s=num;
}
result.setText(s);
}
}
else if(e.getSource()==backspace) //退格按钮
{
if(linklist.size()==1)
{
String num=(String)linklist.getFirst();
if(num.length()>=1)
{
num=num.substring(0,num.length()-1);
linklist.set(0,num);
result.setText(num);
}
else
{
linklist.removeLast();
result.setText("0");
}
}
else if(linklist.size()==3)
{
String num=(String)linklist.getLast();
if(num.length()>=1)
{
num=num.substring(0,num.length()-1);
linklist.set(2,num);
result.setText(num);
}
else
{
linklist.removeLast();
result.setText("0");
}
}
}
else if(e.getSource()==positiveminus) //正负号按钮
{
if(linklist.size()==1)
{
String number1=(String)linklist.getFirst();
try
{
double d=Double.parseDouble(number1);
d=-1*d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
else if(linklist.size()==3)
{
String number2=(String)linklist.getLast();
try
{
double d=Double.parseDouble(number2);
d=-1*d;
String str=String.valueOf(d);
linklist.set(2,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==reciprocal) //求倒数按钮
{
if(linklist.size()==1||linklist.size()==2)
{
String number1=(String)linklist.getFirst();
try
{
double d=Double.parseDouble(number1);
d=1.0/d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
else if(linklist.size()==3)
{
String number2=(String)linklist.getLast();
try
{
double d=Double.parseDouble(number2);
d=1.0/d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==clear) //清零按钮
{
pressequal=false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[])
{
new Calculator();
}
}
class NumberButton extends Button //数字按钮类
{
int number;
public NumberButton(int number) //构造方法
{
super(""+number);
this.number=number;
setForeground(Color.blue);
}
public int getNumber()
{
return number;
}
}
class OperatorButton extends Button //运算符号按钮类
{
String operator;
public OperatorButton(String operator) //构造方法
{
super(operator);
this.operator=operator;
setForeground(Color.red);
}
public String getOperator()
{
return operator;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询