Java AWT | SpringLayout类

AWT(抽象窗口工具包)中的一个SpringLayout类根据一组布局约束将子类布局到其关联的容器中。每个约束由一个弹簧对象表示,该对象控制两个组件边之间的垂直或水平距离。这些边可以属于容器的任何子级,也可以属于容器本身。默认情况下, SpringLayout 创建约束,使其关联的零部件具有 最低限度 , 首选 最大限度 真实的 价值

null

建造师:

  • SpringLayout(): 用于构造新的SpringLayout类。

常用方法:

  • void addLayoutComponent(组件com,对象cons): 如果约束是 SpringLayout。约束条件 ,将约束与指定的零部件相关联。
  • getLayoutAlignmentX(容器c): 用于返回0.5f(居中)。
  • GetLayoutAlignment(容器c): 用于返回0.5f(居中)。
  • getConstraint((字符串edgeName,组件c): 返回控制零部件的指定边与其父零部件的上边缘或左边缘之间距离的弹簧。
  • getConstraint(组件c): 返回指定组件的约束。
  • layoutContainer(容器父级): 用于布置指定的容器。

下面的程序演示了SpringLayout类:

  • 项目1: 下面的程序将组件按顺序排列 窗口 .我们创造了1个 标签类 名为“的组件” 标签 “并创建一个1 文本框 “命名” 文本框 “创建两个类,一个是 JFrame类 另一个是 SpringLayout课程 然后将它们添加到 窗口 顺便说一下 添加() 。我们使用 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: 下面的程序将组件按顺序排列 窗口 .我们创建了一个名为“ springlayout 并创建一个4 按扭 名为“的组件” b1〃、“b2”、“b3”、“b4”、“b5” “然后将它们添加到 窗口 顺便说一下 添加() .我们使用以下方法设置框架的可见性 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

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享