Java by API/java.awt/BorderLayout
Содержание
BorderLayout.BEFORE_FIRST_LINE
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Name:");
JTextField text = new JTextField();
topPanel.add(label, BorderLayout.BEFORE_LINE_BEGINS);
topPanel.add(text, BorderLayout.CENTER);
outerPanel.add(topPanel, BorderLayout.BEFORE_FIRST_LINE);
frame.add(outerPanel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
BorderLayout.BEFORE_LINE_BEGINS
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Name:");
JTextField text = new JTextField();
topPanel.add(label, BorderLayout.BEFORE_LINE_BEGINS);
topPanel.add(text, BorderLayout.CENTER);
outerPanel.add(topPanel, BorderLayout.BEFORE_FIRST_LINE);
frame.add(outerPanel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
BorderLayout.CENTER
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JToggleButton Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JToggleButton("North"), BorderLayout.NORTH);
f.add(new JToggleButton("East"), BorderLayout.EAST);
f.add(new JToggleButton("West"), BorderLayout.WEST);
f.add(new JToggleButton("Center"), BorderLayout.CENTER);
f.add(new JToggleButton("South"), BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
BorderLayout.EAST
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JToggleButton Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JToggleButton("North"), BorderLayout.NORTH);
f.add(new JToggleButton("East"), BorderLayout.EAST);
f.add(new JToggleButton("West"), BorderLayout.WEST);
f.add(new JToggleButton("Center"), BorderLayout.CENTER);
f.add(new JToggleButton("South"), BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
BorderLayout.NORTH
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JToggleButton Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JToggleButton("North"), BorderLayout.NORTH);
f.add(new JToggleButton("East"), BorderLayout.EAST);
f.add(new JToggleButton("West"), BorderLayout.WEST);
f.add(new JToggleButton("Center"), BorderLayout.CENTER);
f.add(new JToggleButton("South"), BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
BorderLayout.SOUTH
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JToggleButton Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JToggleButton("North"), BorderLayout.NORTH);
f.add(new JToggleButton("East"), BorderLayout.EAST);
f.add(new JToggleButton("West"), BorderLayout.WEST);
f.add(new JToggleButton("Center"), BorderLayout.CENTER);
f.add(new JToggleButton("South"), BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
BorderLayout.WEST
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class MainClass {
public static void main(String args[]) {
JFrame f = new JFrame("JToggleButton Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JToggleButton("North"), BorderLayout.NORTH);
f.add(new JToggleButton("East"), BorderLayout.EAST);
f.add(new JToggleButton("West"), BorderLayout.WEST);
f.add(new JToggleButton("Center"), BorderLayout.CENTER);
f.add(new JToggleButton("South"), BorderLayout.SOUTH);
f.setSize(300, 200);
f.setVisible(true);
}
}
new BorderLayout()
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public MainClass() {
JButton btn1 = new JButton("Button1");
JButton btn2 = new JButton("Button2");
JButton btn3 = new JButton("Button3");
JButton btn4 = new JButton("Button4");
JButton btn5 = new JButton("Button5");
JButton btn6 = new JButton("Button6");
setLayout(new BorderLayout());
add("North", btn1);
add("West", btn2);
add("Center", btn3);
add("Center", btn4);
add("South", btn5);
add("East", btn6);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}