1. Diseño de borde
2. FlowLayout
3. Diseño de la cuadrícula
4. BoxLayout
5. Diseño vacío (nulo)
También hay otros dos diseños, a saber, GridbagLayout y CardLayout
Nota: Jframe y Jdialog son diseños como BorderLayout, JPanel y Applet son diseños como FlowLayout
Código de muestra de diseño de límite:
import java.awt.borderlout; import javax.swing.jbutton; import javax.swing.jframe; public class BorderLayouTexample extiende jframe {JButton Btn1 = new JButton ("East"); JButton BTN2 = new JButton ("Sur"); JButton BTN3 = new JButton ("Western"); JButton BTN4 = new JButton ("Norte"); JButton BTN5 = new JButton ("Medio"); BorderlayOutEmample () {init (); this.settitle ("BorderLayout"); this.setResizable (verdadero); this.setsize (200, 200); this.setLocationRelativeto (nulo); this.setDefaultCloseOperation (EXIT_ON_CLOSE); this.setVisible (verdadero); } void init () {this.setLayout (new BorderLayout (10,5)); // El valor predeterminado es 0, 0; espaciado horizontal 10, espaciado vertical 5 this.add (BTN1, BorderLayout.east); this.add (BTN2, BorderLayout.South); this.add (BTN3, BorderLayout.West); this.add (BTN4, BorderLayout.north); this.add (BTN5, BorderLayout.Center); } public static void main (string args []) {new BorderLayouTExample (); }}Resultados de ejecución:
Código de muestra de diseño de transmisión:
import java.awt.flowlayout; import javax.swing.jbutton; import javax.swing.jframe; public class FlowLayouTExample extiende jframe {jButton btn1 = new JButton ("one"); JButton BTN2 = new JButton ("Two"); JButton BTN3 = new JButton ("tres"); JButton BTN4 = new JButton ("cuatro"); JButton BTN5 = nuevo JButton ("cinco"); FlowLayouteExample () {init (); this.settitle ("FlowLayout"); this.setResizable (verdadero); this.setsize (200, 200); this.setLocationRelativeto (nulo); this.setDefaultCloseOperation (EXIT_ON_CLOSE); this.setVisible (verdadero); } void init () {this.setLayout (new FlowLayout (FlowLayout.left, 10,5)); // El valor predeterminado está centrado; espaciado horizontal 10, espaciado vertical 5 this.add (btn1); this.add (btn2); this.add (BTN3); this.add (btn4); this.add (btn5); } public static void main (string args []) {new FlowLayouTExample (); }}Resultados de ejecución:
Código de muestra de diseño de cuadrícula:
import java.awt.gridLayout; import javax.swing.jbutton; import javax.swing.jframe; public class GridLayouTeXample extiende jframe {jButton btn1 = new jButton ("one"); jButton btn2 = new jbutton ("dos"); jbutton btn3 = new jbutton ("" ") JBUTTON (" ") btn4 = new JButton ("cuatro"); JButton Btn5 = new JButton ("cinco"); GridLayouteExample () {init (); this.settitle ("Diseño de tabla"); this.setResizable (verdadero); this.setsize (300, 200); this.setLocationRelativeto (nulo); this.setDefaultCloseOperation (EXIT_ON_CLOSE); this.setVisible (true);} void init () {this.setLayout (nuevo GridLayout (2,3,10,5)); // El valor predeterminado es 1 fila, n columnas; 2 filas y 3 columnas, espaciado horizontal 10, espaciado vertical 5this.add (BTN1); this.add (btn2); this.add (BTN3); this.add (btn4); this.Add (btn5);} public static void main (string args []) {new GridLayouteExample ();}}Resultados de ejecución:
Código de muestra de diseño de caja:
import javax.swing.box; import javax.swing.boxLayout; import javax.swing.jbutton; import javax.swing.jframe; public class boxLayoOutExample extiende jframe {jButton btn1 = new jButton ("uno"); jButton btn2 = jButton ("dos"); jButton btn3 JButton ("tres"); jButton btn4 = new JButton ("cuatro"); JButton Btn5 = new JButton ("cinco"); boxLayouteExample () {init (); this.settitle ("Diseño de tabla"); this.setResizable (verdadero); this.setsize (300, 200); this.setLocationRelativeto (nulo); this.setDefaultCloseOperation (EXIT_ON_CLOSE); this.setVisible (true);} void init () {this.setLayout (new BoxLayout (this.getContentPane (), boxLayout.x_axis)); // puede usar el contenedor de cuadro en su lugar // box = nuevo Box (boxLayout.y_axis); box.add (btn ...); box.add (create ..); this.add (btn1); this.add (btn2); this.getContentPane (). Add (box.createHorizontalStrut (10)); // Al usar el diseño X, agregue componentes de ancho fijos para separar //this.getContentPane().Add(Box.CreateEverticalstrut(5)); // Cuando se usa el diseño y, agregue componentes de altura fijos para separar esto.add (btn3); this.add (btn4); this.add (btn5);} public static void main (string args []) {new BoxLayouDExample ();}}}}}}}}}}Resultados de ejecución:
Código de muestra de diseño vacío:
import javax.swing.jButton; import javax.swing.jframe; public class nullLayoutexample extiende jframe {jButton btn1 = new jButton ("one"); jButton btn2 = new jButton ("dos"); jButton btn3 = new jbutton ("tres"); jButton btn4 JButton ("cuatro"); jButton btn5 = new JButton ("cinco"); nullLayouteExample () {init (); this.settitle ("Diseño vacío"); this.setResizable (true); this.setsize (300, 300); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setVisible (true);} void init () {this.setLayout (null); btn1.setBounds (10, 0, 100, 50); // x coordenada 10, y coordenada 0, ancho de componente 100, altura 50btn2.setBounds (20, 50, 100, 50); btn3.setBounds (30, 100, 100, 50); btn4.setBounds (40, 150, 100, 50); btn5.setBounds (50, 200, 100, 50); Btn5. 50); this.Add (btn1); this.add (btn2); this.add (btn3); this.add (btn4); this.add (btn5);} pública estatic void main (string args []) {nuevo nullicouteutExample ();}}Resultados de ejecución: