Example usage for javax.swing JPanel setLayout

List of usage examples for javax.swing JPanel setLayout

Introduction

In this page you can find the example usage for javax.swing JPanel setLayout.

Prototype

public void setLayout(LayoutManager mgr) 

Source Link

Document

Sets the layout manager for this container.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Focus Cycle Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(3, 3));

    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + i);
        frame.add(button);// w ww  . j a  va2s . co m
    }

    JPanel panel = new JPanel();
    panel.setFocusCycleRoot(true);
    panel.setFocusTraversalPolicyProvider(true);
    panel.setLayout(new GridLayout(1, 3));
    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 3));
        panel.add(button);
    }
    frame.add(panel);

    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 6));
        frame.add(button);
    }

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:org.eclipse.swt.snippets.Snippet337.java

public static void main(String args[]) {
    display = new Display();
    EventQueue.invokeLater(() -> {
        JFrame mainFrame = new JFrame("Main Window");
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainFrame.addWindowListener(new Snippet337.CloseListener());
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
        JButton launchBrowserButton = new JButton("Launch Browser");
        launchBrowserButton.addActionListener(e -> {
            JFrame childFrame = new JFrame();
            final Canvas canvas = new Canvas();
            childFrame.setSize(850, 650);
            childFrame.getContentPane().add(canvas);
            childFrame.setVisible(true);
            display.asyncExec(() -> {
                Shell shell = SWT_AWT.new_Shell(display, canvas);
                shell.setSize(800, 600);
                Browser browser = new Browser(shell, SWT.NONE);
                browser.setLayoutData(new GridData(GridData.FILL_BOTH));
                browser.setSize(800, 600);
                browser.setUrl("http://www.eclipse.org");
                shell.open();/* w  w  w .j ava 2  s. c o m*/
            });
        });

        mainPanel.add(new JTextField("a JTextField"));
        mainPanel.add(launchBrowserButton);
        mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER);
        mainFrame.pack();
        mainFrame.setVisible(true);
    });
    display.addListener(SWT.Close, event -> EventQueue.invokeLater(() -> {
        Frame[] frames = Frame.getFrames();
        for (int i = 0; i < frames.length; i++) {
            frames[i].dispose();
        }
    }));
    while (!display.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

From source file:BoxSample.java

public static void main(String args[]) {
    JButton button;/*from   w  w w.j  av  a2s.  c om*/
    Vector buttons = new Vector();
    Dimension dim;
    JFrame frame = new JFrame("Box Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
    JPanel topLeft = new JPanel();
    topLeft.setLayout(new BoxLayout(topLeft, BoxLayout.X_AXIS));
    topLeft.add(button = new JButton("One"));
    buttons.add(button);
    changeBoth(button);
    topLeft.add(button = new JButton("Two"));
    buttons.add(button);
    changeBoth(button);
    changeWidth(topLeft);
    JPanel bottomLeft = new JPanel();
    bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.X_AXIS));
    bottomLeft.add(button = new JButton("Six"));
    buttons.add(button);
    changeBoth(button);
    bottomLeft.add(button = new JButton("Seven"));
    buttons.add(button);
    changeBoth(button);
    changeWidth(bottomLeft);
    JPanel left = new JPanel();
    left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    left.add(topLeft);
    left.add(button = new JButton("Four"));
    buttons.add(button);
    changeBoth(button);
    left.add(bottomLeft);
    changeBoth(left);

    JPanel right = new JPanel();
    right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    right.add(button = new JButton("Three"));
    buttons.add(button);
    changeWidth(button);
    right.add(button = new JButton("Five"));
    buttons.add(button);
    changeBoth(button);
    changeBoth(right);
    contentPane.add(left);
    contentPane.add(right);
    tweak(buttons);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    layout.layoutContainer(container);/*from   w  ww .  j a  va  2  s.c o m*/
    container.setLayout(layout);

    for (int i = 0; i < labels.length; i++) {
        JButton button = new JButton(labels[i]);
        container.add(button);
    }

    frame.add(container, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    layout.invalidateLayout(container);/* w ww .  jav a 2 s. co  m*/
    container.setLayout(layout);

    for (int i = 0; i < labels.length; i++) {
        JButton button = new JButton(labels[i]);
        container.add(button);
    }

    frame.add(container, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    System.out.println(layout.getTarget());
    container.setLayout(layout);

    for (int i = 0; i < labels.length; i++) {
        JButton button = new JButton(labels[i]);
        container.add(button);/*w w w  . j a  v  a2 s.co  m*/
    }

    frame.add(container, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:CircleLayoutDemo.java

public static void main(String a[]) {
    JFrame frame = new JFrame();
    JPanel circle = new JPanel();
    circle.setLayout(new CircleLayout(true));

    for (int i = 0; i < 10; i++) {
        circle.add(new JLabel("circle_" + i * 7));
    }// ww w .j a  v a 2  s  . c o  m
    frame.setContentPane(circle);
    frame.setSize(200, 400);

    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.RED));
    BoxLayout mgr = new BoxLayout(panel, BoxLayout.Y_AXIS);
    panel.setLayout(mgr);
    for (int i = 0; i < 5; i++) {
        JButton button = new JButton("Remove Hello World " + (i + 1));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(e -> {
            panel.remove(button);//from  ww  w. ja  v a  2 s.  co  m
            panel.revalidate();
            panel.repaint();
        });
        panel.add(button);
    }
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int BTN_COUNT = 3;
    int VERT_GAP = 10;
    int EB_GAP = 5;
    float TITLE_SIZE = 36f;
    String TITLE_TEXT = "This is my Title";

    JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
    titleLabel.setFont(titleLabel.getFont().deriveFont(TITLE_SIZE));
    JPanel titlePanel = new JPanel();
    titlePanel.add(titleLabel);//from  w ww .  jav a2  s .c o  m

    JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
    for (int i = 0; i < BTN_COUNT; i++) {
        JButton btn = new JButton("Button " + (i + 1));
        buttonPanel.add(btn);
    }

    JTextArea textArea = new JTextArea(20, 30);

    JPanel mainPanel = new JPanel();
    mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    mainPanel.add(titlePanel);
    mainPanel.add(Box.createVerticalStrut(VERT_GAP));
    mainPanel.add(buttonPanel);
    mainPanel.add(Box.createVerticalStrut(VERT_GAP));
    mainPanel.add(new JScrollPane(textArea));

    JFrame frame = new JFrame();
    frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    System.out.println(layout.minimumLayoutSize(container));
    container.setLayout(layout);

    for (int i = 0; i < labels.length; i++) {
        JButton button = new JButton(labels[i]);
        container.add(button);/*from www .jav a 2s . co m*/
    }

    frame.add(container, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
}