자바 GUI 는 간단 한 계산 기 를 만든다.
26052 단어 자바
package calculator;
/* :calculator
:
:1.0 Bill 2015 11 28
,
2.0 Bill 2015 12 2
,
2.1 Bill 2015 12 5
, ,
*/
import java.awt.*; //
import java.awt.event.*; //
import javax.swing.*; //gui
import java.io.*; //
public class calculator //implements ActionListener
implements ActionListener
{
JFrame frame;
JButton btn[]=new JButton[12]; //12
JButton add,minus,plus,divide,clean,back,sqrt,equal; // +,-,*,/, , , ,
double x,y; //
int z; //
int z2;
JTextField field;
StringBuffer str;
public static void main(String[] args)
{
calculator cal=new calculator();
}
public calculator()
{
frame=new JFrame(" ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // gui
frame.getContentPane(); //
JPanel shangBufen=new JPanel(); //
JPanel xiaBufen=new JPanel(); //
//
str=new StringBuffer();
//
field=new JTextField(30); //20 ( )
field.setText("0");
field.setHorizontalAlignment(JTextField.RIGHT); //
field.setEditable(false);
shangBufen.add(field);
shangBufen.setLayout(new GridLayout(2,1)); //
//0~9
String s;
for(int i=0;i<10;i++)
{
s=""+i;
btn[i]=new JButton(s);
btn[i].addActionListener(this);
}
btn[10]=new JButton("-/+");
btn[10].addActionListener(this);
btn[11]=new JButton(".");
btn[11].addActionListener(this);
add=new JButton("+");minus=new JButton("-");
add.addActionListener(this);minus.addActionListener(this);
plus=new JButton("*");divide=new JButton("/");
plus.addActionListener(this);divide.addActionListener(this);
clean=new JButton("C");back=new JButton("Back");
clean.addActionListener(this);back.addActionListener(this);
sqrt=new JButton("Sqrt");equal=new JButton("=");
sqrt.addActionListener(this);equal.addActionListener(this);
xiaBufen.add(btn[7]); xiaBufen.add(btn[8]); xiaBufen.add(btn[9]); xiaBufen.add(add); xiaBufen.add(minus);
xiaBufen.add(btn[4]); xiaBufen.add(btn[5]); xiaBufen.add(btn[6]); xiaBufen.add(plus); xiaBufen.add(divide);
xiaBufen.add(btn[1]); xiaBufen.add(btn[2]);xiaBufen.add(btn[3]); xiaBufen.add(clean); xiaBufen.add(back);
xiaBufen.add(btn[0]); xiaBufen.add(btn[10]); xiaBufen.add(btn[11]);xiaBufen.add(sqrt);xiaBufen.add(equal);
xiaBufen.setLayout(new GridLayout(4,5,5,5)); // , 5
frame.add(shangBufen);
frame.add(xiaBufen);
frame.setLayout(new FlowLayout()); //
frame.setSize(400,300);
frame.setResizable(false); //
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) //
{
try
{
if(event.getSource()==clean) //
{
field.setText("0"); //
field.setHorizontalAlignment(JTextField.RIGHT); //
str.setLength(0); //
z=0;z2=0;
}
else if(event.getSource()==back) //
{
if(!field.getText().trim().equals("0")) // 0,trim()
{
if(str.length()!=1)
{
field.setText(str.delete(str.length()-1,str.length()).toString());// ,
field.setHorizontalAlignment(JTextField.RIGHT);
}
else
{
field.setText("0");
field.setHorizontalAlignment(JTextField.RIGHT);
str.setLength(0);
}
}
}
else if(event.getSource()==btn[10]) //
{
x=Double.parseDouble(field.getText().trim());
x=-x;
field.setText(""+x);
field.setHorizontalAlignment(JTextField.RIGHT);
}
else if(event.getSource()==btn[11]) //
{
//
if(field.getText().trim().indexOf(".")==-1) // -1
{
field.setText(str.append(event.getActionCommand()).toString()); //getActionCommmand()
field.setHorizontalAlignment(JTextField.RIGHT);
}
}else if(event.getSource()==sqrt) //
{
x=Double.parseDouble(field.getText().trim());
if(x>=0)
{
x=Math.sqrt(x);
field.setText(""+x);
field.setHorizontalAlignment(JTextField.RIGHT);
}
y=0d;
str.setLength(0);
str.append(""+x);
}else if(event.getSource()==add) //"+"
{
if(z!=0)
{
y=Double.parseDouble(field.getText().trim());
str.setLength(0);
switch(z)
{case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 0:break;
}
}else
{field.setText(""+0);}
y=0d;
z=1;z2=1;
}else if(event.getSource()==minus) //“-”
{
if(z!=0)
{
y=Double.parseDouble(field.getText().trim());
str.setLength(0);
switch(z)
{case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 0:break;
}
}else
{field.setText(""+0);}
y=0d;
z=2;z2=1;
}else if(event.getSource()==plus) //“*”
{
if(z!=0)
{
y=Double.parseDouble(field.getText().trim());
str.setLength(0);
switch(z)
{case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 0:break;
}
}else
{field.setText(""+0);}
y=0d;
z=3;z2=1;
}else if(event.getSource()==divide) //“/”
{
if(z!=0)
{
y=Double.parseDouble(field.getText().trim());
str.setLength(0);
switch(z)
{case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 0:break;
}
}else
{field.setText(""+0);}
y=0d;
z=4;z2=1;
}else if(event.getSource()==equal)
{
y=Double.parseDouble(field.getText().trim());
str.setLength(0);
switch(z)
{case 1:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 2:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 3:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 4:field.setText(str.append(""+(x+y)).toString());field.setHorizontalAlignment(JTextField.RIGHT);break;
case 0:break;
}
z=0;z2=0;
}else if(event.getSource()==btn[0])
{
if(!field.getText().trim().equals("0"))
{
field.setText(str.append(event.getActionCommand()).toString()); //getActionCommmand()
field.setHorizontalAlignment(JTextField.RIGHT);
}
}else //
{ if(z!=0)
{
if(z2==1)
{
str.setLength(0);
field.setText(str.append(event.getActionCommand()).toString()); //getActionCommmand()
field.setHorizontalAlignment(JTextField.RIGHT);
x=Double.parseDouble(field.getText().trim());
z2=0;
}else
{
field.setText(str.append(event.getActionCommand()).toString()); //getActionCommmand()
field.setHorizontalAlignment(JTextField.RIGHT);
x=Double.parseDouble(field.getText().trim());
}
}else
{
field.setText(str.append(event.getActionCommand()).toString()); //getActionCommmand()
field.setHorizontalAlignment(JTextField.RIGHT);
x=Double.parseDouble(field.getText().trim());
}
}
}catch(NumberFormatException e1) //
{e1.printStackTrace();}
catch(StringIndexOutOfBoundsException e2) //
{e2.printStackTrace();}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.