Java Layout Manager uses method _jsp programming

Source: Internet
Author: User
Tags manual
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. I have written a "implementation of the Java Manual layout of the various components can change with the window of the method", there are readers to reflect the coordinates of the calculation, ask if you can use the layout to implement the interface of the article. 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 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 ("Please enter query conditions");
Jtextfield1.setpreferredsize (New Dimension (140, 22));
Jtextfield1.settext ("");
Jbutton1.settext ("Query");
Jbutton1.addactionlistener (This) (new Frame1_jbutton1_actionadapter);
Flowlayout1.setalignment (Flowlayout.left);
Flowlayout1.sethgap (5);
Flowlayout1.setvgap (10);
Jbutton2.settext ("Increase");
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);
}

Mock 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 < 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); Scroll bar set left and right roll
This.jScrollPane1.getViewport (). Add (JTable1, NULL); Put a table in the scroll bar
}
catch (Exception ex) {
Joptionpane.showmessagedialog (null, ex);
}

}
}

Class Frame1_jbutton1_actionadapter
Implements 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.