Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
106 views
in JAVA by (178k points)
How do I use SpringLayout in Java?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

Here's an example code snippet that demonstrates how to use SpringLayout in Java:

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

public class SpringLayoutExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("SpringLayout Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);

        // Create components
        JLabel nameLabel = new JLabel("Name:");
        JTextField nameField = new JTextField(10);
        JButton submitButton = new JButton("Submit");

        // Add components to the content pane
        contentPane.add(nameLabel);
        contentPane.add(nameField);
        contentPane.add(submitButton);

        // Set constraints for components
        layout.putConstraint(SpringLayout.WEST, nameLabel, 10, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, nameLabel, 10, SpringLayout.NORTH, contentPane);

        layout.putConstraint(SpringLayout.WEST, nameField, 10, SpringLayout.EAST, nameLabel);
        layout.putConstraint(SpringLayout.NORTH, nameField, 0, SpringLayout.NORTH, nameLabel);

        layout.putConstraint(SpringLayout.WEST, submitButton, 0, SpringLayout.WEST, nameLabel);
        layout.putConstraint(SpringLayout.NORTH, submitButton, 10, SpringLayout.SOUTH, nameLabel);

        // Set the size and make the frame visible
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}
 

This example creates a simple UI with a label, a text field, and a button. It uses SpringLayout to position these components within the container.

Related questions

0 votes
1 answer
asked May 16, 2023 in JAVA by kvdevika (178k points)
0 votes
2 answers
asked May 16, 2023 in JAVA by kvdevika (178k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...