Java graphical interface design--the layout manager's GridLayout (Grid layout) Calculator

Source: Internet
Author: User


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

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.