APJI-Lab3-Layout Manager, Swing Menu Application Programming I Module – Layout Manager Module – Swing Menu Component Lab Guide for Session Session Objectives In this session, you will be practicing with Different types of Layout Manager:FlowLayout, BorderLayout, GridLayout CardLayout, GroupLayout Menu component:JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem, JRadioButtonMenuItem, JPopupHenu JFileChooser and its subclasses JToolBar Part – Getting started (30 minutes) Create an application that shows 20 buttons in a frame using FlowLayout(10 minutes) Scan the code first, type the code, compile, run and observe the result import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; public class TestFlowLayout extends javax.swing.JFrame { /** Creates new form TestFlowLayout */ public TestFlowLayout() { Container cp = getContentPane(); cp.setLayout(new FlowLayout()); for (int i = 0; i < 20; i++) { cp.add(new JButton("Button " + i)); } pack(); setDefaultCloseOperation( javax.swing.WindowConstants.EXIT_ON_CLOSE); } public static void main(String args[]) { new TestFlowLayout().setVisible(true); } © 2009 FPT-Aptech Page / APJI-Lab3-Layout Manager, Swing Menu } Create an application that shows buttons in a frame using BorderLayout(10 minutes) Scan the code first, type the code, compile, run and observe the result import import import import java.awt.BorderLayout; java.awt.Container; javax.swing.JButton; javax.swing.JFrame; public class TestBorderLayout extends JFrame{ public TestBorderLayout() { setLayout(new BorderLayout()); Container c= getContentPane(); c.add(new JButton("EAST"), BorderLayout.EAST); c.add(new JButton("WEST"), BorderLayout.WEST); c.add(new JButton("NORTH"), BorderLayout.NORTH); c.add(new JButton("SOUTH"), BorderLayout.SOUTH); c.add(new JButton("CENTER"), BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); } public static void main(String[] args) { new TestBorderLayout().setVisible(true); } } Create an application with a frame and menu structure(10 minutes): Subjects Physics Metaphysics Astrophysics Biology Microbiology Biotechnology Chemistry © 2009 FPT-Aptech Page / APJI-Lab3-Layout Manager, Swing Menu Organic Inorganic import javax.swing.*; import java.awt.*; public class MenuTest extends JFrame{ MenuTest() { JMenuBar mb = new JMenuBar(); JMenu subjects = new JMenu("Subjects"); JMenu submenu1 = new JMenu("Physics"); submenu1.add(new JCheckBoxMenuItem("Metaphysics")); submenu1.add(new JCheckBoxMenuItem("Astrophysics")); subjects.add(submenu1); submenu1 = new JMenu("Biology"); submenu1.add(new JCheckBoxMenuItem("Microbiology")); submenu1.add(new JCheckBoxMenuItem("Biotechnology")); subjects.add(submenu1); submenu1 = new JMenu("Chemistry"); submenu1.add(new JCheckBoxMenuItem("Organic")); submenu1.add(new JCheckBoxMenuItem("Inorganic")); subjects.add(submenu1); mb.add(subjects); setJMenuBar(mb); setTitle("MenuTest"); setSize(200,200); } public static void main(String[] args){ new MenuTest().setVisible(true); } } Part – Workshops (30 minutes) Quickly look at workshops for Module and Module for reviewing basic steps Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module and Module carefully Discuss with your class-mates and your instructor if needed © 2009 FPT-Aptech Page / APJI-Lab3-Layout Manager, Swing Menu Part – Do it your self Create this form by two ways not using any tool and using a tool like Netbeans Create another form © 2009 FPT-Aptech Page / APJI-Lab3-Layout Manager, Swing Menu Continue creating this form Create a window with menu system like the Notepad application © 2009 FPT-Aptech Page /