導入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);
}
}
如果要添加sin cos tan,構建它們的按鈕,在actionPerformed方法的if-else語句中添加else if(BTN = = sin){ operate(' S ');操作(' = ');等於=真;}然後將相應的語句添加到operate方法中。應該更容易記住M+,M-沒時間做這個。