Java button control array to realize calculator interface example sharing
The idea is as follows:
Create a class and make it inherit the form class JFrame through extensions; Create a JFrame object and use the setvisible () method of JFrame class to set the form visible; In the constructor, use @ R_ 419_ 1371@ method inherits the construction method of the parent class; Use the settitle () method to set the title of the form; Use the setbounds () method to set the display position and size of the form; Use the setdefaultcloseoperation() method to set the action of the form close button to exit; Create a grid layout manager object using GridLayout; Use the sethgap () method of GridLayout class to set the horizontal spacing of components; Use the setvgap () method of GridLayout class to set the vertical spacing of components; Create JPanel container object; Set the container to adopt grid layout manager through setlayout () method of JPanel class; Create a string type two-dimensional array and initialize its value to the value displayed on the corresponding button on the calculator; Create a JButton type two-dimensional array and allocate space corresponding to the previous string type two-dimensional array; Traverse the string type two-dimensional array, assign each element to the corresponding button in the JButton type two-dimensional array, and add an event to each button so that the corresponding value is displayed in the text input box when the button is clicked. Finally, add the button to the panel using the add() method of JPanel class.