Distributed Computing in Java - Lab Tutorial 2

7 461 0
Distributed Computing in Java - Lab Tutorial 2

Đang tải... (xem toàn văn)

Thông tin tài liệu

Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 1 - ngohaianh@ioit.ac.vn LAB 2 1. Các loại Container thường dùng trong lập trình Swing: - JPanel: ñơn giản, dùng ñể nhóm các ñối tượng con lại, thường là theo thứ tự trái-phải-trên-dưới. - JScrollPane: tương tự JPanel nhưng có thêm hai thanh trượt (scroll) giúp người lập trình tổ chức và người dùng xem các ñối tượng lớn, ví dụ cửa sổ chương trình chỉ là 800x600 nhưng cần xem một bức ảnh kích thước 1024x768. - JFrame: là thành phần thường dùng nhất ñể viết các chương trình GUI, tạo ra một cửa sổ (window) chứa các thành phần (component) của chương trình. - JOptionPane: cũng tạo ra các cửa sổ nhưng “nhỏ” hơn JFrame, các cửa sổ này ở dạng dialog hay pop-up window, thường dùng ñể ñưa ra các thông báo hoặc nhận số liệu nhập vào. - Phối hợp các loại Container trên? Loại nào thường dùng? import javax.swing.*; import java.awt.*; public class JScrollPaneDemo { public static void main(String args[]) { JFrame f=new JFrame("JScrollPane Demo"); f.setSize(200,150); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIcon image=new ImageIcon("news.jpg"); JLabel label=new JLabel(image); JScrollPane jpanel=new JScrollPane(label); f.add(jpanel); f.setVisible(true); } } 2. Layout Managers: khi ñưa một component vào một container thì vấn ñề ñặt ra là phải sắp xếp vị trí cho chúng  các ñối tượng Layout sẽ làm nhiệm vụ này, có nhiều loại Layout như sau: - FlowLayout: sắp xếp các ñối tượng theo thứ tự trái-phải-trên-dưới, các ñối tượng ñều giữ nguyên kích thước. Nếu không thiết lập layout cho chương trình thì kiểu FlowLayout sẽ là kiểu mặc ñịnh: các component sẽ ñược ñặt theo thứ tự từ trái qua phải, khi chiều dài chương trình không ñủ chứa thì sẽ xuống hàng, các ñối tượng ñược canh lề giữa – center align. - BorderLayout: các ñối tượng ñược ñặt theo các ñường viền của khung chứa, theo các cạnh West, East, South, North, Center. - GridLayout: tạo một khung lưới trong suốt với các ô bằng nhau, các ñối tượng sẽ ñược ñặt vừa với kích thước từng ô theo thứ tự trên-dưới. - CardLayout: các ñối tượng ñược ñặt vừa vặn với khung chứa và ñược xếp chồng lên nhau như các lá bài trong bộ bài  chỉ ñối tượng trên cùng ñược hiển thị. - GridBagLayout: tương tự như GridLayout nhưng kích thước các ñối tượng không nhất thiết phải vừa với một ô mà có thể từ hai ñến ba ô hay nhiều hơn nữa tùy theo các ràng buộc mà ta chỉ ñịnh bằng GridBagConstraint. - Dưới ñây chúng ta thực hành các kiểu Layout thường dùng nhất import javax.swing.*; import java.awt.*; public class LayoutDemo extends JFrame { public LayoutDemo() { setTitle("Layout Demo"); setSize(400,500); setDefaultCloseOperation(EXIT_ON_CLOSE); // Demo FlowLayout JPanel panel=new JPanel(); panel.setLayout(new FlowLayout(FlowLayout.RIGHT)); for (int i=1; i<=10; i++) panel.add(new JButton("Button "+i)); getContentPane().add(panel); Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 2 - ngohaianh@ioit.ac.vn /* // Demo BorderLayout JPanel panel=new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JButton("Button Center"), BorderLayout.CENTER); panel.add(new JButton("Button North"), BorderLayout.NORTH); panel.add(new JButton("Button South"), BorderLayout.SOUTH); panel.add(new JButton("Button East"), BorderLayout.EAST); panel.add(new JButton("Button West"), BorderLayout.WEST); getContentPane().add(panel); */ /* // Demo GridLayout JPanel panel=new JPanel(); panel.setLayout (new GridLayout(5, 3)); for (int i=1; i<=15; i++) panel.add(new JButton("Button "+i)); getContentPane().add(panel); */ /* // Demo CardLayout JButton first=new JButton("First"); JButton second=new JButton("Second"); JButton third=new JButton("Third"); JPanel buttonP=new JPanel(); buttonP.add(first); buttonP.add(second); buttonP.add(third); JPanel firstP=new JPanel(); firstP.setBackground(Color.BLUE); JPanel secondP=new JPanel(); secondP.setBackground(Color.YELLOW); JPanel thirdP=new JPanel(); thirdP.setBackground(Color.GREEN); JPanel cardP=new JPanel(new CardLayout()); cardP.add(firstP, "First"); cardP.add(secondP, "Second"); cardP.add(thirdP, "Third"); this.setLayout(new BorderLayout()); this.add(buttonP, BorderLayout.SOUTH); this.add(cardP, BorderLayout.CENTER); */ } public static void main(String args[]) { LayoutDemo obj=new LayoutDemo(); obj.setVisible(true); } } 3. Thực hành về JOptionPane: như ñã nói ở bài 1, JOptionPane giúp chúng ta tạo ra các cửa sổ cảnh báo, kèm theo nó là những lựa chọn cho người dùng, ví dụ khi người dùng chọn xóa một tệp thì ñưa ra thông báo: “Are you sure?” và kèm theo là 2 lựa chọn “Yes”, “No” ñể người dùng chọn lựa. Các thông báo này gọi là dialog, có 4 phương thức cho các JOptionPane dialog là: Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 3 - ngohaianh@ioit.ac.vn - showMessageDialog() - showInputDialog() - showOptionDialog() - showConfimDialog() Ba phương thức ñầu tiên giúp hiển thị một form thông tin ñến người dùng, tuy nhiên mỗi phương thức lại cho phép người dùng phản hồi lại khác nhau: showMessageDialog() chỉ ñưa ra một thông báo kèm theo nút OK, showConfỉmDialog() cho phép kết hợp ña dạng các nút OK, YES, NO, CANCEL. Còn showOptionDialog() cho phép hiển thị các nút có chứa chữ. Chỉ duy nhất phương thức showInputDialog() cho phép người dùng nhập dữ liệu vào một textfield kèm theo hai nút OK, CANCEL. Các kiểu thông báo và các tùy chọn: - Message Types: o JOptionPane.ERROR_MESSAGE red letter x, or a "no entry" sign o JOptionPane.INFORMATION_MESSAGE letter i in a circle o JOptionPane.WARNING_MESSAGE excalamation point o JOptionPane.QUESTION_MESSAGE question mark o JOptionPane.PLAIN_MESSAGE no standard icon - Option Types: o JOptionPane.DEFAULT_OPTION OK button o JOptionPane.YES_NO_OPTION YES and NO buttons o JOptionPane.YES_NO_CANCEL_OPTION YES, NO, and CANCEL buttons o JOptionPane.OK_CANCEL_OPTION OK and CANCEL buttons Lưu ý rằng việc sử dụng JOptionPane thường ñi kèm với JButton  cần ActionListener. Bài dưới ñây minh họa các kiểu dialog này: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JOptionPaneDemo { public static void main(String args[]) { JFrame f=new JFrame("JOptionPane demo"); f.setSize(new Dimension(300, 100)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); JPanel panel=new JPanel(); f.getContentPane().add(panel); // Demo showMessageDialog() JButton start=new JButton("Start"); start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, "Good morning", "Start", JOptionPane.PLAIN_MESSAGE); } }); // Demo showConfirmDialog() JButton exit=new JButton("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int res=JOptionPane.showConfirmDialog(null, "Are you sure", "Exit program", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (res==0) System.exit(0); } }); // Demo showInputDialog() JButton input=new JButton("Input"); input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Object[] selValues={"Gia tri 1", "Gia tri 2", "Gia tri 3"}; ImageIcon icon=new ImageIcon("new.gif"); String res=JOptionPane.showInputDialog(null, "Enter value: ", "Demo showInputDialog()", JOptionPane.PLAIN_MESSAGE); Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 4 - ngohaianh@ioit.ac.vn //Object res=JOptionPane.showInputDialog(null, "Enter value: ", // "Demo showInputDialog()", // JOptionPane.PLAIN_MESSAGE, icon, selValues, selValues[0]); System.out.println("showInputDialog: "+res); } }); // Demo showOptionDialog() JButton option=new JButton("Options"); option.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int messageType=JOptionPane.PLAIN_MESSAGE; ImageIcon icon=new ImageIcon("new.gif"); Object[] selValues={"Dong y", "Khong dong y", "Chua biet"}; // Shows message, choices appear as buttons int res = JOptionPane.showOptionDialog(null, "Choose one:", "Demo showOptionDialog()", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, icon, selValues, selValues[0]); System.out.println("showOptionDialog: "+res); if (res>=0) System.out.println(res+": "+selValues[res]); if (res==JOptionPane.CLOSED_OPTION) System.out.println("CLOSED_OPTION"); } }); panel.add(start); panel.add(exit); panel.add(input); panel.add(option); } } 4. Các ñối tượng JMenuBar, JMenu dùng ñể tạo ra các trình thực ñơn (menu). Chương trình dưới ñây tạo ra: - Một menu ñơn giản có tên “Java”  dùng ñối tượng JMenuBar - Trong Menu “Java” có các menu con các môn học liên quan ñến Java như GAJ, DCJ, DBSJ  dùng ñối tượng JMenu - Trong các menu con có chứa các mục chọn, có nhiều loại mục chọn khác nhau như kiểu checkbox, radio,…  các ñối tượng kiểu “menu item” là JCheckBoxMenuItem, JRadioButtonMenuItem,… - Nhận xét gì về JMenuBar, JMenu, JMenuItem? ðể thực hiện một thao tác gì ñó khi người dùng chọn các thành phần menu item, chúng ta dùng cách lắng nghe sự kiện ActionListener hoặc ItemListener với các phương thức tương ứng là actionPerformed(ActionEvent) và itemStateChanged(ItemEvent). Thực hành bài dưới ñây và quan sát kết quả ở cả cửa sổ chương trình và dòng lệnh. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JMenuDemo extends JFrame { public JMenuDemo() { JMenuBar menu=new JMenuBar(); JMenu java=new JMenu("Java"); JMenu java1=new JMenu("A Guide to Advanced Java"); JMenuItem thread=java1.add(new JCheckBoxMenuItem("Thread")); thread.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if (ie.getStateChange()==ie.SELECTED) System.out.println("You are learning Thread in Java"); else System.out.println("You are NOT learning Thread in Java"); } }); JMenuItem io=java1.add(new JCheckBoxMenuItem("Java I/O")); io.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.out.println("You are learning Java I/O"); Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 5 - ngohaianh@ioit.ac.vn } }); java.add(java1); JMenu java2=new JMenu("Distributed Computing with Java"); java2.add(new JRadioButtonMenuItem("Swing")); java2.add(new JRadioButtonMenuItem("RMI")); java2.add(new JRadioButtonMenuItem("Java Mail")); java.add(java2); JMenu java3=new JMenu("Database and Security in Java"); JMenuItem database=java3.add(new JRadioButtonMenuItem("Database")); database.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.out.println("You are learning Database in Java"); } }); JMenuItem security=java3.add(new JRadioButtonMenuItem("Security")); security.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println("You are learning Security in Java"); } }); ButtonGroup g=new ButtonGroup(); g.add(database); g.add(security); java.add(java3); JMenuItem about=new JMenuItem("About"); about.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, "Cac mon Java\nACCP 2007", "Java courses", JOptionPane.INFORMATION_MESSAGE); } }); java.add(about); JMenuItem exit=new JMenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }); java.add(exit); menu.add(java); setJMenuBar(menu); setTitle("Menu Demo"); setSize(new Dimension(300,400)); setVisible(true); } public static void main(String args[]) { JMenuDemo obj=new JMenuDemo(); } } 5. Bài dưới ñây minh họa cách thay ñổi các kiểu giao diện của một chương trình Swing, có 3 kiểu: javax.swing.plaf.metal.MetalLookAndFeel com.sun.java.swing.plaf.windows.WindowsLookAndFeel com.sun.java.swing.plaf.motif.MotifLookAndFeel Chú ý cách xử lý các sự kiện khi ấn nút: các phương thức actionPerformed(), getActionCommand() Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 6 - ngohaianh@ioit.ac.vn import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LookFeel extends JFrame implements ActionListener { JButton btnMetal, btnMotif, btnWindows; public LookFeel() { super("Look and Feel Example"); getContentPane().setLayout(new BorderLayout()); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JPanel pnlLookFeel = new JPanel(); btnMetal = new JButton("Metal"); btnMetal.addActionListener(this); pnlLookFeel.add(btnMetal); btnMotif = new JButton("Motif"); btnMotif.addActionListener(this); pnlLookFeel.add(btnMotif); btnWindows = new JButton("Windows"); btnWindows.addActionListener(this); pnlLookFeel.add(btnWindows); getContentPane().add(pnlLookFeel, BorderLayout.SOUTH); setSize(450, 250); setVisible(true); } public void actionPerformed(ActionEvent ae) { String strLookFeel = null; if (ae.getActionCommand().equals("Metal")) { strLookFeel = "javax.swing.plaf.metal.MetalLookAndFeel"; } else if (ae.getActionCommand().equals("Windows")) { strLookFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; } else if (ae.getActionCommand().equals("Motif")) { strLookFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; } else { System.err.println("Unrecognized L&F: " + ae.getActionCommand()); return; } try { UIManager.setLookAndFeel(strLookFeel); SwingUtilities.updateComponentTreeUI(this); } catch(UnsupportedLookAndFeelException ex1) { System.err.println("Unsupported LookAndFeel: " + strLookFeel); } catch(ClassNotFoundException ex2) { System.err.println("LookAndFeel class not found: " + strLookFeel); } catch(InstantiationException ex3) { System.err.println("Could not load LookAndFeel: " + strLookFeel); } Distributed Computing in JavaLab Tutorial Last updated: 9/8/2010 © Ngô Hải Anh - 7 - ngohaianh@ioit.ac.vn catch(IllegalAccessException ex4) { System.err.println("Cannot use LookAndFeel: " + strLookFeel); } } public static void main(String args[]) { LookFeel objLookFeel = new LookFeel(); } } 6. Căn chỉnh cửa sổ chương trình vào chính giữa màn hình. Lưu ý: Bài tập này là bài thêm, chỉ mang tính “vui vẻ” ñể các bạn làm sau khi ñã thực hành tốt, thành thạo các bài trên. import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class FrameCenter { public static void main(String[] args) { JFrame aWindow = new JFrame("This is the Window Title"); Toolkit theKit = aWindow.getToolkit(); Dimension wndSize = theKit.getScreenSize(); aWindow.setBounds(wndSize.width / 4, wndSize.height / 4, // Position wndSize.width / 2, wndSize.height / 2); // Size aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aWindow.setVisible(true); } }

Ngày đăng: 13/05/2014, 10:55

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan