Java Box add to BoxLayout

Description

Java Box add to BoxLayout

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

import javax.swing.Box;
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(Box.createRigidArea(new Dimension(0, 10)));
    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"));
    //w  w w  .  j a  v a  2  s. c  o m
    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