Java Layout a calculator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.