import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Main { public static void main(String[] args) { // create a JFrame JFrame frame = new JFrame("Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create a JPanel and add a JLabel to it JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JLabel("Hello World"), BorderLayout.CENTER); // add the JPanel to the JFrame Container contentPane = frame.getContentPane(); contentPane.add(panel); // get the layout manager of the JPanel System.out.println(panel.getLayout()); // show the JFrame frame.pack(); frame.setVisible(true); } }
import java.awt.FlowLayout; import java.awt.Container; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { public static void main(String[] args) { // create a JFrame JFrame frame = new JFrame("Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create a JPanel and add some JButtons to it JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.add(new JButton("Button 1")); panel.add(new JButton("Button 2")); panel.add(new JButton("Button 3")); // add the JPanel to the JFrame Container contentPane = frame.getContentPane(); contentPane.add(panel); // get the layout manager of the JPanel System.out.println(panel.getLayout()); // show the JFrame frame.pack(); frame.setVisible(true); } }This example creates a JFrame and adds a JPanel to it. The JPanel has a FlowLayout layout manager and some JButtons added to it. The getLayout() method is called on the JPanel to print out its layout manager. The output will be "java.awt.FlowLayout@