Tập hợp các lớp java cung cấp hỗ trợ thiết kế, xây dựng
GUI (Graphic User Interface) bao gồm 2 thư viện:
TextArea
Graphics List
Component Choice
CheckBox
LayoutManager CheckBoxGroup
Canvas
MenuBar
Scrollbar
Nguyên tắc xây dựng GUI
#8
y screen
x
screen height
MyWindow height
width
screen width
Top-level Container
#12
• Frame
• Dialog
• Applet
Top-level Container
#13
• Người dùng tương tác với chương trình qua giao diện
}
KeyListener
#20
}
Xử lý sự kiện
#21
• Frame được dùng để xây dựng các ứng dụng GUI chạy
độc lập
• Một giao diện có thể có nhiều panel sắp xếp theo một
layout nhất định, mỗi panel lại có các component sắp
xếp theo một layout riêng
Panel(BorderLayout)
TextField
Button
12 Button
Panel(GridLayout)
Frame(BorderLayout)
Ví dụ Panel
#27
import java.awt.*;
import java.awt.*;
public class ViDuDialog extends Frame {
public ViDuDialog(){
super("Vi du Frame co chua Dialog");
this.setSize(300, 200);
Dialog dialog = new Dialog(this, "Vi du Dialog“
, false);
dialog.setSize(200, 100);
dialog.setVisible(true);
}
public static void main(String []args){
ViDuDialog vd= new ViDuDialog();
vd.setVisible(true);
}
}
Thêm đối tượng component
#30
2. Khởi tạo đối tượng với các phương thức khởi tạo phù
hợp
• Label dùng để trình bày một chuỗi văn bản ra màn hình
• ...
Ví dụ Label
#32
import java.awt.Frame;
import java.awt.Label;
myFrame.add(myLabel);
myFrame.setVisible(true);
}
}
java.awt.Button
#33
• Để lắng nghe sự kiện nhấn nút ta cần cài đặt giao tiếp
ActionListener
Ví dụ Button
#34
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ViDuButton extends Frame
implements ActionListener {
Button myButton;
public ViDuButton() {
super("Frame co chua Button");
this.setSize(300, 200);
myButton = new Button("Button");
myButton.setSize(50, 10);
this.add(myButton);
this.setVisible(true);
myButton.addActionListener(this);
}
Ví dụ Button
#35
• Text Field (ô văn bản) cho phép nhận dữ liệu từ bàn phím trên
một dòng
• Một số phương thức
– TextField(...); // khởi tạo
– void setEditable(boolean b); // đặt/tắt chế độ nhập
– void setEchoChar(char c); // đặt kí tự hiển thị
• Đối tượng nghe cần cài đặt 2 giao tiếp
– ActionListener
– TextListener
• Cài đặt phương thức textValueChanged();
Ví dụ Text Field
#37
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
• Choice cung cấp khả năng lựa chọn một trong số các
mục trong danh sách sổ xuống
• Một số phương thức
• Choice(); // khởi tạo
• void addItem(String s); // thêm item là s
• String getItem(int index);// lấy item có chỉ số index
• String getSeclectedItem(); // trả về item được chọn
• int getSelectedIndex(); // trả về index của item được chọn
@Override
public void itemStateChanged(ItemEvent e) {
//Xử lý sự kiện chọn item
}
}
java.awt.Checkbox
#42
• Checkbox cung cấp các hộp tuỳ chọn cho người dùng
đánh dấu
import java.awt.Checkbox;
import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
@Override
public void itemStateChanged(ItemEvent e) {
//Định nghĩa xử lý sự kiện chọn
}
}
Radio Button
#45
@Override
public void itemStateChanged(ItemEvent e) {
//Định nghĩa xử lý thay đổi lựa chọn
}
}
java.awt.List
#48
• List cho phép người dùng chọn một hay nhiều item
từ một danh sách các item
• Một số phương thức
– List(); // khởi tạo
– List(int items, boolean ms); // cấu tử mở rộng
– String getSeclectedItem(); // lấy lại thành phần được chọn
@Override
public void actionPerformed(ActionEvent e) {
// Xử lý sự kiện
}
@Override
public void itemStateChanged(ItemEvent e) {
// Xử lý sự kiện
}
• Flow Layout
• Border Layout
• Grid Layout
• Gridbag Layout
• Null Layout
FlowLayout
#52
• Cho phép sắp xếp và thay đổi kích thước của những
component chứa trong nó sao cho vừa với 5 vùng
tương ứng
public ViDuGridLayout() {
super("Vi du Grid Layout");
this.setSize(600, 300);
this.setLayout(new GridLayout(4, 2, 20, 10));
myButton = new Button[5];
for(int i=0; i<5; i++){
myButton[i]=new Button("Button " + (i+1));
this.add(myButton[i]);
}
myCheckbox = new Checkbox("Checkbox", true);
#62 myText =new TextField("Enter text here");
myList = new List(3);
myList.add("List 1");
myList.add("List 2");
myList.add("List 3");
this.add(myCheckbox);
this.add(myText);
this.add(myList);
}
cst.gridx = 2;
cst.gridy= 0;
cst.weightx = 0.0;
cst.weighty = 0.0;
cst.gridwidth = 1;
cst.gridheight = 1;
layout.setConstraints(myButton[1], cst);
cst.gridx =3;
cst.gridy=0;
cst.weightx = 0.0;
cst.weighty = 0.5;
cst.gridwidth = 1;
cst.gridheight = 3;
layout.setConstraints(myButton[2], cst);
#67
cst.gridx =0;
cst.gridy=1;
cst.weightx = 0.0;
cst.weighty = 0.0;
cst.gridwidth = 1;
cst.gridheight = 1;
layout.setConstraints(myButton[3], cst);
cst.gridx = 1;
cst.gridy= 1;
cst.weightx = 0.5;
cst.weighty = 0.0;
cst.gridwidth = 2;
cst.gridheight = 1;
layout.setConstraints(myButton[4], cst);
cst.gridx = 0;
#68
cst.gridy= 2;
cst.weightx = 0.5;
cst.weighty = 0.0;
cst.gridwidth = 3;
cst.gridheight = 1;
layout.setConstraints(myButton[5], cst);
• Phải qui định kích thước của khung chứa và kích thước, vị
trí của từng đối tượng component trong khung chứa
Thiết kế giao diện và xử lý cho bài toán cộng, trừ, nhân và chia
2 số được nhập từ 2 TextField, gồm các thành phần:
• 2 ô nhập liệu
• Các nút nhấn: cộng, trừ, nhân, chia và thoát chương trình
!!!Đối với phép chia: nếu số chia = 0 thì hiển thị hộp thoại
thông báo lỗi
Bài tập về nhà
#73
Thiết kế giao diện và viết chương trình giả lập phần mềm
calculator
Menu
#74
• Menubar
• Menu
• PopupMenu
• MenuItem
Menubar
#75
• enable()
• disable(): làm mờ
public ViDuMenuBar(){
super("Vi du menubar");
this.setSize(500, 300);
myMenuBar = new MenuBar();
myMenu = new Menu("File");
this.setMenuBar(myMenuBar);
myMenuBar.add(myMenu);
myMenu.add(new MenuItem("Open..."));
myMenu.add(new MenuItem("Save..."));
myMenu.addSeparator();
myMenu.add(new MenuItem(("Exit")));
myMenu.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
#80 @Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
public static void main(String []args){
ViDuMenuBar vd = new ViDuMenuBar();
vd.setVisible(true);
}
}
Ví dụ PopupMenu
#81
public class ViDuPopupMenu extends Frame
implements MouseListener{
PopupMenu myMenu;
#82 public ViDuPopupMenu(){
super("Vi du PopupMenu");
this.setSize(500, 300);
myMenu = new PopupMenu("Popup menu");
myMenu.add(new MenuItem("Copy..."));
myMenu.add(new MenuItem("Cut..."));
myMenu.addSeparator();
myMenu.add(new MenuItem(("Paste")));
this.add(myMenu);
this.addMouseListener(this);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
#83 if(e.getButton()==MouseEvent.BUTTON3)
myMenu.show(e.getComponent(),
e.getX(), e.getY());
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
public static void main(String []args){
ViDuPopupMenu vd = new ViDuPopupMenu();
vd.setVisible(true);
}
}
Applet
#84
• init(): Chỉ được gọi khi applet bắt đầu thực thi
• start(): Được gọi sau init() hoặc khi applet được trở lại
• destroy(): Được gọi khi applet kết thúc (tắt trình duyệt)
Sử dụng Applet
#89
• Để thực thi một applet, tạo một tập tin HTML có sử dụng
thẻ applet hoặc thẻ object
<APPLET CODE=“ViDuApplet.class"
WIDTH=200
HEIGHT=200>
</APPLET>
Ví dụ
#90
import java.applet.Applet;
import java.awt.*;
<meta charset="UTF-8">
<title>Ví dụ về Java Applet</title>
</head>
<body>
<applet code="MyFirstApplet.class"
width="300" height="200">
</applet>
</body>
</html>
Các phương thức của Graphics
#92