How to use Java layout manager


Many beginners often encounter problems such as not knowing how to define the area size or the distance between buttons when using the Java layout tool to automatically layout and draw the interface. I wrote an article on the method of realizing that each component in Java manual layout can change with the window. Some readers reported that it is difficult to calculate the coordinates and asked if they can use the layout to realize the interface in the article. In fact, automatic layout can also solve problems such as defining the size of the area or the distance between buttons, but it is not as flexible as manual layout. Let me give an example.

First, create a frame file (application program) and set the layout in this to borderlayout in design.

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; In the same way, drag an area at the middle and bottom; On the swing container tab, select the JScrollPane icon and drag the JScrollPane to an area in the middle. The order of dragging must be up, down and then in the middle. In the background of properties, set the upper jpanel1 area to red, the lower jpanel2 area to orange, and the middle jscrollpane1 to pink. Set the layout of jpanel1 and jpanel2 to flowlayout (it must be set manually, not the default value).

Third, put a jlable title bar, jtextfield1 text box and JButton button in the JPanel, click the swing tab in the component tray, select the jlable icon, draw a title bar in the jpanel1, change the text to “Please Enter query criteria”, then select jtextfield, draw a text box in jpanel1, and change the text to empty, Finally, select JButton and draw another button in jpanel1 to change text to “query”. After drawing, they are all in the middle and the size is fixed. At this time, click flowlayout1 of JPanel and set the alignment in the properties on the right to left. At this time, the group keys in jpanel1 will be arranged to the left. Select one of the group keys and click preferredSize in properties to set the width and height of the group key. In the same way, draw three JButton buttons in jpanel2 and set text to “add”, “delete” and “modify” respectively. Click flowlayout2 of jpane2 and set hgap in the properties on the right to 30 (the button spacing can be adjusted according to your own needs). In this way, the distance between the three buttons is adjusted. Setting vgap can also change the height of the jpane2 area.

Fourth, create a table in jscrollpane1 to display the contents of database data, click the swing tab in the component disk, select the JTable icon, and add JTable to jscrollpane1.

Finally, change the defaultcloseoperation in this to exit_ ON_ Close, so that the program will exit automatically when the window is closed.

The source code of the program is as follows (except that the two sentences in the Chinese notes are added by themselves, the rest are generated automatically):

  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 {
  catch (Exception e) {

  public static void main(String[] args) {
  Frame1 frame1 = new Frame1();
  frame1.setSize(new Dimension(400, 350));;


  private void jbInit() throws Exception {
  jLabel1.setPreferredSize(new Dimension(100, 16));
Jlabel1.settext (“Please Enter query criteria”);
  jTextField1.setPreferredSize(new Dimension(140, 22));
Jbutton1.settext (“query”);
  jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
Jbutton2.settext (“add”);
Jbutton3.settext (“delete”);
Jbutton4.settext (“modify”);
  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 query database
  void jButton1_actionPerformed(ActionEvent e) {
Try {/ / create table
  Vector vcol = new Vector(); // Listing
  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);
  DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
  jTable1 = new JTable(vrow, vcol);
  jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // Scroll bar setting scroll left and right
  this.jScrollPane1.getViewport().add(jTable1, null); // Place table in 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) {

Recommended Today

Vue、Three. JS implementation panorama

1、 First, we need to create a Vue project This paper mainly records the process of building panorama in detail, so building Vue project is not described too much. 2、 Install three js npm install three –save npm install three-trackballcontrols –save npm install three-orbit-controls –save npm i three-obj-mtl-loader –save npm i three-fbx-loader –save npm i […]