Learning Java Swing Absolute Layout by example provided below. YouTube video also provided with detail explanation.
Prerequisite: If you are new to Java Swings; Recommended to first visit Hello World Swing Application;
Below is the sample code where JFrame created and fixed size 300 width and 200 height provided by frame.setSize
JLabel is created and added to the pane using pane.add method.
//Create and set up the window. JFrame frame = new JFrame("Age Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. pane.setLayout(null); JLabel l1 = new JLabel("My Label"); pane.add(l1); Insets insets = pane.getInsets(); Dimension size = l1.getPreferredSize(); l1.setBounds(25 + insets.left, 5 + insets.top, size.width, size.height); //Size and display the window. Insets insets = frame.getInsets(); frame.setSize(300 + insets.left + insets.right, 200 + insets.top + insets.bottom); frame.setVisible(true);
When to use Absolute Layout depends on complexity and size of you screens. If it is simple and fixed and you don’t anticipate change often, then this can work. Advantage of this layout is simple and basic.
Note:If you are building large and complex screen applications, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container’s changing size, and to different locales.
Refer YouTube tutorial with detailed explanation
For official Java Docs refer https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
For further reading refer Java Programming Category posts.