Friday, 26 January 2018

Border layout in java

Border layout in java
import java.awt.*;
import javax.swing.*;

public class BorderLayoutExample extends JFrame {

  public static void main(String[] args) {

    //JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame bs = new JFrame();
    bs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = bs.getContentPane();

    pane.setLayout(new BorderLayout());

    JButton label = new JButton("North");
    label.setFont(new Font("Courier", Font.BOLD, 36));
    pane.add(label, BorderLayout.NORTH);

    label = new JButton("South");
    label.setFont(new Font("Courier", Font.BOLD, 36));
    pane.add(label, BorderLayout.SOUTH);

    label = new JButton("East");
    label.setFont(new Font("Courier", Font.BOLD, 36));
    pane.add(label, BorderLayout.EAST);

    label = new JButton("West");
    label.setFont(new Font("Courier", Font.BOLD, 36));
    pane.add(label, BorderLayout.WEST);

    label = new JButton("Center");
    label.setFont(new Font("Courier", Font.BOLD, 36));
    pane.add(label, BorderLayout.CENTER);

    bs.setSize(400, 300);
    bs.setVisible(true);
  }

}

Box layout

Box layout
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class BoxLayoutTest {

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("BoxLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BoxLayout boxLayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); // top to bottom
    frame.setLayout(boxLayout);
    frame.add(new JButton("Button 1"));
    frame.add(new JButton("Button 2"));
    frame.add(new JButton("Button 3"));
    frame.pack();

    frame.setVisible(true);
  }
}

Box Layout in java

Box Layout in java
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FlowLayoutTest {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("FlowLayout Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "Hello this is a testing";
    JTextArea textArea1 = new JTextArea(text, 5, 10);
    textArea1.setPreferredSize(new Dimension(100, 100));
    JTextArea textArea2 = new JTextArea(text, 5, 10);
    textArea2.setPreferredSize(new Dimension(100, 100));
    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textArea1.setLineWrap(true);
    textArea2.setLineWrap(true);
    frame.add(textArea1);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
  }
}

Grid layout

Grid layout
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridLayoutTest {

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GridLayout Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 2));
    frame.add(new JButton("Button 1"));
    frame.add(new JButton("Button 2"));
    frame.add(new JButton("Button 3"));
    frame.add(new JButton("Button 4"));
    frame.add(new JButton("Button 5"));
    frame.add(new JButton("Button 6"));
    frame.add(new JButton("Button 7"));
    frame.add(new JButton("Button 8"));
    frame.pack();
    frame.setVisible(true);
  }
}

Grid layout examples in java

Grid layout examples in java
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridTest extends JFrame {

  public static void main(String[] args) {
    int rows = 2;
    int cols = 3;
    GridTest gt = new GridTest(rows, cols);
    gt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gt.pack();
    gt.setVisible(true);
  }

  public GridTest(int rows, int cols) {
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(rows, cols));
    for (int i = 0; i < 20; i++) {
      JButton button = new JButton(Integer.toString(i + 1));
      pane.add(button);
    }
  }

}

Layouts in java

Layouts in java
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.TextField;

// how to use flowlayout
public class Main {
  public static void main(String[] args) {
    Frame f = new Frame("FlowLayout demo");
   
   
    f.setLayout(new FlowLayout());
    f.add(new Button("Red"));
    f.add(new Button("Blue"));
    f.add(new Button("White"));
    List list = new List();
    for (int i = 0; i < args.length; i++) {
      list.add(args[i]);
    }
    f.add(list);
    f.add(new Checkbox("Pick me", true));
    f.add(new Label("Enter name here:"));
    f.add(new TextField(20));
    f.pack();
    f.setVisible(true);
  }
}