The code is as follows:
Import Java.awt.*;import javax.swing.*;p ublic class Gridframe extends JFrame {//define string array, assign a value to the display text of the button string str[] = {"MC", "MR", "MS", "m+", "←", "C", "%", "π", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};p UBL IC Gridframe () {//define panel and set to Grid layout, 4 rows 4 columns, component horizontal, vertical spacing is 7JPanel pan = new JPanel ();p an.setlayout (New GridLayout (6, 4, 5, 5));p an . SetBackground (Color.yellow);//Add Color setlayout (new BorderLayout ()); Define the form layout for the boundary layout//Loop definition button and add to the panel for (int i = 0; i < str.length; i++) {Pan.add (new JButton (Str[i]));} JTextField Text = new JTextField (); Define the text box//Place the text box in the form North position Getcontentpane (). Add (text, Borderlayout.north);//Place the panel in the form center position Getcontentpane (). Add ( Pan, Borderlayout.center); SetSize (320, 320);//define size Setlocationrelativeto (NULL); Center the window to display setresizable (false);//cannot change form size}public static void main (string[] args) {gridframe cal = new Gridframe (); cal.set Title ("calculator"); Cal.setdefaultcloseoperation (jframe.exit_on_close);//Cal.pack (); cal.setvisible (True);}}
Java graphical interface design--the layout manager's GridLayout (Grid layout) Calculator