You are on page 1of 51

LẬP TRÌNH JAVA CSDL

Bài 1
JAVA SWING

Nguyễn Hữu Thể

1
Nội dung

 Graphical User Interface


 JFrame
 JDialog
 JDesktopPane & JInternalFrame
 JPanel

2
Graphical User Interface - GUI
 Các ứng dụng hiện nay trình bày với giao diện đồ họa đẹp mắt.
 Các ngôn ngữ lập trình hiện nay đã cung cấp các đối tượng đồ
họa để thực hiện việc này.

3
Graphical User Interface - GUI
 Graphical User Interface (GUI)
 Mô hình giao tiếp kiểu tương tác giữa ứng dụng và user
dạng đồ họa.
 Mỗi ngôn ngữ hỗ trợ cách tạo GUI khác nhau:
• VB, VC++ dùng dạng drag and drop,
• C++ đòi hỏi programmer viết toàn bộ code để tạo GUI,
• Java hỗ trợ sẵn các lớp tạo GUI cho Programmer sử
dụng.

4
Graphical User Interface - GUI
 GUI = Container + Components
 There are two types of GUI elements:
 Component: Components are elementary GUI entities (such
as Button, Label, and TextField.)
 Container: Containers (such as Frame, Panel and Applet)
are used to hold components in a specific layout. A
container can also hold sub-containers.
Graphical User Interface - GUI

 Một phần tử (element) GUI được thiết lập bằng cách sử dụng
thủ tục sau:
 Tạo đối tượng
 Xác định sự xuất hiện ban đầu của đối tượng
 Chỉ ra nó nằm ở đâu
 Thêm phần tử vào giao diện trên màn hình
 Một thành phần (component) GUI là một đối tượng trực quan.
Người dùng tương tác với đối tượng này thông qua con trỏ
chuột hay bàn phím.

6
Graphical User Interface - GUI

Ví dụ:

Panel panel = new Panel(); // Panel is a Container

Button btn = new Button(); // Button is a Component

panel.add(btn); // The Panel Container adds a Button Component

7
Graphical User Interface - GUI

 Java cung cấp 2 gói GUI


 AWT: Abstract Windowing Toolkit
 Swing

8
AWT
 Top-Level Containters: Frame, Dialog and Applet
 Each GUI program has a top-level container. The commonly-
used top-level containers in AWT are Frame, Dialog and
Applet:
 A Frame provides the "main window" for the GUI
application, which has a title bar (containing an icon, a title,
the minimize, maximize/restore-down and close buttons),
an optional menu bar, and the content display area.
 To write a GUI program, we typically start with a subclass
extending from java.awt.Frame to inherit the main window
as follows:

9
Cấu trúc gói AWT
Cấu trúc gói Swing
Cấu trúc gói Swing
Swing
 Swing là gói thư viện dựa trên mô hình MVC do Java cung
cấp, dùng để thiết kế giao diện người dùng.
 Phần lớn, các thành phần (component) của Swing được dẫn
xuất từ lớp cha là Jcomponent được thừa kế (extends) từ lớp
Container của AWT.
 Swing hỗ trợ công nghệ gọi là “Pluggable – Look – And –
Feel ” (PLAF), nơi mà các thành phần có thể trình bày trên
mọi hệ điều hành.
 Ví dụ: trên hệ diều hành linux, một nút (button) có thể nhìn
giống như trên hệ điều hành Window.
Swing
 Swing components support pluggable look-and-feel. You can
choose between Java look-and-feel and the look-and-feel of
the underlying OS (e.g., Windows, UNIX or Mac).
 If the later is chosen, a Swing button runs on the Windows
looks like a Windows' button and feels like a Window's button.
Similarly, a Swing button runs on the UNIX looks like a
UNIX's button and feels like a UNIX's button.
Swing
Yêu cầu của GUI
 Thân thiện với user.
 Số phần tử (element, component) trên GUI thay đổi tùy thuộc
vào ứng dụng.
 Khi user tương tác với phần tử của GUI, ứng dụng phải có
phản ứng.
Đưa component vào GUI

Các bước để đưa 1 component vào GUI (viết code)


 Tạo 1 đối tượng component phù hợp.
 Xác định hình thức bên ngoài lúc đầu của component.
 Định vị component này trên GUI.
 Thêm component này vào GUI.
Ví dụ
Container

 Container: Đối tượng chứa các element, cho phép vẽ, tô màu
lên container.
 Frame và Panel là các class thường dùng.
 Panel thường dùng để chứa các element trong 1 GUI phức tạp,
1 Frame có thể chứa nhiều Panel.
 Panel, Applet thường dùng để tạo 1 ứng dụng nhúng vào
Browser.
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
 Tất cả các đối tượng liên quan tới JFrame được quản lý bởi
đứa con duy nhất của nó, một thể hiện (instance) của
JRootPane.

20
JFrame - JRootPane

 JRootPane có 4 phần chính

21
JFrame - ContentPane

 Khi thêm các thành phần (component ) vào JFrame chúng ta


không thêm trực tiếp
 Các thành phần sẽ thêm vào ContentPane bằng cách gọi
phương thức:
 getContentPane().add(component);

22
JFrame - ContentPane

23
JFrame - GlassPane
 Theo mặc định thì GlassPane bị ẩn
 GlassPane có thể là mọi thành phần trong Swing nhưng thành
phần mặc định là Jpanel.
 Để thay đổi thành phần mặc định này sang thành phần khác
chúng ta sử dụng phương thức
 setGlassPane(component);
 Nếu muốn nhìn thấy GlassPane như thế nào thì sử dụng
phương thức sau:
 getGlassPane().setvisible(true);

24
JFrame – Cấu trúc class

package jframe; //Tên package


public class MyFrame extends javax.swing.JFrame {

public MyFrame() {
initComponents();
}

private void initComponents() {


//Các thành phần khởi tạo
}

public static void main(String args[]) {


//Gọi thực hiện JFrame
}
}
25
JFrame - Cấu trúc class trên Netbean
package jframe;
public class MyFrame extends javax.swing.JFrame {
public MyFrame() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(
javax.swing.WindowConstants.EXIT_ON_CLOSE);
//...
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
} 26
}
JFrame - Một số phương thức

Constructor

JFrame()
Constructs a new frame that is initially invisible.

JFrame(GraphicsConfiguration gc)
Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.

JFrame(String title)
Creates a new, initially invisible Frame with the specified title.

JFrame(String title, GraphicsConfiguration gc)


Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen
device

27
JFrame - Một số phương thức
public void setDefaultCloseOperation(int operation);

Phương thức mặc định khi đóng JFrame.


- 4 kiểu:
• WindowContants.DO_NOTHING_ON_CLOSE
• WindowContants.HIDE_ON_CLOSE
• WindowContants.DISPOSE_ON_CLOSE
• WindowContants.EXIT_ON_CLOSE

28
JFrame - Một số phương thức

 DO_NOTHING_ON_CLOSE (defined in WindowConstants):


Don't do anything; require the program to handle the operation
in the windowClosing method of a registered WindowListener
object.
 HIDE_ON_CLOSE (defined in WindowConstants):
Automatically hide the frame after invoking any registered
WindowListener objects.
 DISPOSE_ON_CLOSE (defined in WindowConstants):
Automatically hide and dispose the frame after invoking any
registered WindowListener objects.
 EXIT_ON_CLOSE (defined in JFrame): Exit the application
using the System exit method. Use this only in applications.

29
JFrame - Một số phương thức
public void setExtendedState(int state);

Gán trạng thái của JFrame


 5 kiểu
– JFrame.NORMAL
– JFrame.ICONIFIED
– JFrame.MAXIMIZED_HORIZ
– JFrame.MAXIMIZED_VERT
– JFrame.MAXIMIZED_BOTH

30
JFrame - Một số phương thức
 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 31
JFrame - Một số phương thức
 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 32
JFrame - Một số phương thức
 public void dispose();
• Hủy
 public Containter getCotentPane()
• getContentPane.add

33
Ví dụ 1

34
Ví dụ 2
import javax.swing.*;
public class MyFrame2 extends JFrame {
private JButton bt;
public MyFrame2() {
super();
this.initComponents();
}
private void initComponents() {
this.setTitle("Ví dụ 2"); //JFrame
this.setSize(200, 100);
this.setExtendedState(JFrame.NORMAL);
this.bt=new JButton(); //JButon
this.bt.setText("Button Hello");
this.add(this.bt);
}
public static void main(String[] args) {
MyFrame2 frm = new MyFrame2();
frm.setVisible(true);
} 35
}
Ví dụ 3 – Tool Netbean
package jframe;
public class MyFrame2 extends javax.swing.JFrame {
public MyFrame2() {
initComponents();
}
private void initComponents() { // … }
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame2().setVisible(true);
}
});
}
private javax.swing.JButton btHello;
} 36
private void initComponents() {
btHello = new javax.swing.JButton();
setDefaultCloseOperation(
javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Vi du 2");
setFont(new java.awt.Font("Tahoma", 0, 10));
btHello.setText("Button Hello");
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap(67, Short.MAX_VALUE)
.addComponent(btHello).addGap(64, 64, 64))
);
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32).addComponent(btHello)
.addContainerGap(39, Short.MAX_VALUE))
);
pack(); 37
}
JDialog
 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

38
JDialog – Cấu trúc class

public class MyDialogForm extends javax.swing.JDialog {

public MyDialogForm(java.awt.Frame parent, boolean modal) {


super(parent, modal);
initComponents();
}

private void initComponents() {


//…
}

public static void main(String args[]) {


//…
}
}
39
JDialog with Netbean
public class MyDialogForm extends javax.swing.JDialog {

JDialog
public MyDialogForm(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
//…
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
MyDialogForm dialog = new MyDialogForm(
new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
} 40
}
Hiển thị JDialog
//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ó)
}

41
JDialog - Một số phương thức
public void setDefaultCloseOperation(int operation);

Gán phương thức mặc định khi người dùng đóng Dialog
 Có 3 lựa chọn sau:
– WindowContants.DO_NOTHING_ON_CLOSE
– WindowContants.HIDE_ON_CLOSE
– WindowContants.DISPOSE_ON_CLOSE

42
JDialog - Một số phương thức
 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

43
JDialog - Một số phương thức
 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
44
JDialog - Một số phương thức
 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

45
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 chứa các JInternalFrame bên trong

46
JDesktopPane - Một số phương thức
 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 tro
 JDesktopPane()
 Constructor

47
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

48
JInternalFrame – Một số phương thức

 Các phương thức của JInternalFrame tương tự như JFrame

49
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.

50
JPanel
 Constructors
 Panel(): tạo 1 panel với bố cục mặc định.
 Panel(LayoutManager layout): tạo 1 panel với bố cục đã
biết.
 Methods
 add (component) // thêm 1 component vào panel
 setLayout(LayoutManager layout) //chọn kiểu bố trí
components

51

You might also like