1 Importjava.awt.*;2 Importjavax.swing.*;3 Public classcounter{4 Public Static voidMain (string[] args) {5JFrame frame=NewJFrame ("xx's calculator");//Create a form title6Frame.setlayout (NewGridLayout (6,3,3,3));//table layout 7*3, horizontal and vertical spacing of 37JPanel pan =NewJPanel (NewGridLayout (1,3,3,3));//creates a panel container, specified as table layout, 1*3, horizontal and vertical spacing of 38JButton b1=NewJButton ("View");//9JButton b2=NewJButton ("edit");//TenJButton b3=NewJButton ("Help");//create three buttons and add text to a button OnePan.add (B1);// APan.add (B2);// -Pan.add (B3);//add three buttons to a Pan panel container -Frame.add (PAN);//Add a Pan panel container to a form theJPanel pan1=NewJPanel (NewGridLayout (1,4,3,3));//Create Pan1 panel container, specify as table layout, 1*4, horizontal vertical spacing of 3 -JPanel pan2=NewJPanel (NewGridLayout (1,4,3,3));//Create pan2 panel container, specify as table layout, 1*4, horizontal vertical spacing of 3 -JPanel pan3=NewJPanel (NewGridLayout (1,4,3,3));//Create PAN3 panel container, specify as table layout, 1*4, horizontal vertical spacing of 3 -JPanel pan4=NewJPanel (NewGridLayout (1,4,3,3));//Create PAN4 panel container, specify as table layout, 1*4, horizontal vertical spacing of 3 +JTextField h1=NewJTextField ();//Create a single-line text box H1 -Frame.add (H1);//Add a single-line text box to a form +String str[]={"7", "8", "9", "/",};//defines a str array and initializes the AJButton but=NULL;//Create an empty button model but at for(inti=0;i<4;i++){ -but =NewJButton ("" +Str[i]); -Pan1.add (But);//use loops to add to the Pan1 panel container individually - } -String str2[]={"4", "5", "6", "*",};//defines an array of str2 arrays and initializes -JButton but2=NULL;//Create a button model in for(inti=0;i<4;i++){ -But2=NewJButton ("" +str2[i]); toPan2.add (BUT2);//Add the buttons in the array to the PAN2 container + } - theString str3[]={"1", "2", "3", "-",};//defines an array of STR3 arrays and initializes *JButton but3=NULL; $ for(inti=0;i<4;i++){ Panax NotoginsengBUT3 =NewJButton ("" +Str3[i]); -Pan3.add (BUT3);//Add the buttons in the array to the PAN3 container the } + AString str4[]={"0", ".", "=", "+"};//defines an array of STR4 arrays and initializes theJButton but4=NULL; + for(inti=0;i<4;i++){ -But4 =NewJButton ("" +Str4[i]); $Pan4.add (BUT4);//Add the buttons in the array to the PAN4 container $ } -Frame.add (PAN1);//Add a pan1 container to a form -Frame.add (PAN2);//Add a pan2 container to a form theFrame.add (PAN3);//Add a pan3 container to a form -Frame.add (PAN4);//Add a Pan4 container to a formWuyiFrame.pack ();//set Form size to content size theFrame.setvisible (true);//Show Form - } Wu } -
Results of the operation
****************************** Note that this computer button has not been added to the event!!! So it's just a style, and it's not working properly *****************************
Java Layout a calculator