Bài giảng Lập trình hướng đối tượng - Bài 7: Cải tiến chương trình đồ họa liệt kê các số nguyên tố - Lê Hồng Phương

Nội dung ● Nâng cấp chương trình liệt kê các số nguyên tố trong bài giảng trước: – Thêm các thực đơn – Chọn các kiểu giao diện khác nhau của Swing – Quản lí các ngoại lệ

pdf15 trang | Chia sẻ: thuongdt324 | Lượt xem: 441 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Bài giảng Lập trình hướng đối tượng - Bài 7: Cải tiến chương trình đồ họa liệt kê các số nguyên tố - Lê Hồng Phương, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Bài 7: Cải tiến chương trình đồ họa liệt kê các số nguyên tố Lê Hồng Phương phuonglh@gmail.com Khoa Toán-Cơ-Tin học Trường Đại học Khoa học Tự nhiên Hà Nội 2012-2013 Object-Oriented Programming: Exception 2 Nội dung ● Nâng cấp chương trình liệt kê các số nguyên tố trong bài giảng trước: – Thêm các thực đơn – Chọn các kiểu giao diện khác nhau của Swing – Quản lí các ngoại lệ 2012-2013 Object-Oriented Programming: Exception 3 Các thành phần đồ họa ● Sử dụng thêm các thành phần đồ họa: – JMenuBar – JMenu – JMenuItem – JRadioButtonMenuItem – JSeparator – JOptionPane ● ​Sử dụng thêm các lớp tiện ích: – ButtonGroup, SwingUtilities, UIManager 2012-2013 Object-Oriented Programming: Exception 4 Chương trình ● Thêm các thực đơn File, Look and Feel, Help. ● Thực đơn File có mục Exit – Cho phép thoát chương trình – Tương tự như nhấn chuột vào nút đóng cửa sổ. ● Thực đơn Look and Feel: – Có các kiểu giao diện khác nhau nằm trong các nút chọn. Người dùng chọn kiểu nào thì giao diện được trình bày theo kiểu đó. ● Thực đơn Help chứa mục About. 2012-2013 Object-Oriented Programming: Exception 5 Các kiểu giao diện ● Java hỗ trợ nhiều kiểu giao diện đồ họa khác nhau, gọi là các Look and Feel – LnF. ● Một số LnF thường gặp: – Meta – Nimbus – Motif – GTK (trên các hệ điều hành Unix, Linux) 2012-2013 Object-Oriented Programming: Exception 6 Các kiểu giao diện ● Để biết hệ thống Java trên máy có hỗ trợ sẵn những kiểu LnF nào: public class LookAndFeelLister { public static void main(String[] args) { try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { System.out.println(info.getClassName()); } } catch (Exception e) { e.printStackTrace(); } } } javax.swing.plaf.metal.MetalLookAndFeel com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel com.sun.java.swing.plaf.motif.MotifLookAndFeel com.sun.java.swing.plaf.gtk.GTKLookAndFeel 2012-2013 Object-Oriented Programming: Exception 7 Các kiểu giao diện 2012-2013 Object-Oriented Programming: Exception 8 Chương trình Thông báo lỗi khi người dùng nhập dữ liệu sai Khi người dùng chọn mục Help > About JOptionPane.showMessageDialog(PrimeNumberFrame.this, "A program for listing prime numbers", "About", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(PrimeNumberFrame.this, "Number format error!", "Error", JOptionPane.ERROR_MESSAGE); 2012-2013 Object-Oriented Programming: Exception 9 Chương trình: PrimeNumberFrame // create a menu bar containing some menus JMenuBar menuBar = new JMenuBar(); // ... // add the menu bar to the frame setJMenuBar(menuBar); Viết trong hàm tạo khi xây dựng cửa sổ chương trình. 2012-2013 Object-Oriented Programming: Exception 10 Chương trình: PrimeNumberFrame // File menu JMenu menu = new JMenu("File"); menu.setMnemonic(KeyEvent.VK_F); JMenuItem exitMenuItem = new JMenuItem("Exit"); exitMenuItem.setMnemonic(KeyEvent.VK_X); exitMenuItem.addActionListener(new ExitActionListener()); menu.add(exitMenuItem); menuBar.add(menu); class ExitActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { System.exit(0); } } Đặt phím tắt Alt+F 2012-2013 Object-Oriented Programming: Exception 11 Chương trình: PrimeNumberFrame // Look and Feel menu menu = new JMenu("Look and Feel"); menu.setMnemonic(KeyEvent.VK_L); ButtonGroup group = new ButtonGroup(); // Meta LnF JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem("Meta"); menuItem.setMnemonic(KeyEvent.VK_M); ActionListener lookAndFeelActionListener = new ChooseLookAndFeelActionListener( "javax.swing.plaf.metal.MetalLookAndFeel"); menuItem.addActionListener(lookAndFeelActionListener); group.add(menuItem); menu.add(menuItem); Các nút nằm trong cùng nhóm thì chỉ chọn được một tại một thời điểm. 2012-2013 Object-Oriented Programming: Exception 12 Chương trình: PrimeNumberFrame // Nimbus LnF (Java 6, update 10) menuItem = new JRadioButtonMenuItem("Nimbus"); menuItem.setMnemonic(KeyEvent.VK_N); lookAndFeelActionListener = new ChooseLookAndFeelActionListener( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); menuItem.addActionListener(lookAndFeelActionListener); group.add(menuItem); menu.add(menuItem); 2012-2013 Object-Oriented Programming: Exception 13 Chương trình: PrimeNumberFrame class ChooseLookAndFeelActionListener implements ActionListener { private String name; public ChooseLookAndFeelActionListener(String name) { this.name = name; } @Override public void actionPerformed(ActionEvent e) { try { UIManager.setLookAndFeel(name); } catch (ClassNotFoundException exp) { exp.printStackTrace(); } catch (InstantiationException exp) { exp.printStackTrace(); } catch (IllegalAccessException exp) { exp.printStackTrace(); } catch (UnsupportedLookAndFeelException exp) { exp.printStackTrace(); } SwingUtilities.updateComponentTreeUI(PrimeNumberFrame.this); PrimeNumberFrame.this.pack(); } } Đặt giao diện mới Cập nhật giao diện 2012-2013 Object-Oriented Programming: Exception 14 Các thành phần đồ họa thực đơn Object Component Container JComponent JMenuBar JPopupMenu JAbstractButton JSeparator JMenuItem JMenu JCheckboxMenuItem JRadioButtonMenuItem 2012-2013 Object-Oriented Programming: Exception 15 Chương trình ● Xem các tệp mã nguồn trong gói lecture6.primes2 – ​LookAndFeelLister.java – PrimeNumberApp.java – PrimeNumberFrame.java – PrimeNumbers.java