Example to explain how to use Layout manager in Java

Source: Internet
Author: User
Tags query

Many beginners use the Java layout to automatically layout the interface, often encounter problems such as not knowing how to define the area size or the distance between the buttons. In fact, automatic layout can also solve the definition of area size or the distance between buttons, and so on, but not the manual layout so flexible. Let me give you an example.

First, build a frame file (application application) in the design to set the layout in this to BorderLayout.

Second, click the Swing Container tab in the Component tray, select the JPanel icon, drag an area above this, and the layout will automatically adjust the position and size; The same method drags an area below the bottom; In swing container page sign, Select the JScrollPane icon to drag the JScrollPane in the middle of the area. The order must be dragged first, then the middle. To make it easier to distinguish, in the background of properties, set the upper Jpanel1 area to red, the lower Jpanel2 area to Orange, and the middle jScrollPane1 pink. Set the layout of Jpanel1 and Jpanel2 to FlowLayout (you must set it manually, not the default).

Third, in the JPanel, put a jlable title bar, JTextField1 text box and JButton button, click the Swing tab in the Palette, select the Jlable icon in the Jpanel1 to draw a title bar, change text to "Please enter query conditions", Then select JTextField to draw a text box in Jpanel1, change text to empty, and finally select JButton to draw a button in Jpanel1 to change text to query. After the painting they are all in the middle, and the size of fixed, then click the JPanel flowLayout1 to the right side of the properties of the alignment set to left, then the JPANEL1 in the group key will be aligned to the Ieft. Select one of the group keys, and the Preferredsize in properties can set the width and height of the group keys. The same method draws three JButton buttons in the Jpanel2, and the text is set to add, delete, and modify respectively. Click the Jpane2 flowLayout2 to set the Hgap in the right side of the properties to 30 (the spacing of the buttons, adjust the numeric size according to your needs), adjust the distance between the three buttons, and set the Vgap to change the height of the Jpane2 area.

In JScrollPane1, a table is built to display the contents of the database data, click the Swing tab in the Palette, select the JTable icon, and add JTable to the jScrollPane1.

Finally, change the defaultcloseoperation in this to Exit_on_close so that the program exits automatically when you close the window.

The program source code is as follows (in addition to the Chinese annotation part of the two sentences are added, the rest is automatically generated):

       
        
Import Javax.swing.*;import java.awt.*;import Java.awt.event.*;import Java.util.vector;import Javax.swing.table.DefaultTableModel; public class Frame1extends JFrame {borderlayout borderLayout1 = new BorderLayout (); JPanel JPanel1 = new JPanel (); JPanel JPanel2 = new JPanel (); JPanel jPanel3 = new JPanel (); JLabel JLabel1 = new JLabel (); JTextField jTextField1 = new JTextField (); JButton jButton1 = new JButton (); FlowLayout flowLayout1 = new FlowLayout (); FlowLayout flowLayout2 = new FlowLayout (); JButton jButton2 = new JButton (); JButton JButton3 = new JButton (); JButton jButton4 = new JButton (); GridLayout gridLayout1 = new GridLayout (); JScrollPane jScrollPane1 = new JScrollPane (); JTable jTable1 = new JTable ();p ublic Frame1 () {try {jbinit ();} catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {Frame1 frame1 = new Frame1 (); Frame1.setsize (new Dimension ()); Frame1.show ( );} private void Jbinit () throws Exception {This.getcontentpane (). setlayout (BORDERLAYOUT1); JpanEl1.setbackground (color.red); Jpanel1.setlayout (FLOWLAYOUT1); Jpanel2.setbackground (color.red); jpanel2.setlayout (FLOWLAYOUT2); Jpanel3.setbackground (Color.pink); Jpanel3.setlayout (GRIDLAYOUT1); Jlabel1.setpreferredsize (new Dimension); Jlabel1.settext ("Please enter query conditions"); Jtextfield1.setpreferredsize (new Dimension (140, 22)); Jtextfield1.settext (""); Jbutton1.settext ("query"); Jbutton1.addactionlistener (New Frame1_jbutton1_actionadapter ( this)); Flowlayout1.setalignment (Flowlayout.left); Flowlayout1.sethgap (5); Flowlayout1.setvgap (10); Jbutton2.settext ("Add"); Jbutton3.settext ("delete"); Jbutton4.settext ("modification"); Flowlayout2.sethgap (30); Flowlayout2.setvgap (5); this.setdefaultcloseoperation (exit_on_close); This.getcontentpane (). Add (JPanel1, Borderlayout.north); Jpanel1.add (jLabel1, null); Jpanel1.add (jTextField1, null); Jpanel1.add (jButton1, NULL); This.getcontentpane (). Add (JPanel2, Borderlayout.south); Jpanel2.add (jButton2, null); Jpanel2.add (JButton3, NULL); Jpanel2.add (jButton4, null); This.getcontentpane (). Add (JPanel3, BoRderlayout.center); Jpanel3.add (jScrollPane1, null); Jscrollpane1.getviewport (). Add (JTable1, NULL); Analog query database void jbutton1_actionperformed (ActionEvent e) {try {//* Make table vector vcol = new vector ();//column name vector vrow = new vector (); Content for (int col = 1; col < col++) {vcol.addelement ("column" + col);} for (int row = 1; row < row++) {vector vr1 = new vector (), for (int col = 1; col < to col++) {vr1.addelement (ro W + "/" + col);} Vrow.addelement (VR1);} DefaultTableModel DTM = new DefaultTableModel (Vrow, vcol); jTable1 = new JTable (Vrow, Vcol); Jtable1.setautoresizemode ( Jtable.auto_resize_off); The scroll bar sets the left and right roll This.jScrollPane1.getViewport (). Add (JTable1, NULL); Place table}catch (Exception ex) {joptionpane.showmessagedialog (null, ex) in the scroll bar}} Class frame1_jbutton1_actionadapterimplements Java.awt.event.ActionListener {Frame1 adaptee; Frame1_jbutton1_actionadapter (Frame1 adaptee) {this.adaptee = Adaptee;} public void actionperformed (ActionEvent e) {adaptee.jbutton1_actionperformed (e);}}
       


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.