Cách tạo giao diện trong Java – phần 3 : BorderLayout

Để tạo được giao diện trong Java, trước tiên các bạn phải biết được LayoutManager trong Java, nó giống như là bản vẽ kỹ thuật cho một ngôi nhà.

Có nhiều loại LayoutManager, chẳng hạn như:

  •  FlowLayout
  •  BoxLayout
  •  BorderLayout
  •  CardLayout
  •  GridBagLayout
  •  GridLayout
  •  GroupLayout
  •  SpringLayout

Trong phần 3 chúng ta sẽ học về BorderLayout

======================================================================

BorderLayout

======================================================================

BorderLayout giúp chúng ta hiển thị các control theo 5 vùng chính theo hình dưới đây:

Nếu như không có 4 vùng : North, West, South, East. Thì vùng Center sẽ tràn đầy cửa sổ, thông thường khi đưa các control JTable, JTree, ListView, JScrollpane… ta thường đưa vào vùng Center để nó có thể tự co giãn theo kích thước cửa sổ giúp giao diện đẹp hơn.

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.Font;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class MyBorderLayout extends JFrame

{

private static final long serialVersionUID = 1L;

public MyBorderLayout(String title)

{

setTitle(title);

}

public void doShow()

{

setSize(400,300);

setLocationRelativeTo(null);

setDefaultCloseOperation(EXIT_ON_CLOSE);

addControl();

setVisible(true);

}

public void addControl()

{

JPanel pnBorder=new JPanel();

pnBorder.setLayout(new BorderLayout());

Font ft=new Font(“Arial”, Font.BOLD|Font.ITALIC, 25);

JPanel pnNorth=new JPanel();

pnNorth.setBackground(Color.RED);

pnNorth.setPreferredSize(new Dimension(0, 50));

JLabel lblTitleNorth=new JLabel(“North”);

pnNorth.add(lblTitleNorth);

lblTitleNorth.setForeground(Color.WHITE);

lblTitleNorth.setFont(ft);

pnBorder.add(pnNorth,BorderLayout.NORTH);

JPanel pnSouth=new JPanel();

pnSouth.setBackground(Color.RED);

pnSouth.setPreferredSize(pnNorth.getPreferredSize());

JLabel lblTitleSouth=new JLabel(“South”);

pnSouth.add(lblTitleSouth);

lblTitleSouth.setForeground(Color.WHITE);

lblTitleSouth.setFont(ft);

pnBorder.add(pnSouth,BorderLayout.SOUTH);

JPanel pnWest=new JPanel();

pnWest.setBackground(Color.BLUE);

pnWest.setPreferredSize(new Dimension(70, 0));

JLabel lblTitleWest=new JLabel(“West”,JLabel.CENTER);

lblTitleWest.setFont(ft);

lblTitleWest.setForeground(Color.WHITE);

pnWest.setLayout(new BorderLayout());

pnWest.add(lblTitleWest,BorderLayout.CENTER);

pnBorder.add(pnWest,BorderLayout.WEST);

JPanel pnEast=new JPanel();

pnEast.setBackground(Color.BLUE);

pnEast.setPreferredSize(new Dimension(70, 0));

JLabel lblTitleEast=new JLabel(“East”,JLabel.CENTER);

lblTitleEast.setFont(ft);

lblTitleEast.setForeground(Color.WHITE);

pnEast.setLayout(new BorderLayout());

pnEast.add(lblTitleEast,BorderLayout.CENTER);

pnBorder.add(pnEast,BorderLayout.EAST);

JPanel pnCenter=new JPanel();

pnCenter.setBackground(Color.YELLOW);

pnCenter.setLayout(new BorderLayout());

JLabel lblTitleCenter=new JLabel(“Center”,JLabel.CENTER);

lblTitleCenter.setFont(ft);

pnCenter.add(lblTitleCenter,BorderLayout.CENTER);

pnBorder.add(pnCenter,BorderLayout.CENTER);

Container con=getContentPane();

con.add(pnBorder);

}

public static void main(String[] args) {

MyBorderLayout bor=new MyBorderLayout(“BorderLayout”);

bor.doShow();

}

}

Chúng ta có thể kết hợp FlowLayout , BoxLayout, BorderLayout để thiết kế giao diện, theo kinh nghiệm của Tôi thì chúng ta chỉ cần biết 3 Layout này là có thể thiết kế giao diện đẹp mắt được.

4 thoughts on “Cách tạo giao diện trong Java – phần 3 : BorderLayout”

  1. Xin thầy cho em hoi cái này cái dòng” pnSouth.setPreferredSize(pnNorth.getPreferredSize());” có công dụng gì vậy ạ! em tra qua nhiều trang nó toàn giải thích rieng rẽ it thấy gom chung thế này.
    với lại đoạn ” blCateId.setPreferredSize(lblProName.getPreferredSize());
    lblProId.setPreferredSize(lblProName.getPreferredSize());
    lblUnitPrice.setPreferredSize(lblProName.getPreferredSize());
    lblQuantity.setPreferredSize(lblProName.getPreferredSize());
    lblDescription.setPreferredSize(lblProName.getPreferredSize()); ”
    trong chương trình quan lý của thây bên trong đêu dùng “lblProName” hay “pnNorth”
    em ngẩm mãi mà hok ra mong thầy giải thích dùm hay ban nao bik giai thích dum nhé.

Leave a Reply