Java layout manager usage

Source: Internet
Author: User

Many beginners often encounter problems such as how to define the area size or the distance between buttons when using the Java la er to automatically layout and draw interfaces. I have written an article titled how to implement the change of various components in the manual layout of JAVA with the window. Some readers have reported that the calculation of coordinates is not easy and asked if the la er can be used to implement the interface in the article. In fact, automatic layout can also solve the problem of defining the area size or the distance between buttons, but it is not as flexible as manual layout. Here is an example.

First, create a frame file (Application) and set layout in this in Design to BorderLayout.

Second, click the Swing iner tab in the component disk, select the Jpanel icon, and drag a area above this to automatically adjust the location and size of the layout; in the same way, drag a region in the middle and lower layers. In the Swing iner tab, select the jScrollPane icon and drag a jScrollPane area in the middle. The drag and drop sequence must first go up, down, and then in the middle. For easy differentiation, set the Jpanel1 area above to red in the background of Properties, the Jpanel2 area below to orange, and the jScrollPane1 area in the middle to pink. Set layout of Jpanel1 and Jpanel2 to flowLayout (you must set it manually instead of the default value ).

Third, place a Jlable title bar, JTextField1 text box and Jbutton in Jpanel, click the Swing tab in the component tray, and select the JLable icon to draw a title bar in jpanel1, change text to "Enter query conditions", select JtextField to draw a text box in Jpanel1, change text to null, and then select Jbutton to draw a button in Jpanel1 to change text to "query ". After painting, they are all in the middle and the size is fixed. Then, click flowLayout1 in Jpanel to set alignment in Properties on the right to LEFT. Then, the group keys in Jpanel1 are arranged to the LEFT. Select a group key. In Properties, you can set the width and height of the Group Key. The same method draws three Jbutton buttons in Jpanel2 and sets the text as "add", "delete", and "modify ". Click flowLayout2 of Jpane2 and set the hgap in Properties on the right to 30 (the gap between buttons, which can be adjusted according to your needs). In this way, the distance between the three buttons is adjusted, setting vgap also changes the height of the Jpane2 region.

Fourth, create a table in jScrollPane1 to display the content of the database data, click the Swing tab in the component disk, select the JTable icon, and add the Jtable to jScrollPane1.

Finally, change defaultCloseOperation in this to EXIT_ON_CLOSE, so that the program will automatically exit when the window is closed.

The source code of the program is as follows (except for the two sentences in the Chinese comments, they are automatically generated ):

Import javax. swing .*;
Import java. awt .*;
Import java. awt. event .*;
Import java. util. Vector;
Import javax. swing. table. DefaultTableModel;
Public class Frame1
Extends 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 ();

Public Frame1 (){
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}

Public static void main (String [] args ){
Frame1 frame1 = new Frame1 ();
Frame1.setSize (new Dimension (400,350 ));
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 (100, 16 ));
JLabel1.setText ("Enter the query condition ");
JTextField1.setPreferredSize (new Dimension (140, 22 ));
JTextField1.setText ("");
JButton1.setText ("query ");
JButton1.addActionListener (new frameworkjbutton?actionadapter (this ));
FlowLayout1.setAlignment (FlowLayout. LEFT );
FlowLayout1.setHgap (5 );
FlowLayout1.setVgap (10 );
JButton2.setText ("add ");
JButton3.setText ("delete ");
JButton4.setText ("modify ");
FlowLayout2.setHgap (30 );
FlowLayout2.setVgap (5 );
This. setdefaclocloseoperation (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 );
}

// Simulate Database Query
Void jbutton?action=med (ActionEvent e ){
Try {// create a table
Vector vcol = new Vector (); // column name
Vector vrow = new Vector (); // content
For (int col = 1; col <31; col ++ ){
Vcol. addElement ("column" + col );
}
For (int row = 1; row <101; row ++ ){
Vector vr1 = new Vector ();
For (int col = 1; col <31; col ++ ){
Vr1.addElement (row + "/" + col );
}
Vrow. addElement (vr1 );
}
DefaultTableModel dtm = new DefaultTableModel (vrow, vcol );
JTable1 = new JTable (vrow, vcol );
JTable1.setAutoResizeMode (JTable. AUTO_RESIZE_OFF); // set the Left and Right rollback of the scroll bar.
This. jScrollPane1.getViewport (). add (jTable1, null); // add the table to the scroll bar.
}
Catch (Exception ex ){
JOptionPane. showMessageDialog (null, ex );
}

}
}

Class frameworkjbutton=actionadapter
Implements java. awt. event. ActionListener {
Frame1 adaptee;

Frameworkjbutton?actionadapter (Frame1 adaptee ){
This. adaptee = adaptee;
}

Public void actionreceivmed (ActionEvent e ){
Adaptee. jbutton?action=med (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.