Implementation of JAVA manual layout of each component can change with the window

Source: Internet
Author: User

Implementation of JAVA manual layout of each component can change with the window

Mao yongheng,-8-9, Department of Computer Science, Guizhou University

JAVA programmers may have encountered this situation. When using a manual layout to create an interface, the size of each component cannot be changed randomly with the window. However, it is especially troublesome to switch to the Automatic Layout mode. Sometimes a complicated design is required for the location of a button, and the final effect is still unsatisfactory.
Is there any way to use a manual layout component that can change freely as the window changes? I use the componentResized event of the Frame to implement manual layout. Not only can components change with the change of the window, but also the idea is clear, the layout is flexible, and the code is small. I only need a few programs to solve the problem.
First look:
Http://www.javaresearch.org/members/w.mao yongheng #/images/java.gif
Simulate an interface for adding, deleting, modifying, and querying database content. Red indicates the condition selection area, orange indicates the functional area of the program, and the table indicates the data area. The program is edited and run in JBuilder 9.
First, create a frame file (Application) and set layout in this in Design to null (do not use the layout tool );
Second, click the Swing iner tab in the component tray, select the Jpanel icon, and drag a area above this. In the same way, drag a area at the bottom, to facilitate differentiation, set the Jpanel1 area above to red, and the Jpanel2 area below to orange. Set layout of Jpanel1 and Jpanel2 to null (do not la ER ).
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 on the left of jpanel1, change text to "Enter query conditions", and then select JtextField to draw a text box in the middle of Jpanel1 to empty the text, finally, select Jbutton and draw a button in Jpanel1 to change text to "query ". In Jpanel2, draw three jbuttons and set the text to "add", "delete", and "modify ".
Fourth, create a table in the middle of this to display the content of the database data. On the Swing iner tab, select the jScrollPane icon and drag a large area in the middle of this, click the Swing tab in the component disk, select the JTable icon, and add the Jtable to jScrollPane.
In this way, my interface is ready, but this interface cannot be adjusted automatically with the window. The following program implements manual layout so that the area can be adjusted automatically with the window.
First select this form with the mouse, then select componentResized In the event Events on the right, double-click the mouse, and enter void this_componentResized (ComponentEvent e) {}, and write the following code in:

// Set the start coordinate of jPanel1
JPanel1.setLocation (0, 0 );
// Set the height and width of jpanel1.
JPanel1.setSize (this. getWidth (), jPanel1.getHeight ());

// Set the start coordinate of jPanel2 (37 is the height of the title bar)
JPanel2.setLocation (0, this. getHeight ()-jPanel2.getHeight ()-37 );
// Set the height and width of jpanel1.
JPanel2.setSize (this. getWidth (), jPanel2.getHeight ());

// Set the start coordinate of jScrollPanel
JScrollPane1.setLocation (0, jPanel1.getHeight ());
// Set the height and width of jScrollPanel
JScrollPane1.setSize (this. getWidth (), jPanel2.getY ()-jPanel1.getHeight ());

This program only adjusts the changes of Jpanel1, Jpanel2, and JScrollPane1 with the window. In the same way, buttons and text boxes can change with the window.
The program simulates data extraction from the database in the "query" button event and displays it in the table. For simplicity, the program does not process input conditions, add, delete, and modify.
Program source code (the red part is hand-written. For specific functions, see comments. The rest are automatically generated by JB9 ):

Package untitled1;

Import javax. swing .*;
Import java. awt .*;
Import java. awt. event .*;
Import java. util. Vector;
Import javax. swing. table. DefaultTableModel;

Public class Frame1 extends JFrame {
JPanel jPanel1 = new JPanel ();
JPanel jPanel2 = new JPanel ();
JScrollPane jScrollPane1 = new JScrollPane ();
JTable jTable1 = new JTable ();
JLabel jLabel1 = new JLabel ();
JTextField jTextField1 = new JTextField ();
JButton jButton1 = new JButton ();
JButton jButton2 = new JButton ();
JButton jButton3 = new JButton ();
JButton jButton4 = new JButton ();

Public Frame1 (){
Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}
}
Public static void main (String [] args ){
Frame1 frame1 = new Frame1 ();
// Define the size of the display window
Frame1.setSize (400,350 );
Frame1.show ();
// Activate the "query" button event
Frame1.jButton1. doClick ();
}
Private void jbInit () throws Exception {
This. getContentPane (). setLayout (null );
JPanel1.setBackground (Color. red );
JPanel1.setBounds (new Rectangle (43, 15,297, 48 ));
JPanel1.setLayout (null );
JPanel2.setBounds (new Rectangle (50,232,297, 48 ));
JPanel2.setLayout (null );
JPanel2.setBackground (Color. orange );
JScrollPane1.getViewport (). setBackground (Color. pink );
JScrollPane1.setBounds (new Rectangle (53, 74,287,138 ));
JLabel1.setFont (new java. awt. Font ("Dialog", 0, 15 ));
JLabel1.setText ("input query content ");
JLabel1.setBounds (new Rectangle (44, 12, 80, 27 ));
JTextField1.setText ("");
JTextField1.setBounds (new Rectangle (125, 14, 70, 22 ));
JButton1.setBounds (new Rectangle (221, 12, 61, 24 ));
JButton1.setFont (new java. awt. Font ("Dialog", 0, 13 ));
JButton1.setText ("query ");
JButton1.addActionListener (new frameworkjbutton?actionadapter (this ));
JButton2.setBounds (new Rectangle (13, 14, 66, 27 ));
JButton2.setFont (new java. awt. Font ("Dialog", 0, 13 ));
JButton2.setText ("add ");
JButton3.setText ("delete ");
JButton3.setBounds (new Rectangle (115, 14, 66, 27 ));
JButton3.setFont (new java. awt. Font ("Dialog", 0, 13 ));
JButton4.setText ("modify ");
JButton4.setBounds (new Rectangle (217, 14, 66, 27 ));
JButton4.setFont (new java. awt. Font ("Dialog", 0, 13 ));
This. addHierarchyBoundsListener (new frameworkthis_hierarchyboundsadapter (this ));
This. addComponentListener (new frameworkthis_componentadapter (this ));
This. getContentPane (). add (jPanel1, null );
JPanel1.add (jTextField1, null );
JPanel1.add (jButton1, null );
JPanel1.add (jLabel1, null );
This. getContentPane (). add (jPanel2, null );
JPanel2.add (jButton3, null );
JPanel2.add (jButton4, null );
JPanel2.add (jButton2, null );
This. getContentPane (). add (jScrollPane1, null );
JScrollPane1.getViewport (). add (jTable1, null );
}

Void this_componentResized (ComponentEvent e ){
// Set the start coordinate of jPanel1
JPanel1.setLocation (0, 0 );
// Set the height and width of jpanel1.
JPanel1.setSize (this. getWidth (), jPanel1.getHeight ());

// Set jPanel

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.