1. Grenzlayout
2. Flowlayout
3. Grid -Layout
4. BoxLayout
5. leeres Layout (Null)
Es gibt auch zwei weitere Layouts, nämlich Gridbaglayout und Cardlayout
HINWEIS: JFRAME und JDIALOG sind Layouts als BorderLayout, JPanel und Applet sind Layouts als Flowlayout
Grenzlayout -Beispielcode:
Import Java.awt.BorderLayout; Import javax.swing.jbutton; import Javax.swing.jframe; öffentliche Klasse BorderLayoutExample erweitert JFrame {Jbutton btn1 = new Jbutton ("East"); Jbutton Btn2 = neuer Jbutton ("South"); Jbutton Btn3 = neuer Jbutton ("Western"); Jbutton Btn4 = neuer Jbutton ("North"); Jbutton Btn5 = neuer Jbutton ("Medium"); BorderLayoutExample () {init (); this.settitle ("BorderLayout"); this.setResizable (true); this.setsize (200, 200); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setvisible (true); } void init () {this.setLayout (New BorderLayout (10,5)); // Standard ist 0, 0; Horizontaler Abstand 10, vertikaler Abstand 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 (); }}Auslaufergebnisse:
Streaming -Layout -Beispielcode:
importieren java.awt.flowlayout; import javax.swing.jbutton; import javax.swing.jframe; public class flowlayoutExample erweitert jframe {jbutton btn1 = new Jbutton ("eins"); Jbutton Btn2 = neuer Jbutton ("zwei"); Jbutton Btn3 = neuer Jbutton ("drei"); Jbutton Btn4 = neuer Jbutton ("Four"); Jbutton Btn5 = neuer Jbutton ("fünf"); FlowLayOutExample () {init (); this.settitle ("FlowLayout"); this.setResizable (true); this.setsize (200, 200); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setvisible (true); } void init () {this.setLayout (neuer FlowLayout (FlowLayout.Left, 10,5)); // Standard ist zentriert; Horizontaler Abstand 10, vertikaler Abstand 5 this.add (btn1); this.add (btn2); this.add (btn3); this.add (btn4); this.add (btn5); } public static void main (String args []) {new FlowLayoutExample (); }}Auslaufergebnisse:
Grid -Layout -Beispielcode:
Import Java.awt.GridLayout; Import Javax.swing.jbutton; Import Javax.swing.jframe; Public Class GridLayoutExample erweitert JFrame {Jbutton btn1 = new Jbutton ("eins"); jbutton btn2 = new jbutton ("two"); Jbutton ("drei"); Jbutton btn4 = new Jbutton ("vier"); this.settitle ("Tischlayout"); this.setResizable (true); this.setsize (300, 200); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setvisible (true);} void init () {this.setLayout (neuer GridLayout (2,3,10,5)); // Standard ist 1 Zeile, n Spalten; 2 Zeilen und 3 Säulen, horizontaler Abstand 10, vertikaler Abstand 5this.Add (Btn1); this.add (btn2); this.add (btn3); this.add (btn4); this.add (btn5);} public static void main (String args []) {new GridLayoutExample ();}}Auslaufergebnisse:
Box -Layout -Beispielcode:
import Javax.swing.box; import Javax.swing.boxLayout; Import Javax.swing.jbutton; Import Javax.swing.jframe; Public Class BoxLayoutExample erweitert JFrame {Jbutton btn1 = new Jbutton ("One"); Jbutton ("drei"); Jbutton btn4 = new Jbutton ("vier"); this.settitle ("Tischlayout"); this.setResizable (true); this.setsize (300, 200); this.setLocationRelativeto (null); this.setDefaultCloseOperation (exit_on_close); this.setvisible (true);} void init () {this.setLayout (neuer BoxLayout (this.getContentPane (), boxLayout.x_axis); // Box (boxLayout.y_axis); box.add (btn ...); box.add (creat ..); this.add (btn1); this.add (btn2); this.getContentPane (). // Bei Verwendung von X -Layout die Komponenten der festen Breite hinzufügen //this.getContentPane().add(box.createverticalstrut(5)); // Fügen Sie bei Verwendung von Y -Layout feste Höhenkomponenten hinzu, um dies zu trennen.Auslaufergebnisse:
Leerer Layout -Beispielcode:
import Javax.swing.jbutton; Import Javax.swing.jframe; öffentliche Klasse nullLayoutExample erweitert JFrame {Jbutton Btn1 = neuer Jbutton ("eins"); Jbutton ("vier"); Jbutton btn5 = neuer Jbutton ("fünf"); 300; // x Koordinate 10, y Koordinate 0, Komponentenbreite 100, Höhe 50BTN2.Setbounds (20, 50, 100, 50); Btn3.Setbounds (30, 100, 50); Btn4.Setbounds (40, 150, 100, 50); Btn5.Setbounds (50, 200, 100, 50, 50); 50); this.add (btn1); this.add (btn2); this.add (btn3);Auslaufergebnisse: