Đăng ký Đăng nhập
Trang chủ Công nghệ thông tin Kỹ thuật lập trình Lập trình swing căn bản java...

Tài liệu Lập trình swing căn bản java

.PDF
117
1009
58

Mô tả:

lập trình swing_căn bản _java
ThS. Trần Thị Thanh Nga Khoa CNTT, Trường ĐH Nông L}m TPHCM Email: [email protected] Nội dung  Giao diện người dùng với Java (Java GUI)  Các container JFrame, JPanel  Quản lý Layout  C|c component thường dùng 2 Java GUI  Cung cấp c|c công cụ cho phép tạo giao tiếp trực quan v{ hấp dẫn với người dùng, được biết đến l{ Swing.  Giao diện người dùng bao gồm 1 cửa sổ chính, v{ c|c control được đặt lên trên.  C|c th{nh phần tạo nên giao diện n{y nằm trong gói javax.swing.  Tên của lớp n{y bắt đầu bằng chữ J. 3 Swing Components Example frame panel button label • SwingApplication example creates four commonly used Swing components: – a frame, or main window (JFrame) – a panel, sometimes called a pane (JPanel) – a button (JButton) – a label (JLabel) 4 JFrame  Đ}y l{ cửa sổ chính, dùng để chứa c|c th{nh phần giao diện kh|c.  Đóng vai trò l{ một container  JFrame thường dùng để tạo cửa sổ trong chương trình Swing  Contructor:  JFrame()  JFrame(String title)  C|c component được đưa v{o content pane, không đưa trực tiếp v{o JFrame.  frame.getContentPane().add(b);  frame.add(comp); //shortly 5 Example import javax.swing.JFrame; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame(“Demo Frame");//Create a frame frame.setSize(400, 300);//Set the frame size frame.setLocationRelativeTo(null);//center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 6 JPanel  L{ container trung gian dùng để chứa c|c component khác.  Thường dùng để ph}n chia c|c component trong ứng dụng.  Layout mặc định l{ FlowLayout.  Contructor:  JPanel();  JPanel(LayoutManager lm); 7 Bố trí các thành phần bên trong đối tượng chứa  Sử dụng Layout Managers  FlowLayout  BorderLayout  GridLayout  L{m việc không có Layout 8 FlowLayout  L{ một class cung cấp c|ch quản lý việc sắp đặt đơn giản c|c component.  C|c component được sắp xếp trong container từ tr|i sang phải theo thứ tự m{ chúng được thêm v{o.  C|c component có thể được sắp xếp bằng c|ch sử dụng 3 hằng số sau:  FlowLayout.RIGHT  FlowLayout.CENTER  FlowLayout.LEFT  Có thể x|c định khoảng c|ch giữa c|c component (pixel). 9 FlowLayout 10 FlowLayout example public class ShowFlowLayout extends JFrame { public ShowFlowLayout(){ setLayout(new FlowLayout(FlowLayout.LEFT, 12, 25)); add(new JLabel("FirstName")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("LastName")); add(new JTextField(8)); } public static void main(String[] args) { ShowFlowLayout frame = new ShowFlowLayout(); frame.setTitle("ShowFlowLayout"); frame.setSize(200, 200); frame.setLocationRelativeTo(null);//Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 11 FlowLayout Example 12 BorderLayout  L{ trình quản lý layout mặc định của content pane của mỗi Jframe  Một BoderLayout có 5 vùng để chứa c|c component: NORTH, SOUTH, EAST, WEST, CENTER.  C|c component được thêm v{o BorderLayout dùng:  add(Component, index), trong đó index l{ 1 trong 5 hằng số của 5 vùng của BorderLayout. 13 BorderLayout 14 BorderLayout example public class ShowBorderLayout extends JFrame { public ShowBorderLayout(String title) { super(title); setLayout(new BorderLayout(5, 10)); add(new JButton("East"), BorderLayout.EAST); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("West"), BorderLayout.WEST); add(new JButton("North"), BorderLayout.NORTH); add(new JButton("Center"), BorderLayout.CENTER); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 15 BorderLayout 16 GridLayout  Sắp xếp c|c component v{o một lưới gồm c|c h{ng v{ cột.  C|c component được bổ sung bắt đầu tại ô bên tr|i, tiến sang phải cho tới khi một h{ng đầy.  C|c ô trong lưới có cùng kích thước. 17 GridLayout example public class ShowGridLayout extends JFrame { public ShowGridLayout(String title) { super(title); setLayout(new GridLayout(3, 2, 5, 5)); // Add labels and text fields to the frame add(new JLabel("First Name")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("First Name")); add(new JTextField(8)); setSize(300, 180); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 18 GridLayout 19 Làm việc không có Layout Manager (Absolute Positioning)  Có thể đặt vị trí cho c|c component m{ không cần dùng Layout Manager. Giải ph|p n{y được dùng để x|c định ho{n to{n kích thước v{ vị trí của component.  Mặc dù có thể l{m việc m{ không cần LayoutManager, bạn nên dùng LayoutManager nếu có thể. LayoutManager dễ thay đổi kích thước của container v{ điều chỉnh hình dạng của c|c component. 20
- Xem thêm -

Tài liệu liên quan