Java online chat project version 0.2 creates client forms, and uses swing (User Interface Development Kit) and awt (Abstract Window Toolkit) BorderLayout layout to set the size of JPanel, which is different from GridLayout layout,
The Code is as follows:
Package com. swift; import java. awt. borderLayout; import java. awt. color; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. JTextArea; import javax. swing. JTextField; public class ChatClientFrame extends JFrame {private static final long serialVersionUID =-118470059355655240L; JLabel label_shang = new JLabel (); JLabel label_xia = new JLabel (); JTextArea ta = new JTextArea (15, 50); JTextField tf = new JTextField (38); JButton button = new JButton (); public ChatClientFrame () {setBounds (200,200,500,400 ); setTitle ("client chat tool -- 0.2"); // large window layout, divided into three rows and one column, add three panels to the pBasic panel. setLayout (new GridLayout (3, 1, 5); // use BorderLayout pBasic. setLayout (new BorderLayout (); // if this is not set, the default Layout mode is setContentPane (pBasic). // you can place the panel in the window. Do not remember to use this. keyword: JPanel shang = new JPanel (); JPanel zhong = new JPanel (); JPanel xia = new JPanel (); // set the size of the JPanel shang. 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, with a tag "chat record" in the top, a text field in the middle, and * bottom divided into left-right -- respectively put the tag "input information", text Box and "send" button */label_shang.setText ("chat record"); shang. add (label_shang); ta. setLineWrap (true); // automatically wrap JScrollPane scroll = new JScrollPane (ta); // Add a scroll bar so that no rows are added. add (scroll); label_xia.setText ("input information"); xia. add (label_xia, BorderLayout. WEST); xia. add (tf, BorderLayout. CENTER); button. setText ("send"); xia. add (button, BorderLayout. EAST); // automatically adjust each panel pack () through compression; setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // click the close button to exit the program setVisible (true);} public static void main (String [] args) {// do not forget to create a form object, you can also use the generated object to call other methods, such as launchFrame () new ChatClientFrame ();}}
When grid layout is used, the size of multiple jpanels is the same, because the grid size is fixed.
To adjust the JPanel size, use the BorderLayout layout method, which is also the default layout method.
There is a basic panel in the chat window, and three panels are added to it, which are placed in the upper and lower layers;
Three more content (TAG, text box, And button) are added to the lower panel, which are placed in the middle left and right sides;
The middle panel changes to the scroll bar status before being placed in the hidden area.
As follows: