09. java programming - swing gui form
Post on 28-Apr-2015
100 Views
Preview:
TRANSCRIPT
CHUYÊN ĐỀ JAVA
Nguyễn Hoàng Anh – nhanh@fit.hcmus.edu.vn
SWING GUI FORMS
Nội dung
JFrame
JDialog
JDesktopPane & JInternalFrame
JPanel
JFrame – Hình ảnh
3
JFrame
JFrame được sử dụng làm giao diện chính trong ứng
dụng Swing (top-level windows)
Hầu hết các ứng dụng Swing được xây dựng từ
JFrame
4
JFrame – Mở rộng JFrame
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package packageName;
import javax.swing.*;
public class FrameName extends JFrame {
public FrameName() {
this.initComponents();
}
/**
* Khởi tạo các thành phần
*/
private void initComponents() {
...
pack();
}
}
JFrame – Hiển thị
6
1
2
3
4
5
6
7
8
9
package packageName;
public class Main {
public static void main(String[] args) {
FrameName frm = new FrameName();
frm.setVisible(true);
}
}
JFrame – Một số phương thức thông dụng
7
Gán phương thức mặc định khi người dùng đóng Frame
Có 4 lựa chọn sau:
– WindowContants.DO_NOTHING_ON_CLOSE
– WindowContants.HIDE_ON_CLOSE
– WindowContants.DISPOSE_ON_CLOSE
– WindowContants.EXIT_ON_CLOSE
public void setDefaultCloseOperation(int operation);
Jframe – Một số phương thức thông dụng
8
Gán trạng thái của JFrame
Có 5 lựa chọn sau
– JFrame.NORMAL
– JFrame.ICONIFIED
– JFrame.MAXIMIZED_HORIZ
– JFrame.MAXIMIZED_VERT
– JFrame.MAXIMIZED_BOTH
public void setExtendedState(int state);
JFrame – Một số phương thức thông dụng
9
public void setResizable(boolean resizable);
– true: Cho phép thay đổi kích thước
– false: không cho phép
public void setTitle(String title);
– Gán tựa đề cho JFrame
public void setBackground(Color c);
– Gán màu nền cho JFrame
public void setForeground(Color c);
– Gán màu chữ cho JFrame
JFrame – Một số phương thức thông dụng
10
public void setIconImage(Image image);
• Gán hình ảnh Icon cho JFrame
public void setSize(Dimension d);
public void setSize(int width, int height);
• Gán kích thước cho JFrame
public void setLocation(Point p);
public void setLocation(int x, int y);
• Gán vị trí cho JFrame
public void setVisible(boolean visible);
• true : hiện JFrame
• false : ẩn Jframe
Tương tự cho các phương thức get/is
JFrame – Một số phương thức thông dụng
public void dispose();
• Hủy
public Containter getCotentPane()
– getContentPane.add
11
Jframe – Ví dụ 1
12
JFrame – Ví dụ 1
13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package swingcontainersample;
import javax.swing.*;
public class ViDu1Frame extends JFrame{
public ViDu1Frame() {
super();
this.initComponents();
}
private void initComponents() {
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon icon=new ImageIcon(
this.getClass().getResource("images/java-icon.jpg")
);
this.setIconImage(icon.getImage());
this.setTitle("Ví dụ 1");
this.setLocation(0, 0);
this.setSize(300, 400);
this.setExtendedState(JFrame.NORMAL);
}
}
JFrame – Ví dụ 1
14
1
2
3
4
5
6
7
8
9
package swingcontainersample;
public class Main {
public static void main(String[] args) {
ViDu1Frame frm = new ViDu1Frame();
frm.setVisible(true);
}
}
JFrame – Ví dụ 2
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package swingcontainersample;
import javax.swing.*;
public class ViDu2Frame extends JFrame {
private JButton jbt1;
public ViDu2Frame() {
super();
this.initComponents();
}
private void initComponents() {
//JFrame
this.setTitle("Ví dụ 2");
this.setSize(200, 100);
this.setExtendedState(JFrame.NORMAL);
//Buton
this.jbt1=new JButton();
this.jbt1.setText("Button 1");
this.add(this.jbt1);
}
}
JDialog – Hình Ảnh
16
JDialog
JFrame hay JDialog thường sử dụng JDialog nhập
liệu hoặc xuất liệu
JDialog có 2 trạng thái
– Modal: Khi Jdialog thực hiện xong mới được phép
thao tác lên form cha .
– Modeless: Sau khi hiển thị dialog, người dùng có thể
thao tác lên form cha
JDialog thường được sử dụng với trạng thái Modal
17
JDialog – Mở rộng JDialog
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package packageName;
import javax.swing.*;
public class NameDialog extends JDialog {
Dữ liệu + Các phương thức get/set (nếu có)
public NameDialog(JFrame parent, boolean modal){
super(parent, modal);
this.intComponents();
}
public NameDialog(JDialog parent, boolean modal){
super(parent, modal);
this.intComponents();
}
private void intComponents(){
}
}
JDialog – Hiển thị JDialog
JFrame hay JDialog thường sử dụng JDialog nhập
liệu hoặc xuất liệu
Jdialog thường được thiết lập ở trạng thái Modal,
nghĩa là khi Jdialog thực hiện xong mới được phép
thao tác lên form cha .
19
JDialog – Hiển thị JDialog
20
1
2
3
4
5
6
7
8
//JFrame hoặc JDialog khác gọi
private void jbuttonActionPerformed(ActionEvent evt){
DialogName dlg=new DialogName (this, true);
Gán dữ liệu cho dlg thông qua set (nếu có)
dlg.setVisible(true);
Lấy dữ liệu cho dlg thông qua get (nếu có)
}
JDialog – Một số phương thức thông dụng
21
Gán phương thức mặc định khi người dùng đóng Frame
Có 3 lựa chọn sau:
– WindowContants.DO_NOTHING_ON_CLOSE
– WindowContants.HIDE_ON_CLOSE
– WindowContants.DISPOSE_ON_CLOSE
public void setDefaultCloseOperation(int operation);
JDialog – Một số phương thức thông dụng
22
public void setResizable(boolean resizable);
– true: Cho phép thay đổi kích thước
– false: không cho phép
public void setTitle(String title);
– Gán tựa đề cho JDialog
public void setBackground(Color c);
– Gán màu nền cho JFrame
public void setForeground(Color c);
– Gán màu chữ cho JFrame
JDialog – Một số phương thức thông dụng
23
public void setIconImage(Image image);
• Gán hình ảnh Icon cho JFrame
public void setSize(Dimension d);
public void setSize(int width, int height);
• Gán kích thước cho JFrame
public void setLocation(Point p);
public void setLocation(int x, int y);
• Gán vị trí cho JFrame
public void setVisible(boolean visible);
• true : hiện JFrame
• false : ẩn Jframe
JDialog – Các phương thức thông dụng
public void setModal(boolean modal);
• true: Modal
• false : Modeless
public void dispose();
• Hủy
public Containter getCotentPane()
– getContentPane.add
Tương tự cho các phương thức get/is
24
JDesktopPane – Hình ảnh
25
JDesktopPane
MDI
JDesktopPane
JDesktopPane được sử dụng để xây dựng ứng dụng
MDI
JDesktopPane thường là một thành phần bên trong
JFrame
JDesktopPane thường được sử dụng để chứa các
JInternalFrame bên trong
26
JDesktopPane – Một số phương thức thông dụng
public void add(Component component);
– component : thường là JInternalFrame
public JInternalFrame getSelectedFrame();
– Lấy JInternalFrame đang được chọn
public JInternalFrame [] getAllFrames();
– Lấy tất cả các JInternalFrame bên trong
27
JInternalFrame – Hình ảnh
28
JDesktopPane MDI
JInternalFrame
JInternalFrame được sử dụng để đưa vào bên trong
JDesktopPane của JFrame để xây dựng ứng dụng
MDI.
Các bước xây dựng ứng dụng MDI:
– Bước 1: Xây dựng một hay nhiều JInternalFrame
– Bước 2:Tạo một JFrame có một JDesktopPane bên
trong
– Bước 3: Gắn JInternalFrame vào bên trong
JDesktopPane
29
JInternalFrame – Các phương thức thông dụng
Tương tự như JFrame
30
JPanel – Hình ảnh
31
JPanel
JPanel được sử dụng gom nhóm các control bên
trong, có thể được sử dụng như một user control
JPanel được sử dụng như một thành phần bên trong
JFrame, JDialog, JInternalFrame, hoặc trong một
Jpanel khác.
32
Tài liệu tham khảo
http://download.oracle.com/javase/tutorial/uiswing/co
mponents/index.html
33
HỎI VÀ ĐÁP
34
top related