Tải bản đầy đủ (.doc) (5 trang)

APJI lab3

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (207.93 KB, 5 trang )

APJI-Lab3-Layout Manager, Swing Menu

Application Programming I
Module 3 – Layout Manager
Module 4 – Swing Menu Component
Lab Guide for Session 3

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 1 – Getting started (30 minutes)
1. 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 1 / 5


APJI-Lab3-Layout Manager, Swing Menu

}
2. Create an application that shows 5 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);
}
}

3. Create an application with a frame and menu structure(10 minutes):
Subjects
Physics
Metaphysics
Astrophysics
Biology
Microbiology
Biotechnology
Chemistry

© 2009 FPT-Aptech

Page 2 / 5



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 2 – Workshops (30 minutes)



Quickly look at workshops for Module 3 and Module 4 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 3 – Lab Assignment (60 minutes)
Do the assignment for Module 3 and Module 4 carefully. Discuss with your class-mates and
your instructor if needed.
© 2009 FPT-Aptech

Page 3 / 5


APJI-Lab3-Layout Manager, Swing Menu

Part 4 – Do it your self
1. Create this form by two ways not using any tool and using a tool like Netbeans

2. Create another form

© 2009 FPT-Aptech

Page 4 / 5



APJI-Lab3-Layout Manager, Swing Menu

3. Continue creating this form

4. Create a window with menu system like the Notepad application

© 2009 FPT-Aptech

Page 5 / 5



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×