Java Swing BoxLayout class

Description

Java Swing BoxLayout class

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Demo extends JPanel {
  public Demo() {
    JPanel patternPanel = new JPanel();
    patternPanel.setLayout(new BoxLayout(patternPanel,BoxLayout.PAGE_AXIS));
    patternPanel.add(new JLabel("demo2s.com"));
    patternPanel.add(new JButton("demo2s.com"));
    patternPanel.add(new JLabel("demo2s.com"));
    patternPanel.add(new JLabel("demo2s.com test"));
    patternPanel.add(new JLabel("demo2s.com test test"));
    patternPanel.add(new JLabel("demo2s.com test"));
    patternPanel.add(new JLabel("demo2s.com"));
    /*from  w w w. j ava  2s  . c  om*/
    patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

    setLayout(new FlowLayout());
    add(patternPanel);
  }
}

public class Main {
  public static void main(String[] args) {
    Demo panel = new Demo();
    JFrame application = new JFrame();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
  }
}



PreviousNext

Related