Java Online chat project 0.2 build client form, use Swing (user Interface Development Kit) and AWT (Abstract window Toolkit) BorderLayout layout differs from GridLayout layout JPanel set size

Source: Internet
Author: User

The code is as follows:

 PackageCom.swift;Importjava.awt.BorderLayout;ImportJava.awt.Color;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JScrollPane;ImportJavax.swing.JTextArea;ImportJavax.swing.JTextField; Public classChatclientframeextendsJFrame {Private Static Final LongSerialversionuid = -118470059355655240l; JLabel Label_shang=NewJLabel (); JLabel Label_xia=NewJLabel (); JTextArea Ta=NewJTextArea (15, 50); JTextField TF=NewJTextField (38); JButton Button=NewJButton ();  PublicChatclientframe () {setbounds (200, 200, 500, 400); Settitle ("Client Chat tool--0.2"); //Large layout of the window, divided into three rows and a column, add three panels on the pbasic panel Shang Zhong XiaJPanel pbasic=NewJPanel (); //using the grid layout of course is the upper and lower the same size ah, decisively give up//pbasic.setlayout (New GridLayout (3,1,5,5)); //using BorderLayoutPbasic.setlayout (NewBorderLayout ());//This layout mode is also not set by defaultSetcontentpane (Pbasic);//Put the Panel on the window, do not remember to use the This keywordJPanel shang=NewJPanel (); JPanel Zhong=NewJPanel (); JPanel Xia=NewJPanel (); //to set the size of the JPanel panelShang.setsize (470, 25); Zhong.setsize (470, 180); Xia.setsize (470, 40);        Pbasic.add (Shang,borderlayout.north);        Pbasic.add (Zhong,borderlayout.center);        Pbasic.add (Xia,borderlayout.south);        Shang.setbackground (color.red);        Zhong.setbackground (Color.yellow);        Xia.setbackground (Color.Blue); /** Three panels, put a label "chat history", put a text field in the middle, * the bottom is divided into left and right--put the label "input Information", text box and "Send" button*/Label_shang.settext ("Chat History");        Shang.add (Label_shang); Ta.setlinewrap (true);//Wrap LineJScrollPane scroll=NewJScrollPane (TA);//increase the scrollbar so that the number of rows is not increasedZhong.add (scroll); Label_xia.settext ("Enter Information");        Xia.add (label_xia,borderlayout.west);        Xia.add (Tf,borderlayout.center); Button.settext (Send);                Xia.add (Button,borderlayout.east); //Auto-Adjust individual panels with compressionpack (); Setdefaultcloseoperation (jframe.exit_on_close);//Click the Close button while exiting the programSetVisible (true); }     Public Static voidMain (string[] args) {//don't forget to create a Form object, or you can call other methods with a build object such as Launchframe ()        NewChatclientframe (); }}

With a grid layout, the size of multiple jpanel is the same because the mesh is fixed in size

If you want to adjust the size of the JPanel, you need to use the BorderLayout layout, which is also the default layout method

The Chat window has a basic panel and adds three panels to the upper and lower three positions;

The bottom panel also adds three items (labels, text boxes, and buttons) to the left and right three positions respectively;

The middle panel is turned into a scrollbar state before the text area is placed

As follows:

Java Online chat project 0.2 build client form, use Swing (user Interface Development Kit) and AWT (Abstract window Toolkit) BorderLayout layout differs from GridLayout layout JPanel set size

Related Article

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.