In this video, we will create two AWT Panels with controls in it. Then we will add those to the AWT Frame Window. This video will help you understand how to use Nested Containers.
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 06: Implements ActionListener public class FrameWindow extends Frame implements WindowListener, ActionListener { //Sample 01: To Show data entered by user TextField txtOutput; //Sample 02: To Collect Information TextField txtRegNo; TextField txtName; TextField txtNationality; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(460, 300); setLocation(100,100); addWindowListener(this); //Sample 03: Create two Panels Panel TopPanel = new Panel(); Panel BottomPanel = new Panel(); //Sample 04: Prepare Top Panel TopPanel.setLayout(new GridLayout(1,1)); txtOutput = new TextField(); txtOutput.setForeground(Color.RED); TopPanel.add(txtOutput); add(BorderLayout.NORTH, TopPanel); //Sample 05: Prepare Bottom Panel //5.1: Grid First Column BottomPanel.setLayout(new GridLayout(4,2)); Label lblRegNo = new Label("Student Register Number:"); Label lblName = new Label("Student Name:"); Label lblNationality = new Label("Nationality:"); Label lblSave = new Label("Click here to Record =>"); //5.2: Grid Second Column txtRegNo = new TextField(); txtName = new TextField(); txtNationality = new TextField(); Button btnSave = new Button("Save Info"); //5.3: Fill the Bottom Panel's Grid BottomPanel.add(lblRegNo); BottomPanel.add(txtRegNo); BottomPanel.add(lblName); BottomPanel.add(txtName); BottomPanel.add(lblNationality); BottomPanel.add(txtNationality); BottomPanel.add(lblSave); BottomPanel.add(btnSave); //5.4: Add Buttom Panel to Frame Window add(BorderLayout.CENTER, BottomPanel); //Sample 08: Register the Button With Action Listener btnSave.addActionListener(this); } 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(); } @Override //Sample 07: Implement ActionListener public void actionPerformed(ActionEvent e) { //7.1: Form Output Text String output = "[RegNo: " + txtRegNo.getText() + "], " + "[Name: " + txtName.getText() + "], " + "[Nationality: " + txtNationality.getText() + "]"; //7.2: Set Text in the Top Panel txtOutput.setText(output); } } |
Categories: AWT-Tube