Bài giảng Lập trình Java 3 - Bài 2: Các thành phần GUI cơ bản
Phương thức chung của các component trong SWING
Một số Swing component (JTextfield, JLabel,JButton, JCheckBox, JRadioButton,JTextArea, JPasswordField)
Modal và Non – Modal Dialog
Custom Dialog

Trang 1

Trang 2

Trang 3

Trang 4

Trang 5

Trang 6

Trang 7

Trang 8

Trang 9

Trang 10
Tải về để xem bản đầy đủ
Bạn đang xem 10 trang mẫu của tài liệu "Bài giảng Lập trình Java 3 - Bài 2: Các thành phần GUI cơ bản", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
Tóm tắt nội dung tài liệu: Bài giảng Lập trình Java 3 - Bài 2: Các thành phần GUI cơ bản
Bài 2:
Các thành phần GUI cơ bản
SOF203 - Lập trình Java 3
Bài 2 : Các thành phần GUI cơ bản
Mục tiêu bài học
Phương thức chung của các component trong SWING
Một số Swing component (JTextfield, JLabel,JButton,
JCheckBox, JRadioButton,JTextArea, JPasswordField)
Modal và Non – Modal Dialog
Custom Dialog
JComponent
JComponent là lớp cơ sở abstract của tất cả các Swing
components (có tên bắt đầu chữ J - ngoại trừ top-
container)
Kế thừa từ Component và Container
Đóng gói hầu hết các đặc điểm và thao tác cơ bản của 1
thành phần GUI
Lớp JComponent
• Cảm quan khả kiến (Pluggable)
• Phím tắt (tính dễ nhớ)
• Khả năng xử lý sự kiện chung
JComponent
Bao gồm:
JButton, JList, JLabel, JTextField,JTextArea
JComboBox, JRadioButton,
JCheckBox extend functionality existing in AWT
Components.
JProgressBar, JSlider, JTable, JToolBar, JTree provide new
components.
JInternalFrame, JScrollPane,
JSplitPane, JTabbedPane provide new ways to combine
components.
JComponent
Property Data type get is set
Default value (if applicable)
Background Color
colorModel ColorModel
component' Component
componentCount int
Components Component[]
Cursor Cursor Cursor.DEFAULT CURSOR
Enabled boolean true
Font Font
Foreground Color
insets Insets Insets(0,0, 0, 0)
layout LayoutManager BorderLayout( )
locale Locale
location Point
locationOnScreen Point
name String ""
parent Container null
size Dimension
showing boolean true
valid boolean
visible boolean true
'indexed
JTextField
Để hiển thị dữ liệu, nhập dữ liệu
Khởi tạo
JTextField(): text field trống
JTextField(int): A text field with the specified width
JTextField(String): A text field with text
JTextField(String, int): A text field with the specified text and
width
JTextField(int cols): khởi tạo JtextField với số cột quy định.
JTextField(String text, int cols): khởi tạo JTextField với
dòng text và số cột quy định.
JTextField
Ví dụ:
JTextField textField = new JTextField(20);
Và sau đó setText():
textField.setText("Hello!");
JTextField
Methods:
-void setText(String t) set Text in code behind for
JTextField
-String getText()
• String s = txtHoten.getText();
• void setEditable(boolean b): chỉnh sửa nội dung
• setColumns()
• strim(): bỏ đi khoảng trắng đầu và cuối chuỗi
• setFont
• textField.setFont(new Font("Arial", Font.BOLD,12));
• requestFocus();
Events:
• –caretUpdate
AWT
Giới thiệu chung
Thư viện API cung cấp các đối tượng GUI
Tạo liên kết giao diện giữa ứng dụng Java và OS
Chiếm nhiều tài nguyên hệ thống(Heavy-weight component)
Package java.awt
Gồm nhiều phần tử (class) để tạo GUI.
Có các lớp quản lý việc bố trí các phần tử.
Có (event-oriented application) mô hình ứng dụng hướng sự kiện.
Có các công cụ xử lý đồ họa và hình ảnh.
Các lớp sử dụng các tác vụ với clipboard (vùng nhớ đệm) như cut,
paste.
(Abstract Windows Toolkit)
JLabel
JLabel thường được dùng để hiển thị text hoặc hình ảnh để
tạo các chỉ dẫn, hướng dẫn trên giao diện người dùng.
Khai báo:
Label() : An empty label
JLabel(String) : A label with the specified text
JLabel(String, int) : A label with the specified text and
alignment LEFT, CENTER, and RIGHT.
JLabel(String, Icon, int) : A label with the specified text, icon,
and Alignment
JLabel
Methods:
• void setFont (Font f)
• void setText(String S) quy định chuỗi văn bản.
• String getText()
• void setIcon(Icon) quy định Icon
• getLength(): đưa ra chiều dài của chuỗi text.
Events:
mouseClicked
JLabel
Example:
• JLabel lbl=new JLabel("Họ và tên:");
• JLabel lbl=new JLabel("Ngày sinh:");
Sử dụng HTML để tạo ra các JLabel nhiều dòng, nhiều
định dạng
JButton
là một đối tượng mà cho phép chúng ta khi click chuột
vào sẽ thực hiện một việc gì đó
Khai báo
• JButton() Creates a button with no set text or icon.
• JButton(Action a) Creates a button where properties
are taken from the Action supplied.
• JButton(Icon icon) Creates a button with an icon.
• JButton(String text) Creates a button with text.
• JButton(String text, Icon icon) Creates a button with
initial text and an icon.
JButton
Methods:
• setText (String text)
• getText ()
• setForeground (Color fg)
• setFocusCycleRoot (boolean b)
Events
• actionPerformed
• mousePressed
JButton
Các cách tạo và bắt sự kiện JButton
JButton bt=new JButton("Watch");
bt.setIcon(new ImageIcon("mywatch.png"));
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
//do something here
}
});
JButton
Ví dụ
JCheckBox
là đối tượng cho phép chúng ta chọn nhiều thuộc tính.
Ví dụ như khi điền thông tin một người xem có tiền, có nhà, có xe hơi
không chẳng hạn. Người đó có thể có cả 3 hoặc không có một cái nào cả
Khai báo
• JCheckBox() Creates an initially unselected check box button with no
text, no icon.
• JCheckBox(Action a) Creates a check box where properties are taken
from the Action supplied.
• JCheckBox(Icon icon) Creates an initially unselected check box with an
icon.
• JCheckBox(Icon icon, boolean selected) Creates a check box with an icon
and specifies whether or not it is initially selected.
• JCheckBox(String text) Creates an initially unselected check box with text.
• JCheckBox(String text, boolean selected)
• JCheckBox(String text, Icon icon)
• JCheckBox(String text, Icon icon, boolean selected)
JCheckBox
Methods:
• –setSelected (boolean b)
• –isSelected ()
• –setText (String text)
• –getText ()
Events:
–actionPerformed
JCheckBox
Ví dụ:
panelCheck=new JPanel();
panelCheck.setLayout(new GridLayout(2, 2));
JCheckBox chk1=new JCheckBox("C/C++");
JCheckBox chk2=new JCheckBox("C#");
JCheckBox chk3=new JCheckBox("PHP");
JCheckBox chFile đính kèm:
bai_giang_lap_trinh_java_3_bai_2_cac_thanh_phan_gui_co_ban.pdf

