Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

GUI Components and Events JavaMethods An Introduction to Object-Oriented Programming TM.

Παρόμοιες παρουσιάσεις


Παρουσίαση με θέμα: "GUI Components and Events JavaMethods An Introduction to Object-Oriented Programming TM."— Μεταγράφημα παρουσίασης:

1 GUI Components and Events JavaMethods An Introduction to Object-Oriented Programming TM

2 15-2 Περιεχόμενα: l Πιο συστηματική εισαγωγή to basic swing components, their methods, and events they generate. l Τα Components που θα συζητηθούν: –JLabel –JButton –JToggleButton –JCheckBox –JComboBox –JSlider –JTextField –JPasswordField –JTextArea

3 15-3 GUI Components l Τα Components δημιουργούνται από constructors: l Για να γίνει χρήσιμο ένα component πρέπει να προστεθεί στο application’s “content pane” ή σε ένα άλλο component: JLabel guest = new JLabel ("Guest”); JPanel scorePanel = new JPanel(); scorePanel.add (guest);

4 15-4 GUI Events l Components (except JLabel) μπορεί να δημιουργήσει events. l Events are captured and processed by “listeners” — objects equipped για να διαχειρίζονται ένα συγκεκριμένο τύπο από events. l Διαφορετικοί τύποι events επεξεργάζονται από διαφορετικούς τύπους of listeners.

5 15-5 Interfaces l Ένα interface ορίζει μια ή περισσότερες public methods. l ένα interface είναι παρόμοιο με μια abstract class αλλά καμία από τις μεθόδους του μπορεί να έχει code. l μια class που ορίζεται να υλοποιεί ένα interface πρέπει να υλοποιήσει όλες τις μεθόδους του interface. l μια class μπορεί να κάνει implement διάφορα interfaces.

6 15-6 Interfaces (cont’d) l ένα object ενός class που κάνει implements ένα interface έχει ένα secondary data type αυτό του interface (επιπρόσθετα στον primary data type of the class). l Διάφοροι τύποι από “listeners” είναι interfaces. Το ίδιο object can serve as different types of listeners (as long as its class implements all the corresponding interfaces).

7 15-7 Listeners public class GoHandler implements ActionListener {... public void actionPerformed (ActionEvent e) {... }... JButton go = new JButton (“Go”); go.addActionListener (new GoHandler ()); Objects of this class are “GoHandlers” but also ActionListeners This method expects an ActionListener; a GoHandler object qualifies. This method is called automatically when the button is clicked

8 15-8 Listeners (cont’d) l Όταν υλοποιείται ένα event listener, οι programmers συχνά χρησιμοποιούν μια private embedded class που έχει πρόσβαση σε όλα τα fields of the surrounding public class. l αυτές, ονομάζονται inner classes και μπορούν να έχουν constructors και private fields, όπως όλες οι άλλες class. l Inner classes είναι προσβασιμες ΜΟΝΟ από της outer class τους.

9 15-9 Listeners (cont’d) public class MyPanel extends JPanel { private JButton go;... go = new JButton (“Go”); go.addActionListener (new GoHandler ());... private class GoHandler implements ActionListener { public void actionPerformed (ActionEvent e) { go.setText(“Stop”);... } go is accessible in constructors and methods of the inner class

10 15-10 Listeners (cont’d) l Δεν είναι αναγκαστικό να capture all events. l Αν δεν θες να ασχοληθείς με events από ένα component, απλά μην επισυνάψεις ένα listener σε αυτό. l Αν θες να capture events αλλά ξεχάσεις να κάνεις add a listener, τότε κανένα events will be captured (κοινή παράληψη).

11 15-11 Listeners (cont’d) l Listener methods παίρνουν αντίστοιχους type of event σαν argument. l Event objects έχουν χρήσιμες methods. Για παράδειγμα, getSource returns το object που παρήγαγε το event. l A MouseEvent έχει τις methods getX, getY.

12 15-12 Menus l Μπορείς να προσθέσεις ένα JMenuBar object σε JFrame ή JApplet. l Μπορείς να προσθέσεις JMenu objects σε JMenuBar. l Μπορείς να προσθέσεις και άλλα JMenus, JMenuItems, JCheckBoxMenuItems, JRadioButtonMenuItems, etc. to a JMenu.

13 15-13 JLabel Constructors: JLabel(String text); JLabel(ImageIcon icon); JLabel(String text, ImageIcon icon, SwingConstants.LEFT); // or CENTER, RIGHT, LEADING, TRAILING. Methods: void setText(String text); void setIcon(ImageIcon icon); Events: None

14 15-14 JButton Constructors: JButton(String text); JButton(ImageIcon picture); JButton(String text, ImageIcon picture); Methods: void addActionListener(ActionListener object) void setText(String text); void setActionCommand(String cmd); void setIcon(ImageIcon icon); void requestFocus(); Events: class... implements ActionListener { public void actionPerformed(ActionEvent e) { JButton b = (JButton)e.getSource(); String s = e.getActionCommand(); }

15 15-15 JCheckBox Constructors: JCheckBox(String text, boolean checked); JCheckBox(ImageIcon icon, boolean checked); JCheckBox(String text, ImageIcon icon, boolean checked); Methods: void addActionListener(ActionListener object) boolean isSelected() void setSelected(boolean checked) void setText(String text); void setIcon(ImageIcon icon); Events: class... implements ActionListener { public void actionPerformed(ActionEvent e) { JCheckBox b = (JCheckBox)e.getSource(); if (b == checkBox1 && b.isSelected())... }

16 15-16 Layouts l ένας layout manager είναι ένα είδος «στρατηγικής» (“strategy”) για τοποθέτηση components on the content pane or another component (usually a panel). l In Java, the content pane and any GUI component is a Container. l A layout is chosen by calling the container’s setLayout method.

17 15-17 Layouts (cont’d) l Layouts are used to achieve some degree of platform independence and scalability. l AWT/Swing support several layout managers. Here we consider four: FlowLayout, GridLayout, BorderLayout, BoxLayout. l These classes implement the java.awt.LayoutManager interface.

18 15-18 FlowLayout l τοποθετεί components σε μια γραμμή όσο χωράνε, έπειτα ξεκινά νέα γραμμή. l Χρησιμοποιεί “best judgement” για το spacing components. Centers by default. l Lets each component assume its natural (preferred) size. l Χρησιμοποιείται συχνά για τοποθέτηση buttons on panels.

19 15-19 FlowLayout (cont’d) Container c = getContentPane(); c.setLayout (new FlowLayout() ); c.add (new JButton ("Next Slide")); c.add (new JButton ("Previous Slide")); c.add (new JButton ("Back to Start")); c.add (new JButton ("Exit"));

20 15-20 GridLayout l Χωρίζει το panel into a grid όταν δίνεται ο αριθμός των γραμμών (rows) και των στιλών (columns). l τα components τοποθετούνται στα κελιά (cells) του grid. l Αναγκάζει το μέγεθος κάθε component να καλύπτει ολόκληρο το cell. l Προσθέτει επιπλέον spacing ανάμεσα στα cells.

21 15-21 GridLayout (cont’d) Container c = getContentPane(); c.setLayout (new GridLayout(3, 2, 10, 20 )); c.add (new JButton ("Next Slide")); c.add (new JButton ("Previous Slide")); c.add (new JButton ("Back to Start")); c.add (new JButton (”Last Slide")); c.add (new JButton ("Exit")); Extra space between the cells

22 15-22 BorderLayout l Divides the area into five regions and adds a component to the specified region. l Forces the size of each component to occupy the whole region. NORTH SOUTH CENTEREASTWEST

23 15-23 BorderLayout (cont’d) Container c = getContentPane(); c.setLayout(new BorderLayout() ); c.add (new JButton ("Next Slide"), BorderLayout.NORTH); c.add (new JButton ("Previous Slide"), BorderLayout.SOUTH); c.add (new JButton ("Back to Start"), BorderLayout.EAST); c.add (new JButton ("Last Slide"), BorderLayout.WEST); c.add (new JButton ("Exit"), BorderLayout.CENTER);

24 15-24 BoxLayout l Σε ένα horizontal box, τα components τοποθετούνται horizontally, από αριστερά προς τα δεξιά. l Σε ένα vertical box, τα components τοποθετούνται vertically, από πάνω προς τα κάτω. “Horizontal” or “vertical” has nothing to do with the shape of the box itself.

25 15-25 BoxLayout (cont’d) l BoxLayout είναι ο default type of layout για ένα Box container. l The idiom for working with boxes is slightly different: Box box1 = Box.createHorizontalBox(); box1. add (...); // add a spacer, 60 pixels: box1.add(Box.createHorizontalStrut (60); Box box2 = Box.createVerticalBox();...

26 15-26 BoxLayout (cont’d) Container c = getContentPane(); c.setLayout(new FlowLayout() ); Box box = Box.createVerticalBox(); box.add (new JButton ("Next Slide")); box.add (new JButton ("Previous Slide")); box.add (Box.createVerticalStrut (20) ); box.add (new JButton ("Exit")); c.add (box);

27 15-27 Default Layouts l Each component has a default layout manager, which remains in effect until the component’s setLayout method is called. l Some of the defaults are: Content pane BorderLayout JPanel FlowLayout Box BoxLayout


Κατέβασμα ppt "GUI Components and Events JavaMethods An Introduction to Object-Oriented Programming TM."

Παρόμοιες παρουσιάσεις


Διαφημίσεις Google