AWT(抽象窗口工具包)中的 SpringLayout 類根據一組布局約束將子級布局到其關聯容器。每個約束都由一個 Spring 對象表示,該對象控製兩個組件邊之間的垂直或水平距離。邊可以屬於容器的任何子容器,也可以屬於容器本身。默認情況下,SpringLayout 創建約束,使其關聯組件具有最小值、首選值、最大值和實際值。
構造函數:
- SpringLayout():用於構造新的SpringLayout 類。
常用方法:
- void addLayoutComponent(Component com, Object cons):如果約束是一個實例SpringLayout.Constraints,將約束與指定組件相關聯。
- getLayoutAlignmentX(容器c):用於返回 0.5f(居中)。
- getLayoutAlignmentY(容器c):用於返回 0.5f(居中)。
- getConstraint((字符串邊名稱,組件 c):返回控製組件的指定邊與其父級的上邊或左邊之間距離的彈簧。
- getConstraint(組件c):返回指定組件的約束。
- 布局容器(容器父級):用於布置指定的容器。
以下示例程序旨在說明 SpringLayout 類:
- 程序1:下麵的程序將組件排列在一個J框架。我們創建 1J標簽名為“的組件標簽”並創建一個 1文本字段名為“文本域”並創建 2 個類,一個是JFrame類另一個是SpringLayout 類然後將它們添加到J框架通過方法add()。我們使用設置框架的可見性setvisible()方法。布局是通過使用設置的setLayout()方法。
// Java program to show Example of SpringLayout. // in java. Importing different Package. import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SpringLayout; // construct a class springdemo public class Springdemo { // It is used to create and show GUI private static void createAndShowGUI() { // Creating Object of "JFrame" class JFrame frame = new JFrame("MySpringDemp"); // Function to set default // close operation of JFrame. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // to get content pane Container contentPane = frame.getContentPane(); // Creating Object of "Springlayout" class SpringLayout layout = new SpringLayout(); // to set content pane contentPane.setLayout(layout); // Initialization of object // "label" of JLabel class. JLabel label = new JLabel("Label: "); // Initialization of object // "label" of JLabel class. JTextField textField = new JTextField("Enter the text ", 15); // to add content pane of JLabel contentPane.add(label); // to add content pane of JTextField contentPane.add(textField); // It is used to put the layout // constraint in JFrame using springLayout class layout.putConstraint(SpringLayout.WEST, label, 6, SpringLayout.WEST, contentPane); layout.putConstraint(SpringLayout.NORTH, label, 6, SpringLayout.NORTH, contentPane); layout.putConstraint(SpringLayout.WEST, textField, 6, SpringLayout.EAST, label); layout.putConstraint(SpringLayout.NORTH, textField, 6, SpringLayout.NORTH, contentPane); layout.putConstraint(SpringLayout.EAST, contentPane, 6, SpringLayout.EAST, textField); layout.putConstraint(SpringLayout.SOUTH, contentPane, 6, SpringLayout.SOUTH, textField); // Function to pack the JFrame. frame.pack(); // Function to set visible status of JFrame. frame.setVisible(true); } // Main Method public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { // create a class public void run() { // to create and show GUI createAndShowGUI(); } }); } }
輸出:
-
程序2:下麵的程序將組件排列在一個J框架。我們創建 1 個類,名為“彈簧布局類”並創建一個 4J按鈕名為“的組件b1”,“b2”,“b3”,“b4”,“b5”,然後將它們添加到J框架通過方法add()。我們使用以下方法設置框架的可見性setvisible()。布局設置為setLayout()方法。
// Java program to show Example of SpringLayout. // in java. Importing different Package. import java.awt.*; import javax.swing.*; // construct a class Springclassdemo public class Springclassdemo { // Main Method public static void main(String[] arguments) { // main window // Function to set the default look // and feel decorated status of JFrame. JFrame.setDefaultLookAndFeelDecorated(true); // Creating Object of "JFrame" class JFrame frame = new JFrame("SpringLayoutExample Example"); // Function to set the default // close operation status of JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Function to set the // size status of JFrame frame.setSize(300, 200); // to get the content pane Container content = frame.getContentPane(); // Creating Object of "SpringLayout" class SpringLayout layout = new SpringLayout(); // to set the layout class frame.setLayout(layout); // Initialization of object // "b1" of JButton class. Component b1 = new JButton("GEEKS"); // Initialization of object // "b2" of JButton class. Component b2 = new JButton("GFG"); // Initialization of object // "b3" of JButton class. Component b3 = new JButton("JAVA"); // Initialization of object // "b4" of JButton class. Component b4 = new JButton("Sudo Placement"); // Adding the JButton "b1" on frame frame.add(b1); // Adding the JButton "b2" on frame frame.add(b2); // Adding the JButton "b3" on frame frame.add(b3); // Adding the JButton "b4" on frame frame.add(b4); // It is used to put the layout // constraint in JFrame using // springLayout class on b1 JButton layout.putConstraint(SpringLayout.WEST, b1, 25, SpringLayout.WEST, content); layout.putConstraint(SpringLayout.NORTH, b1, 10, SpringLayout.NORTH, content); // It is used to put the layout // constraint in JFrame using // springLayout class on b2 JButton layout.putConstraint(SpringLayout.WEST, b2, 50, SpringLayout.WEST, content); layout.putConstraint(SpringLayout.NORTH, b2, 10, SpringLayout.SOUTH, b1); // It is used to put the layout // constraint in JFrame using // springLayout class on b3 JButton layout.putConstraint(SpringLayout.WEST, b3, 75, SpringLayout.WEST, content); layout.putConstraint(SpringLayout.NORTH, b3, 10, SpringLayout.SOUTH, b2); // It is used to put the layout // constraint in JFrame using // springLayout class on b4 JButton layout.putConstraint(SpringLayout.WEST, b4, 15, SpringLayout.EAST, b1); layout.putConstraint(SpringLayout.NORTH, b4, 10, SpringLayout.NORTH, content); // Function to set the // visible status of JFrame frame.setVisible(true); } }
輸出:
注意:上述程序可能無法在在線 IDE 中運行。請使用離線編譯器。
參考: https://docs.oracle.com/javase/7/docs/api/javax/swing/SpringLayout.html
相關用法
- Java AWT Dimension用法及代碼示例
- Java AWT Choice用法及代碼示例
- Java AWT MenuShortcut用法及代碼示例
- Java AWT Desktop用法及代碼示例
- Java AWT BoxLayout用法及代碼示例
- Java AWT GridBagLayout用法及代碼示例
- Java AWT CardLayout用法及代碼示例
- Java AWT Canvas用法及代碼示例
- Java AWT GridLayout用法及代碼示例
- Java AWT BorderLayout用法及代碼示例
- Java AWT Color用法及代碼示例
- Java ArrayList add()用法及代碼示例
- Java ArrayList addAll()用法及代碼示例
- Java ArrayList clear()用法及代碼示例
- Java ArrayList clone()用法及代碼示例
- Java ArrayList contains()用法及代碼示例
- Java ArrayList get()用法及代碼示例
- Java ArrayList indexOf()用法及代碼示例
- Java ArrayList removeAll()用法及代碼示例
- Java ArrayList remove()用法及代碼示例
- Java ArrayList size()用法及代碼示例
- Java ArrayList isEmpty()用法及代碼示例
- Java ArrayList subList()用法及代碼示例
- Java ArrayList set()用法及代碼示例
注:本文由純淨天空篩選整理自Shivi_Aggarwal大神的英文原創作品 Java AWT | SpringLayout Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。