1. Preface
1) This calculator is written based on the graphical interface AWT in Java language. Actually, I think it doesn’t matter what function it implements when writing this calculator. The market does not lack this calculator, but rather uses the operation and use of simple controls in AWT. By connecting and familiarizing yourself with the basics of AWT, laying the foundation for in-depth learning. This calculator also has a very simple function and is easy to use. However, if you are interested, you can continue to add functions and improve operations.
2) Usage platform: Mainly using Myeclipse2014
3) Main knowledge points: Java basics; awt+swing
2. Function implementation and code display
1) Mainly it is the exercise of layout layout, the use of artboard text boxes, and the registration and monitoring of events of the control button. Because the function is also very simple, the code is abbreviated into the same class. The basic four operations are realized.
2) The code is as follows:
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; public class JiSuanJi { private JFrame frame; //Declare the relevant layout panel private JPanel panelwest; private JPanel panelcenter; private JPanel paneleas; private TextField tf; private JButton buttonzero; //Declare button control private JButton buttondot; private JButton buttonqual; private JButton buttonplus, buttonminus, buttonmultiple, buttondevision, buttonsin, buttontozero; private JButton buttonone,buttontwo,buttonthree,buttonfour,buttonfive,buttonsix,buttonseven,buttonight,buttonnine; private ButtonListener listener; //Declare the listening event public static void main(String args[]){ new JiSuanJi(); } public JiSuanJi(){ initView(); //Instantiate the related object setCenter(); //Layout add controls and related controls to handle setWest(); setEast(); addListener(); //Set listening setFrame(); //Set layout and display processing for frame} class ButtonListener implements ActionListener{ int biiaozhi=0; //Initialize the related variable double flag1=0,flag2=0,flag3=0; //@Override public void actionPerformed(ActionEvent e){ //(1) Through e.getSource(), get the button source of the click event and make corresponding processing if(e.getSource()==buttondot){ tf.setText("0."); } if(e.getSource()==buttontozero){ tf.setText(""); } if(e.getSource()==buttonzero){ tf.setText(tf.getText()+"0"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonone){ tf.setText(tf.getText()+"1");//The text box displays 1, 2, 3. . flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttontwo){ tf.setText(tf.getText()+"2"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonthree){ tf.setText(tf.getText()+"3"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonfour){ tf.setText(tf.getText()+"4"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonfive){ tf.setText(tf.getText()+"5"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonsix){ tf.setText(tf.getText()+"6"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonsix){ tf.setText(tf.getText()+"6"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonseven){ tf.setText(tf.getText()+"7"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttoneight){ tf.setText(tf.getText()+"8"); flag1=Double.parseDouble(tf.getText()); } else if(e.getSource()==buttonnine){ tf.setText(tf.getText()+"9"); flag1=Double.parseDouble(tf.getText()); } if(e.getSource()==buttonplus){ tf.setText(""); flag2=flag1; biozhi=0; } if(e.getSource()==buttonminus){ tf.setText(""); flag2=flag1; biozhi=1; } if(e.getSource()==buttonmultiple){ tf.setText(""); flag2=flag1; biozhi=2; } if(e.getSource()==buttondevision){ tf.setText(""); flag2=flag1; biozhi=3; } if(e.getSource()==buttonsin){ flag3=Math.sin(flag1); tf.setText(flag3+""); } if(e.getSource()==buttonequal){ if(biaozhi==0){ flag3=flag2+flag1; } if(biaozhi==1){ flag3=flag2-flag1; } if(biaozhi==2){ flag3=flag2*flag1; } if(biaozhi==3){ flag3=flag2/flag1; } tf.setText(flag3+""); } } } private void initView(){ /** * Create frame, small container object, button object*/ tf = new TextField(30);//Initialize interface width frame = new JFrame("Simple Computer"); panelcenter = new JPanel(); panelwest = new JPanel(); paneleast = new JPanel(); listener = new ButtonListener(); //Instantiate the listening object} private void setCenter(){ //(1) Initialize the display value of the control buttonone=new JButton("1"); buttontwo=new JButton("2"); buttonthree=new JButton("3"); buttonfour=new JButton("4"); buttonfive=new JButton("5"); buttonsix=new JButton("6"); buttonseven=new JButton("7"); buttonneight=new JButton("8"); buttonnine=new JButton("9"); //(2) Set layout style panelcenter.setLayout(new GridLayout(3,3)); // Layout according to the content//(3) Add control buttons panelcenter.add(buttonone); panelcenter.add(buttontwo); panelcenter.add(buttonthree); panelcenter.add(buttonfour); panelcenter.add(buttonfive); panelcenter.add(buttonsix); panelcenter.add(buttonseven); panelcenter.add(buttonneight); panelcenter.add(buttonnine); } private void setEast(){ //(1) Set the control display symbol buttonplus=new JButton("+"); buttonminus=new JButton("-"); buttonmultiple=new JButton("*"); buttondevision=new JButton("/"); buttonsin=new JButton("sin"); buttontozero=new JButton("clear"); //(2) Set the layout style paneleasst.setLayout(new GridLayout(3,2)); //(3) Add the corresponding button control paneleasst.add(buttonplus); paneleasst.add(buttonminus); paneleasst.add(buttonmultiple); paneleasst.add(buttondevision); paneleasst.add(buttondevision); paneleasst.add(buttonsin); paneleast.add(buttontozero); } private void setWest(){ //(1) Initialize the control display value buttonzero=new JButton("0"); buttondot=new JButton("."); buttonqual = new JButton("="); //(2) Set layout for these three buttons panelwest.setLayout(new GridLayout(3,1)); //(3) Add button control on the left layout, add three buttons into the layout panelwest.add(buttonzero); panelwest.add(buttondot); panelwest.add(buttonqual); } private void addListener(){ //(1) Add listening buttons for the buttons corresponding to 1-9.addActionListener(listener); buttontwo.addActionListener(listener); buttonthree.addActionListener(listener); buttonfour.addActionListener(listener); buttonfive.addActionListener(listener); buttonsix.addActionListener(listener); buttonseven.addActionListener(listener); buttonneight.addActionListener(listener); buttonneight.addActionListener(listener); buttonnine.addActionListener(listener); //(2) Add listening buttonplus.addActionListener(listener); buttonminus.addActionListener(listener); buttonmultiple.addActionListener(listener); buttondevision.addActionListener(listener); buttonsin.addActionListener(listener); buttontozero.addActionListener(listener); buttontozero.addActionListener(listener); //(3) Listen buttonzero.addActionListener(listener); buttonzero.addActionListener(listener); buttondot.addActionListener(listener); buttonqual.addActionListener(listener); } private void setFrame(){ frame.setLayout(new BorderLayout()); //Add content from different directions prepared to large frame frame.add(paneleast,"East"); frame.add(tf,BorderLayout.NORTH); frame.add(panelwest,BorderLayout.WEST); frame.add(panelcenter,BorderLayout.CENTER); //Set the color tf.setBackground(Color.green); frame.pack(); //Expand the frame frame.setLocation(500,500); //The program frame is in the screen position frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit the process, without adding this sentence, close the page, but the program process is still running frame.setVisible(true); //A graphical interface is invisible by default, setVisible sets the graphical interface to visible} }Code testing is available.
3. Demonstration and effects
1) The running interface is as follows:
Of course, the size and position of the graphical interface can be adjusted, and its position and size can also be initialized.
2) Simple test four operations: 11*12
It seems that simple can't be simpler. This calculator is written down to focus on the layout connection with the simple use of buttons .
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.