Cấp bậc tác giả:

TRAINING

JAVA - Làm quen layout trong swing

Được viết bởi webmaster ngày 15/07/2018 lúc 03:32 PM
Bài trước hướng dẫn cách lập trình giao diện form với Swing, bài tiếp theo này sẽ giúp làm quen thêm về layout trong swing.
  • 0
  • 10438

JAVA - Làm quen layout trong swing

Bài trước hướng dẫn cách lập trình giao diện form với Swing, bài tiếp theo này sẽ giúp làm quen thêm về layout trong swing.
1. BoxLayout
Lớp BoxLayout, trong java.swing package, được sử dụng để sắp xếp các thành phần hoặc theo chiều dọc hoặc theo chiều ngang. Để phục vụ mục đích này, lớp BoxLayout cung cấp 4 hằng:
public static final int X_AXIS
public static final int Y_AXIS
public static final int LINE_AXIS
public static final int PAGE_AXIS

swing-layout-01.png

import java.awt.Button;
import java.awt.Frame;
import javax.swing.BoxLayout;

public class BoxLayoutExample1 extends Frame {

    Button buttons[];

    public BoxLayoutExample1() {
        buttons = new Button[5];

        for (int i = 0; i < 5; i++) {
            buttons[i] = new Button("Button " + (i + 1));
            add(buttons[i]);
        }

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String args[]) {
        BoxLayoutExample1 b = new BoxLayoutExample1();
    }
}
2. BoxLayout
Lớp BorderLayout sắp xếp các thành phần để phù hợp với 5 miền: đông, tây, nam, bắc và trung tâm. Nó là layout mặc định của Frame hoặc Window. Mỗi khu vực (miền) chỉ có thể chứa một thành phần và mỗi thành phần trong mỗi khu vực được nhận diện bởi các hằng tương ứng là:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
swing-layout-02.png 
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Border {

    JFrame f;

    Border() {
        f = new JFrame();

        JButton b1 = new JButton("NORTH");;
        JButton b2 = new JButton("SOUTH");;
        JButton b3 = new JButton("EAST");;
        JButton b4 = new JButton("WEST");;
        JButton b5 = new JButton("CENTER");;

        f.add(b1, BorderLayout.NORTH);
        f.add(b2, BorderLayout.SOUTH);
        f.add(b3, BorderLayout.EAST);
        f.add(b4, BorderLayout.WEST);
        f.add(b5, BorderLayout.CENTER);

        f.setSize(300, 300);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new Border();
    }
}
3. FlowLayout
Lớp FlowLayout được sử dụng để sắp xếp các thành phần trong một line, line sau nối tiếp line trước (trong một luồng từ trái qua phải left-to-right flow). Nó là Layout mặc định của applet hoặc panel.
swing-layout-03.png

import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyFlowLayout {

    JFrame f;

    MyFlowLayout() {

        f = new JFrame("Ví dụ về FlowLayout");

        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");

        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
        f.add(b5);

        f.setLayout(new FlowLayout(FlowLayout.RIGHT));
        //thiet lap flow layout la can chinh phai 

        f.setSize(300, 300);
        f.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
        f.setVisible(true);
    }

    public static void main(String[] args) {

        new MyFlowLayout();
    }
}
4. GridLayout
Lớp GridLayout sắp xếp các thành phần trong một lưới hình chữ nhật. Một thành phần được hiển thị trong mỗi hình chữ nhật.
 swing-layout-04.png
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyGridLayout {

    JFrame f;

    MyGridLayout() {
        f = new JFrame("Ví dụ về GridLayout");

        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");

        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
        f.add(b5);
        f.add(b6);
        f.add(b7);
        f.add(b8);
        f.add(b9);

        f.setLayout(new GridLayout(3, 3));
    //thiet lap 3 hang va 3 cot cho grid layout 

        f.setSize(300, 300);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new MyGridLayout();
    }
}
5. GridBagLayout
GridBagLayout là một lớp quản lý layout linh động. Đối tượng của GridBagLayout căn chỉnh các thành phần theo chiều dọc, ngang hoặc theo baseline của chúng mà không yêu cầu các thành phần phải có cùng kích cỡ.
 swing-layout-05.png
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SwingLayoutDemo {

    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;
    private JLabel msglabel;

    public SwingLayoutDemo() {
        prepareGUI();
    }

    public static void main(String[] args) {
        SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
        swingLayoutDemo.showGridBagLayoutDemo();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Vi du Java Swing");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));

        headerLabel = new JLabel("", JLabel.CENTER);
        statusLabel = new JLabel("", JLabel.CENTER);

        statusLabel.setSize(350, 100);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }

    private void showGridBagLayoutDemo() {
        headerLabel.setText("Layout in action: GridBagLayout");

        JPanel panel = new JPanel();
        panel.setBackground(Color.darkGray);
        panel.setSize(300, 300);
        GridBagLayout layout = new GridBagLayout();

        panel.setLayout(layout);
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JButton("Button 1"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        panel.add(new JButton("Button 2"), gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.ipady = 20;
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JButton("Button 3"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        panel.add(new JButton("Button 4"), gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 2;
        panel.add(new JButton("Button 5"), gbc);

        controlPanel.add(panel);

        mainFrame.setVisible(true);
    }
}

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML