1 / 24

Programare Orientata Obiect

Programare Orientata Obiect. Curs 12 Pachetele AWT si Swing. Continutul cursului. Introducere Platforma Java, Clase si Obiecte Modificatori, Constructori Pachete. Incapsularea datelor Mostenirea / Extinderea claselor Conversii de date. Clase si metode abstracte Interfete Exceptii (I)

thane
Download Presentation

Programare Orientata Obiect

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programare Orientata Obiect Curs12 Pachetele AWT si Swing Curs 12

  2. Continutul cursului • Introducere • Platforma Java, Clase si Obiecte • Modificatori, Constructori • Pachete. Incapsularea datelor • Mostenirea / Extinderea claselor • Conversii de date. Clase si metode abstracte • Interfete • Exceptii (I) • Exceptii (II) • Fire de executie • Pachetul I/O • Pachetele AWT si Swing Curs 12

  3. Curs 12 • Pachetele AWT si Swing 1.1 Privire de ansamblu 1.2 Abstract Window Toolkit (AWT) 1.3Swing 1.4 AWT sau Swing? 1.5 Construireauneiinterfetegrafice 1.6 Containeresicomponente 1.7 Hello World! 1.8 Managementulevenimentelor 1.9 Moduri de afisare a componentelor 1.10 Exemple Curs 12

  4. C12. Pachetele AWT si Swing • 1.1 Privire de ansamblu • GUI – Graphical User Interface • Java pune la dispozitie 2 librariipentrudezvoltarea de aplicatii cu interfatagrafica: • AWT – in pachetuljava.awt • Swing – in pachetuljavax.swing

  5. C12. Pachetele AWT si Swing • 1.2 Abstract Window Toolkit (AWT) • AWT este o librarieportabilapentrudezvoltarea de interfategrafice. • Face legaturadintre o aplicatie Java sisistemul de operarepe care ruleaza. • AWT “ascunde” detaliile de implementare ale interfeteigrafice. • AWT depinde de componente ale sistemului de operarepe care ruleazapentru a puteaconstruiinterfata cu utilizatorul.

  6. C12. Pachetele AWT si Swing • 1.2 Abstract Window Toolkit (AWT) • Avantaje: • Viteza • Look and feel – datoritafaptului ca folosestecomponente ale sistemului de operarearata similar cu alteaplicatii • Dezavantaje: • Portabilitateredusa – fiindca se folosesccomponente ale sistemului de operare pot saaparadependentespecifice • AWT oferadoar un numarredus de elemente de interfatagrafica

  7. C12. Pachetele AWT si Swing • 1.3 Swing • Libraria Swing are la bazalibraria AWT • Swing esteimplementata in intregime in Java

  8. C12. Pachetele AWT si Swing • 1.3 Swing • Avantaje: • Portabilitate – sunt implementate 100% in Java • Ofera o gama larga de componente grafice • Look and feel – componentele adopta stilul sistemului de operare pe care ruleaza • Dezavantaje: • Performanta redusa fata de AWT – datorita faptului ca nu foloseste API-urile sistemului de operare pentru desenarea interfetelor • Look and feel – componentele pot fi usor diferite fata de cele ale sistemului de operare

  9. java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent java.awt.Window javax.swing.JPanel java.awt.Frame javax.swing.JFileChooser javax.swing.JPopupMenu javax.swing.JFrame javax.swing.JToolbar javax.swing.JLabel C12. Pachetele AWT si Swing • 1.3 Swing

  10. C12. Pachetele AWT si Swing • 1.4 AWT sauSwing? • Swing!

  11. C12. Pachetele AWT si Swing • 1.5 Construireauneiinterfetegrafice • Pentru a construi o interfatagrafica cu utilizatorulestenevoiesaputemrealizacontainere (de exemplu, ferestre) in care saadaugam diverse componente • Pasi: • Se importapachetele AWT sau Swing • Se construiestecontainerul principal al aplicatiei • Se adaugaelemente de interfata • Se facmodificarilenecesarepentruinterpretareaevenimentelorceapar in urmainteractiunii cu utilizatorul • Se afiseazacontainerulaplicatiei

  12. C12. Pachetele AWT si Swing • 1.6 Containeresicomponente • Un container (obiect de tip Container) continesiafiseazacomponente (obiecte de tip Component) • Catevasubclase ale claseiComponent: Button, Checkbox, Label, Scrollbar, TextField, TextArea • Catevasubclase ale claseiComponent:Panel, Applet, Window, Frame

  13. C12. Pachetele AWT si Swing • 1.6 Containeresicomponente • In pachetul Swing un container poatefi: • JFrame– o fereastraclasica a uneiaplicatii cu toateleelementele de graficaobisnuite • JDialog– o ferestra care estedependenta de o altafereastra • JApplet – o zona de afisareintr-o pagina web (un applet este o aplicatie Java care ruleazaintr-un browser)

  14. C12. Pachetele AWT si Swing • 1.7 Hello World! • AWT • import java.awt.*; • public class Hello extends Frame { • Hello() { • Label hello = • new Label("Hello World"); • add(hello, "Center"); • setSize(200, 200); • setVisible(true); • } • public static void main(String argv[]) • { • new Hello(); • } • }

  15. C12. Pachetele AWT si Swing • 1.7 Hello World! • Swing • import javax.swing.*; • public class HelloWorldSwing { • public static void main(String[] args) { • JFrame frame = new JFrame("HelloWorldSwing"); • final JLabel label = new JLabel("Hello World"); • frame.getContentPane().add(label); • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); • frame.pack(); • frame.setVisible(true); • } • }

  16. C12. Pachetele AWT si Swing • 1.8 Managementulevenimentelor • De fiecare data cand un utilizatorapasa o tastasaufoloseste mouse-ul, apare un eveniment la nivelulinterfeteiutilizator. • Oriceobiect din cadrulaplicatieipoatefianuntatatuncicandapare un eveniment • Pentruaceastaobiectultrebuiesaimplementeze o anumitainterfatasisa se inregistreze ca event listener (adicasaanunte ca vreasainterpreteze un anumiteveniment)

  17. C12. Pachetele AWT si Swing • 1.8 Managementulevenimentelor • Creareaunuievent handler • Se creaza o clasaceimplementeaza o interfata de tip listener • public class MyClass implements ActionListener { • Se implementeazametodele din interfata, metodecevorfiexecutatecandvaapareaevenimentul • public void actionPerformed(ActionEvent e) • { … • } • Se inregistreaza o instanta din clasadefinita ca listener pentru o componenta • someComponent.addActionListener(instanceOfMyClass);

  18. C12. Pachetele AWT si Swing • 1.8 Managementulevenimentelor 1 import javax.swing.*; import java.awt.event.*; public class ButtonClickExample extends JFrame implements ActionListener { JButton b = new JButton("Click me!"); public ButtonClickExample() { b.addActionListener(this); getContentPane().add(b); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { b.setBackground(Color.CYAN); } public static void main(String[] args) { new ButtonClickExample(); } } 2 3

  19. C12. Pachetele AWT si Swing • 1.8 Managementulevenimentelor • Se declara o clasa ca implementeaza o interfata de tip listener (ActionListener) • Se inregistreaza o instanta a claseipentru a interpretaevenimenteleceapar la nivelulbutonului • Se definescactiunile care se intamplaatuncicandapareevenimentul

  20. C12. Pachetele AWT si Swing • 1.9 Moduri de afisare a componentelor • Exista 6 moduri de afisare (pozitionare) a componentelorintr-un container: • BorderLayout • BoxLayout • FlowLayout • GridBagLayout • GridLayout • CardLayout • Fiecare mod de afisarecontroleazapozitiasiordinea de aranjare a componentelor

  21. C12. Pachetele AWT si Swing • 1.9 Moduri de afisare a componentelor

  22. C12. Pachetele AWT si Swing • 1.10 Exemple (I) • import javax.swing.*; • import java.awt.event.*; • Import java.util.*; • public class ComboBoxExample implements ActionListener { • JComboBox box; • public ComboBoxExample() { • JFrame frame = new JFrame("ComboBoxExample"); • JPanel panel = new JPanel(); • Set s = new TreeSet(); • s.add(new Integer(1)); • s.add(new Integer(2)); • s.add(new Integer(3)); • box = new JComboBox(s.toArray()); • box.addActionListener(this); • panel.add(box); • frame.getContentPane().add(panel); • frame.pack(); • frame.setVisible(true); • } • public static void main(String[] args) { • new ComboBoxExample(); • } • public void actionPerformed(ActionEvent e) { • if ("comboBoxChanged".equals(e.getActionCommand())) { • System.out.println("User chose index " + box.getSelectedIndex()); • } } }

  23. C12. Pachetele AWT si Swing • 1.10 Exemple (II) • import javax.swing.*; • import java.awt.*; • public class GridLayoutTest extends JFrame { • JButton b1=new JButton("Red"), • b2=new JButton("Green"),b3=new JButton("Blue"), • b4=new JButton("Yellow"),b5=new JButton("Pink"); • public GridLayoutTest() { • setTitle("GridLayout Test"); • Container pane = getContentPane(); • pane.setLayout(new GridLayout(2,3)); • setBounds(0,0,300,100); • pane.add(b1); pane.add(b2); pane.add(b3); • pane.add(b4); pane.add(b5); • } • public static void main(String args[]) { • JFrame f = new GridLayoutTest(); • f.setVisible(true); • } • }

  24. Continutul cursului • Introducere • Platforma Java, Clase si Obiecte • Modificatori, Constructori • Pachete. Incapsularea datelor • Mostenirea / Extinderea claselor • Conversii de date. Clase abstracte • Interfete • Exceptii (I) • Exceptii (II) • Fire de executie • Pachetul I/O • Pachetele AWT si Swing Curs 12

More Related