Danh mục

Lecture Java: Chapter 12

Số trang: 59      Loại file: pptx      Dung lượng: 1.93 MB      Lượt xem: 30      Lượt tải: 0    
tailieu_vip

Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Lecture Java: Chapter 12 (Java Graphic User Interface) focuses on How to display a Window, Layout Manager, Common Control, Event Listener, Event Listener, Event Listener.
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 12DONG NAI UNIVERSITY OF TECHNOLOGY 1 DONG NAI UNIVERSITY OF TECHNOLOGY ContactFull Name: Trần Duy ThanhBlog : http://duythanhcse.wordpress.comEmail: tranduythanh@dntu.edu.vnPhone: 0987773061 2 DONG NAI UNIVERSITY OF TECHNOLOGY1. How to display a Window? 2. Layout Manager 3. Common Control4. Event Listener5. Dialogbox6. Advanced Control 3 DONG NAI UNIVERSITY OF TECHNOLOGY1. How to display a Window?Ø Extends from Frame or JFrame? import java.awt.*; import java.awt.event.*; import javax.swing.*; 4 DONG NAI UNIVERSITY OF TECHNOLOGY1. How to display a Window?public class MyWindow extends JFrame{ public MyWindow(){ super(Demo Windows);setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[]args) { MyWindow ui=new MyWindow(); ui.setSize(400, 300); ui.setLocationRelativeTo(null); ui.setVisible(true); 5 DONG NAI UNIVERSITY OF TECHNOLOGY1. How to display a Window?super(Demo Windows); Use to set title for this windowsetDefaultCloseOperation(EXIT_ON_CLOSE); Allow click ‘x’ Top right cornerto close the windowui.setSize(400, 300); set Width =400 and Height =300ui.setLocationRelativeTo(null); Display window on desktop centerscreenui.setVisible(true); 6 DONG NAI UNIVERSITY OF TECHNOLOGY2. Layout Manager Ø FlowLayout Ø BoxLayout Ø BorderLayout - Setting up the Layout before add another control. Ø CardLayout - Usually, we use JPanel to Ø GridBagLayout add another control, JPanel Ø GridLayout is container. JPanel could add another JPanel. Ø GroupLayout Ø SpringLayout 7 DONG NAI UNIVERSITY OF TECHNOLOGYØ FlowLayout FlowLayout cho phép add các control trên cùng một dòng, khi nào hết chỗ chứa nó sẽ tự động xuống dòng, ta cũng có thể điều chỉnh hướng xuất hiện của control. Mặc định khi một JPanel được khởi tạo thì bản thân lớp chứa này sẽ có kiểu Layout là FlowLayout. Resize the Width 8 DONG NAI UNIVERSITY OF TECHNOLOGYØ FlowLayout Code E:HUIJavaStudyJPanel pnFlow=new JPanel(); hocuisrcMyFlowLayout. pnFlow.setLayout(new FlowLayout());pnFlow.setBackground(Color.PINK);JButton btn1=new JButton(FlowLayout);JButton btn2=new JButton(Add cáccontrol);JButton btn3=new JButton(Trên 1 dòng);JButton btn4=new JButton(Hết chỗchứa);JButton btn5=new JButton(Thì xuốngdòng);pnFlow.add(btn1);pnFlow.add(btn2);pnFlow.add(btn3);pnFlow.add(btn4); 9 DONG NAI UNIVERSITY OF TECHNOLOGYØ FlowLayoutpnFlow.setLayout(new FlowLayout()); Setup FlowLayout for pnFlowpnFlow.add(btn1); Add new button into the pnFlowContainer con=getContentPane(); get the Window containercon.add(pnFlow); add pnFlow panel into the windowcontainer 10 DONG NAI UNIVERSITY OF TECHNOLOGYØ BoxLayoutBoxLayout cho phép add các control theo dòng hoặccột, tại mỗi vị trí add nó chỉ chấp nhận 1 control, dođó muốn xuất hiện nhiều control tại một vị trí thì bạnnên add vị trí đó là 1 JPanel rồi sau đó add cáccontrol khác vào JPanel này.BoxLayout.X_AXIS cho phép add các control theohướng từ trái qua phải.BoxLayout.Y_AXIS cho phép add các control theohướng từ trên xuống dưới.BoxLayout sẽ không tự động xuống dòng khi hếtchỗ chứa, tức là các control sẽ bị che khuất nếu nhưthiếu không gian chứa nó. 11 DONG NAI UNIVERSITY OF TECHNOLOGYØ BoxLayout BoxLayout.X_AXIS BoxLayout.Y_AXIS No wrap row when resize dimension 12 DONG NAI UNIVERSITY OF TECHNOLOGYØ BoxLayout E:HUIJavaStudy Code hocuisrcMyBoxLayout.jJPanel pnBox=new JPanel();pnBox.setLayout(new BoxLayout(pnBox,BoxLayout.X_AXIS));JButton btn1=new JButton(BoxLayout);btn1.setForeground(Color.RED);Font font=new Font(Arial,Font.BOLD |Font.ITALIC, ...

Tài liệu được xem nhiều: