Tải bản đầy đủ (.pdf) (57 trang)

Chương 6: Lập trình GUI (tt) ppsx

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 (739.56 KB, 57 trang )

Chương 6: L p trình GUI (tt)

GVLT: Tr n Anh Dũng

1


N i dung
Gi i thi u v Swing
Nh ng ñ c ñi m c a Swing
Các thành ph n GUI thông d ng c a Swing
Menu
H th ng menu
Các lo i menu
Các thành ph n c a menu
Menubar
MenuItems
M t s l p đ h a thơng d ng
2


Gi i thi u Swing (1)
AWT (Abstract Windows Toolkit)
Java 1
ðư c g n v i platform xác đ nh
Thích h p v i vi c phát tri n các ng d ng GUI đơn
gi n.
Swing
Java 2
Khơng g n v i platform c ñ nh
M nh, ña năng, linh ho t



3


Gi i thi u Swing (2)
Là m t gói n m trong thư vi n JFC (Java Foundation
Classes), g m nhi u l p (classes) và giao di n
(interfaces) h tr m nh m cho vi c l p trình giao di n
ñ h a trên JDK.
JFC là m t nhóm các thư vi n đư c thi t k ñ h tr
l p trình viên t o ra các ng d ng enterprise v i Java.
Swing ch là m t trong các thư vi n t o nên JFC.
javax.swing

4


Nh ng ñ c ñi m c a Swing (1)
C m quan (Look & Feels)
Kh năng vi t nh ng c m quan (Look & Feels) cho
m i thành ph n, th m chí thay đ i c m quan vào th i
đi m runtime.
Swing có kh năng th hi n nhi u L&F khác nhau và
hi n t i h tr các L&F bao g m Metal (m c ñ nh),
Motif và Windows.

5


Nh ng ñ c ñi m c a Swing (2)

Swing s
(MVC).

d ng ki n trúc Model - View - Controller

Swing có r t nhi u nh ng thành ph n m i
Table, Tree, Slider, Progress Bar, Spinner, Internal
frame và Text
Các thành ph n Swing có các Tooltip đ t bên trên
chúng. B n có th tùy ch n các s ki n bàn phím cho
các thành ph n, đ nh nghĩa chúng ho t ñ ng như th
nào v i nh ng phím nóng đã cho.

6


AWT & Swing
AWT v n ñư c h tr trong Java 2
Các thành ph n trong thư vi n Swing không th thay t t
c các thành ph n trong thư vi n AWT.
Chúng ch thay th m t ph n c a AWT như: Label,
Button, TextFeild, Panel...
Các l p tr giúp khác trong AWT như: Graphics,
Color, Font, FontMetrics,… v n khơng thay đ i.
Các thành ph n Swing s d ng mơ hình s lý s ki n
c a AWT.

7



Sơ ñ phân c p javax.swing

8


JComponent
JCheckBoxMenuItem
JMenuItem

JButton

AbstractButton

JMenu
JRadioButtonMenuItem

JToggleButton

JCheckBox
JRadioButton

JEditorPane

JComponent

JTextField

JTextComponent

JPasswordField


J TextArea

JLabel

JList

JTabbedPane

JComboBox

JMenuBar

JPanel

JOptionPane

JScrollBar

JScrollPane

JFileChooser

JPopupMenu

JSeparator

JSlider

JRootPane


JPane

JProgressBar

JToolBar

JSplitPane

JTable

JTree

JInternalFrame

JToolTip

JLayeredPane

JTableHeader

JColorChooser

9


Swing Components

Top-level
container


Menu Bar

Visible
Components

Content
pane

10


JComponent
M t s thành ph n GUI thư ng dùng:
JFrame, JPanel, JScrollPane
Jbutton, JLabel
JTextArea
Jcheckbox
JRadioButton
JList
JComboBox
...
11


JLabel
JLabel
JLabel (Icon img): Only Icon will be used for label.
JLabel (String str): Only text will be used for label.
JLabel (String str, Icon img, int align): Label will have

both text and icon. Alignment is specified by the align
argument and can be LEFT, RIGHT, CENTER,
LEADING or TRAILING. These are constants and are
defined in SwingConstant interface.
l.setToolTipText(“ToolTip");
l.setDisplayedMnemonic('e');
12


JButton
JButton
JButton(), JButton(Icon icon)
JButton(String text)
JButton(String text, Icon icon)
b1.setMnemonic(KeyEvent.VK_F);

13


JTextComponent
JTextComponent is the root class of all Swing text
components.
JTextField
JTextArea
JPasswordField
JPasswordField p = new JPasswordField(“123");
p.setEchoChar('a');

14



JCheckBox
JCheckBox class has the following constructors:
JCheckBox()
JCheckBox(Icon icon)
JCheckBox(Icon icon, boolean selected)
JCheckBox(String text)
JCheckBox(String text, boolean selected)
JCheckBox(String text, Icon icon)
JCheckBox(String text, Icon icon, boolean selected)

15


JRadioButton
JRadioButton object can be created by using:
JRadioButton()
JRadioButton(Icon icon)
JRadioButton(Icon, boolean selected)
JRadioButton(String text)
JRadioButton(String text, boolean selected)
JRadioButton(String text, Icon icon)
JRadioButton(String text, Icon icon, boolean selected)

16


JList
JFrame f = new JFrame("My frame");
String[ ] label = {"ASM","Pascal","C\\C++","VB","Java"};

JList l = new JList(label);
l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ScrollPane s = new ScrollPane();
s.add(l);
f.getContentPane().add(s);
f.setSize(100,100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
17


JComboBox
JFrame f = new JFrame("My frame");
JComboBox com = new JComboBox();
com.addItem("Item 1");
com.addItem("Item 2");
com.addItem("Item 3");
f.getContentPane().add(com);
f.getContentPane().setLayout(new FlowLayout());
f.setSize(100,100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

18


JTabbedPane
JFrame f = new JFrame("My frame");
String[ ] label={"ASM","Pascal","C\\C++","VB","Java"};
JButton[ ] b = new JButton[label.length];

JTabbedPane p = new JTabbedPane();
for(int i=0;ib[i] = new JButton(label[i]);
p.addTab(label[i], b[i]);
}
f.add(p);
f.setSize(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
19


JTable
JFrame f = new JFrame("My frame");
String[ ][ ] dat={{"1","JCreator","Xinox","Beginer"},
{"2","jGRASP","Auburn","Medium"},
{"3","NetBeans","Sun Microsystems","Expert"},
{"4","Gel","GExperts","Beginer"},
{"5","Eclipse","Eclipse","Expert"},
{"6","JBuilder","Borland","Expert"}};
String[ ] columnName = {"ID","Name","Company","Rank"};
JTable t = new JTable(dat,columnName);
JScrollPane s = new JScrollPane(t);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(s);
f.setSize(500,150);
f.setVisible(true);
20



JTable
String[ ][ ] dat = {{"1","JCreator","Xinox","Beginer"},
{"2","jGRASP","Auburn","Medium"},
{"3","NetBeans","Sun Micro","Expert"},
{"4","Gel","GExperts","Beginer"},
{"5","Eclipse","Eclipse","Expert"},
{"6","JBuilder","Borland","Expert"}};
String[ ] columnName = {"ID","Name","Company","Rank"};
JTable t = new JTable(dat,columnName);
JScrollPane s = new JScrollPane(t);
f.add(s);
JComboBox c = new JComboBox(new String[]{"Low","High","Extremely"});
TableColumn rankColumn = t.getColumn("Rank");
rankColumn.setCellEditor(new DefaultCellEditor((c));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,200);
f.setVisible(true);
21


JTree
Windows Explorer has a tree like structure depicting files
and folders.
Windows Explorer structures can be created in Java
using JTree.
Every row in the hierarchy is termed as a node.
When the nodes
are clicked

22



Menu
H th ng menu (Menu System): T p các m c ch n ch c
năng c a ng d ng ñư c t ch c phù h p.
Ngôn ng Java có m t t p h p các l p ñ i tư ng ñ t o
các menu.
Có hai lo i menu
Pull-down menu
Pop-up menu
Ch có th
đ t các menubar vào trong các
Frame/JFrame, M i Frame ch ch a duy nh t m t
menubar
23


C u trúc m t h menu

MenuBar

Các Menu

MenuItem

Thanh
phân
cách
24



Các l p liên quan ñ n menu

25


×