In this video, we will create 5 java AWT Buttons and hand-over that to FlowLayout Manager. Then, we will study how this layout manager is organizing these button when the Frame Window is resized. We will also learn about the control alignment and adjust gap between the buttons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package AwtDemoPkg; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class FrameWindow extends Frame implements WindowListener { public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(460, 170); setLocation(100,100); addWindowListener(this); //Sample 01: Create 5 Buttons Button btn1 = new Button("Button 01"); Button btn2 = new Button("Button 02"); Button btn3 = new Button("Button 03"); Button btn4 = new Button("Button 04"); Button btn5 = new Button("Button 05"); //Sample 02: Set the Layout Manager setLayout(new FlowLayout()); //Sample 03: Add All the Buttons add(btn1); add(btn2); add(btn3); add(btn4); add(btn5); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } } |
Variation 1
1 2 3 4 |
//Sample 02: Set the Layout Manager FlowLayout flowmanager = new FlowLayout(); flowmanager.setAlignment(FlowLayout.LEFT); //Try with Right setLayout(flowmanager); |
Variation 2
1 2 3 4 5 |
//Sample 02: Set the Layout Manager FlowLayout flowmanager = new FlowLayout(); flowmanager.setHgap(10); flowmanager.setVgap(10); setLayout(flowmanager); |
Categories: AWT-Tube