Border Layout
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
{
public Main()
{
setTitle("BorderLayout Practice");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout(5, 7));
c.add(new JButton("North"), BorderLayout.NORTH);
c.add(new JButton("West"), BorderLayout.WEST);
c.add(new JButton("East"), BorderLayout.EAST);
c.add(new JButton("Center"), BorderLayout.CENTER);
c.add(new JButton("South"), BorderLayout.SOUTH);
setSize(700, 500);
setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}
Grid Layout
import javax.swing.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
public class Main extends JFrame
{
public Main()
{
setTitle("Ten Color Buttons Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(1,11));
JButton z = new JButton("0");
z.setBackground(Color.YELLOW);
z.setForeground(Color.RED);
c.add(z);
String[] str = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
for(int i = 0; i < 10; i++
{
c.add(new JButton(str[i]));
}
setSize(700,500);
setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}