Calculator written in Java, code shows!

Calculator written in Java, code shows!
java.awt.BorderLayout The Import;
Import java.awt.Dimension;
Import java.awt.GridLayout;
Import java.awt.event.ActionEvent The;
Import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class ButtonArrayExample extends JFrame {// form class inherits the JFrame
/ **

  • */
    private static final long serialVersionUID = 6626440733001287873L;
    private JTextField textField;

    static void main public (String args []) {
    the try {
    the UIManager.setLookAndFeel ( "");
    } the catch (the Throwable E) {
    e.printStackTrace ();
    }
    ButtonArrayExample new new ButtonArrayExample Frame = ();
    frame.setVisible (to true); // set form visible, default is not visible
    }

    ButtonArrayExample public () {
    Super (); // inherited parent class constructor of
    the BorderLayout borderLayout = (the BorderLayout) the getContentPane () getLayout ();.
    borderLayout.setHgap (20 is);
    borderLayout.setVgap (10);
    setTitle ( "Calculator "); // set the title of the form
    setBounds (100, 100, 290, 282); // set the form and size of the display position
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); // Close button operation form is set to exit
    textField the JTextField new new = ();
    textField.setHorizontalAlignment (SwingConstants.TRAILING);
    textfield.setPreferredSize (new new the Dimension (12 is, 50));
    the getContentPane () the Add (the textField, BorderLayout.NORTH);.
    textField.setColumns (10);
    Final GridLayout gridLayout = new GridLayout (4, 0 ); // Create a grid layout manager object
    gridLayout.setHgap (5); // set the horizontal spacing component
    gridLayout.setVgap (5); // set vertical gap assembly
    JPanel panel = new JPanel (); // get the container object
    panel.setLayout (gridLayout); // set container using a grid layout manager
    getContentPane () add (. Panel, BorderLayout.CENTER);
    String [] [] = {{names ". 1", "2", ". 3", "+"}, { ". 4", ". 5", ". 6", "-"} , { ". 7", ". 8", ". 9", "×"}, { ".", "0", "=", "÷"}};
    the JButton [] [] Buttons = new new the JButton [. 4] [. 4];
    for (int Row = 0; Row <of names.length; Row ++) {
    for (int COL = 0; COL <of names.length; COL ++) {
    Buttons [Row] [COL] = new new the JButton (names [Row ] [col]); // Create button object
    buttons [row] [col] .addActionListener (new ActionListener () {

                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton button = (JButton) e.getSource();
                    String text = textField.getText();
                    textField.setText(text + button.getText());
                }
            });
            panel.add(buttons[row][col]); // 将按钮添加到面板中
        }
    }

    }

}
Like this article can follow me, I will continue to update, update your concerns are my motivation! For more java learning materials may also private letter I!
I wish my people are concerned about: good health, plenty of money, welfare, such as the East China Sea, many happy returns, Taoyuan County, from not afford to send!

Guess you like

Origin blog.51cto.com/14623707/2472288