In this video, we will create Wizard Pages using Java AWT CardLayout Manager and Panels. You will learn how Next() and Previous() methods are used to flip the cards which looks like user going through the Wizard Pages or Property Sheets and Pages.
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Button; import java.awt.CardLayout; import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.FlowLayout; 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; public class FrameWindow extends Frame implements WindowListener, ActionListener { //Sample 00: Declarations Panel WizardPan; CardLayout Sheets; int SheetNo = 0; Button btnPrev; Button btnNext; Button btnApply; Panel CommandPanel; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(460, 200); setLocation(100,100); addWindowListener(this); //Sample 01: Create three Panels for Cards Panel ThemeNamePan = new Panel(); Panel ThemeFontPan = new Panel(); Panel ThemeColorPan = new Panel(); //Sample 02: Create Panel for Button Controls CommandPanel = new Panel(); //Sample 03: Prepare Theme Name Panel ThemeNamePan.setLayout(new GridLayout(5,1)); Label lblThemeName = new Label("Specify Theme Name:"); Label lblUsage = new Label("Usage:"); TextField txtThemeName = new TextField(30); CheckboxGroup chkgUsage = new CheckboxGroup(); Checkbox chkCurrentUser = new Checkbox("Current User", false, chkgUsage); Checkbox chkAllUser = new Checkbox("All User", false, chkgUsage); ThemeNamePan.add(lblThemeName); ThemeNamePan.add(txtThemeName); ThemeNamePan.add(lblUsage); ThemeNamePan.add(chkCurrentUser); ThemeNamePan.add(chkAllUser); //add(ThemeNamePan); //Sample 04: Prepare Font Name Panel ThemeFontPan.setLayout(new GridLayout(2,2)); Label lblFontName = new Label("Font Name: "); Label lblSize = new Label("Size: "); TextField txtFontName = new TextField(20); TextField txtSize = new TextField(2); ThemeFontPan.add(lblFontName); ThemeFontPan.add(txtFontName); ThemeFontPan.add(lblSize); ThemeFontPan.add(txtSize); //add(ThemeFontPan); //Sample 05: Prepare ThemeColor Panel ThemeColorPan.setLayout(new GridLayout(4,1)); Label lblColor = new Label("Pick a Theme Colour Below:"); CheckboxGroup chkgThemeColor = new CheckboxGroup(); Checkbox chkDark = new Checkbox("Dark", false, chkgThemeColor); Checkbox chkLight = new Checkbox("Light", false, chkgThemeColor); Checkbox chkWindows = new Checkbox("Windows", false, chkgThemeColor); ThemeColorPan.add(lblColor); ThemeColorPan.add(chkDark); ThemeColorPan.add(chkLight); ThemeColorPan.add(chkWindows); add(ThemeColorPan); //Sample 06: Prepare the Command Panel CommandPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); btnPrev = new Button("< Previous"); btnNext = new Button("Next >"); btnApply = new Button("Apply"); btnPrev.setActionCommand("Prev"); btnNext.setActionCommand("Next"); btnApply.setActionCommand("Apply"); CommandPanel.add(btnPrev); CommandPanel.add(btnNext); CommandPanel.add(btnApply); btnPrev.addActionListener(this); btnNext.addActionListener(this); btnApply.addActionListener(this); btnPrev.setEnabled(false); btnApply.setEnabled(false); add(BorderLayout.SOUTH, CommandPanel); //Sample 07:Create Panel for CardLayout //7.1 Create a Panel for Card Layout WizardPan = new Panel(); Sheets = new CardLayout(); WizardPan.setLayout(Sheets); //7.2 Add each panel as cards or sheets WizardPan.add(ThemeNamePan); WizardPan.add(ThemeFontPan); WizardPan.add(ThemeColorPan); //7.3 Display this Panel in the Center // of the Frame Window add(BorderLayout.CENTER, WizardPan); } 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 public void actionPerformed(ActionEvent e) { //Sample 08: Enable or Disable the Command Panel // Flip the sheets String str = e.getActionCommand(); switch(str) { case "Prev": Sheets.previous(WizardPan); btnNext.setEnabled(true); SheetNo--; break; case "Next": Sheets.next(WizardPan); btnPrev.setEnabled(true); SheetNo++; break; case "Apply": break; } if (SheetNo == 0) btnPrev.setEnabled(false); else btnPrev.setEnabled(true); if (SheetNo == 2) { btnApply.setEnabled(true); btnNext.setEnabled(false); } else { btnApply.setEnabled(false); btnNext.setEnabled(true); } } } |
Categories: AWT-Tube