找简单的java写的加减乘除计算器代码
导入Java . awt . *;
导入Java . awt . event . *;
公共类计算器扩展JFrame实现ActionListener
{
私有布尔dotExist,operated,equaled//帮助操作的布尔变量
私有双storedNumber//当前结果
私有char lastOperator//表示前一个运算符。
私有JTextField操作;//结果列
private JButton dot,加号,减号,multi,div,sqrt,equal,changePN,clear//运算符
专用JButton[]号码;//数字
//生成器
公共计算器()
{
setTitle(“计算器”);
//初始化变量
dotExist = false//指示当前数字是否有小数点。
operated = false//指示是否按下了任何运算符。
等于=假;//指示是否按下了等号。
stored number = 0;
lastOperator = '?';
//初始化窗口变量
operation = new JTextField(" 0 ");
operation . set editable(false);
numbers = new JButton[10];
for(int I = 0;我& lt10;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(" = ");
change pn = new JButton(" ");
clear = new JButton(" AC ");
//将窗口对象放入窗口。
gridbag layout layout = new gridbag layout();
getContentPane()。setLayout(布局);
addComponent(布局,运算,0,0,4,1);
addComponent(布局,编号[1],1,0,1,1);
addComponent(布局,数字[2],1,1,1,1);
addComponent(布局,数字[3],1,2,1,1);
addComponent(布局,数字[4],2,0,1,1);
addComponent(布局,数字[5],2,1,1,1);
addComponent(布局,数字[6],2,2,1,1);
addComponent(布局,数字[7],3,0,1,1);
addComponent(布局,数字[8],3,1,1,1);
addComponent(布局,数字[9],3,2,1,1);
addComponent(layout,dot,4,0,1,1);
addComponent(布局,数字[0],4,1,1,1);
addComponent(layout,sqrt,4,2,1,1);
addComponent(布局,加号,1,3,1,1);
addComponent(布局,减,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);
}
//对按钮做出反应的方法
公共无效操作已执行(操作事件e)
{
JButton BTN =(JButton)e . getsource();
if (btn == clear)
{
operation . settext(" 0 ");
dotExist = false
stored number = 0;
lastOperator = '?';
}
else if (btn == equal)
{
操作(' = ');
等于=真;
}
else if (btn == plus)
{
操作('+');
等于=假;
}
else if (btn == minus)
{
操作('-');
等于=假;
}
else if (btn == multi)
{
操作(' * ');
等于=假;
}
else if (btn == div)
{
操作('/');
等于=假;
}
else if (btn == changePN)
{
操作(' p ');
操作(' = ');
等于=真;
}
else if (btn == sqrt)
{
操作(' s ');
操作(' = ');
等于=真;
}
其他
{
如果(等于)
stored number = 0;
for(int I = 0;我& lt10;i++)
if (btn == numbers[i])
{
if (operation.getText()。等于(" 0 "))
operation . settext("+I);
else if(!已操作)
operation . settext(operation . gettext()+I);
其他
{
operation . settext("+I);
operated = false
}
}
if(BTN = = dot & amp;& amp!dotExist)
{
operation . settext(operation . gettext()+"。");
dotExist = true
}
}
}
//操作方法
私有空操作(计费操作)
{
double current number = double . value of(operation . gettext())。double value();
if (lastOperator == '?')
storedNumber = currentNumber
else if (lastOperator == '+')
stored number+= current number;
else if (lastOperator == '-')
stored number-= current number;
else if (lastOperator == '* ')
storedNumber * = currentNumber
else if (lastOperator == '/')
stored number/= current number;
else if (lastOperator == 'p ')
stored number * =-1;
else if (lastOperator == 's ')
stored number = math . sqrt(current number);
else if(last operator = = ' = ' & amp;& amp等于)
storedNumber = currentNumber
operation . settext("+stored number);
operated = true
lastOperator =运算符;
}
//快速使用GridBagLayout的方法
private void add Component(gridbag layout布局,组件组件,整数行,整数列,整数宽度,整数高度)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints . fill = GridBagConstraints。两者都有;
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(组件,约束);
JButton的组件实例)
((JButton)组件)。addActionListener(this);
getContentPane()。添加(组件);
}
main方法初始化并显示窗口。
公共静态void main(String[] args)
{
Calculator calc =新计算器();
calc.setSize(290,400);
calc . set visible(true);
}
}