Lớp SpringLayout đặt vị trí các con của Container liên kết với nó tuân theo một tập hợp các ràng buộc.

Cú pháp để khai báo lớp javax.swing.SpringLayout là:

public class SpringLayout
   extends Object
      implements LayoutManager2

Lớp này có SpringLayout() constructor để tạo một SpringLayout mới.

Lớp này kế thừa các phương thức từ các lớp sau:

  • java.lang.Object

Lớp SpringLayout bao gồm các trường sau:

  • static String BASELINE: Xác định baseline của một thành phần.
  • static String EAST: Xác định cạnh phải của hình chữ nhật bao của một thành phần.
  • static String HEIGHT: Xác định chiều cao của hình chữ nhật bao của một thành phần.
  • static String HORIZONTAL_CENTER: Xác định sự căn chỉnh ngang của hình chữ nhật bao của một thành phần.
  • static String NORTH: Xác định cạnh trên của hình chữ nhật bao của một thành phần.
  • static String SOUTH: Xác định cạnh dưới của hình chữ nhật bao của một thành phần.
  • static String VERTICAL_CENTER: Xác định sự căn chỉnh dọc của hình chữ nhật bao của một thành phần.
  • static String WEST: Xác định cạnh trái của hình chữ nhật bao của một thành phần.
  • static String WIDTH: Xác định độ rộng của hình chữ nhật bao của một thành phần.

Ví dụ lớp SpringLayout

package com.hoclaptrinh.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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.showSpringLayoutDemo();       
   }

   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 showSpringLayoutDemo(){

      headerLabel.setText("Layout in action: SpringLayout");   
      SpringLayout layout = new SpringLayout();

      JPanel panel = new JPanel();
      panel.setLayout(layout);
      JLabel label = new JLabel("Enter Name: ");
      JTextField textField = new JTextField("", 15);
      panel.add(label);
      panel.add(textField);

      layout.putConstraint(SpringLayout.WEST, label,5,
         SpringLayout.WEST, controlPanel);
      layout.putConstraint(SpringLayout.NORTH, label,5,
         SpringLayout.NORTH, controlPanel);
      layout.putConstraint(SpringLayout.WEST, textField,5,
         SpringLayout.EAST, label);
      layout.putConstraint(SpringLayout.NORTH, textField,5,
         SpringLayout.NORTH, controlPanel);
      layout.putConstraint(SpringLayout.EAST, panel,5,
         SpringLayout.EAST, textField);
      layout.putConstraint(SpringLayout.SOUTH, panel,5,
         SpringLayout.SOUTH, textField);
      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   } 
}

Viết câu trả lời

Drop Images

0 Bình luận