Deel
SYNTRA-WEST
Initiatiecursus JAVA
Syntra-West
Syntra-West (vroeger Vormingsinstituut West-Vlaanderen) Doorniksesteenweg 220 8500 Kortrijk Tel. 056/26.02.00• Fax 056/22.81.07
i
Inhoudsopgave SYNTRA-WEST............................................................................................................................................................ 0 INITIATIECURSUS JAVA.......................................................................................................................................... 0 JBUILDER X ................................................................................................................................................................. 0 1.
INLEIDING.......................................................................................................................................................... 2 1.1 1.1.1 1.1.2 1.1.3 1.2 1.3 1.3.1 1.3.2 1.3.3 1.4 1.5 1.5.1 1.5.2
2.
JAVA EN JBUILDER .......................................................................................................................................... 2 Java ........................................................................................................................................................... 2 Java Platformen ........................................................................................................................................ 2 JBuilder ..................................................................................................................................................... 3 DOS-PROGRAMMA VERSUS GUI PROGRAMMA .................................................................................................. 3 APPLICATIE OF APPLET ..................................................................................................................................... 4 Applicatie (toepassing).............................................................................................................................. 4 Applet ........................................................................................................................................................ 4 JSP, Servlets, WebServices,....................................................................................................................... 4 WAAROM ZOU U JAVA-TOEPASSINGEN EN -APPLETS ONTWIKKELEN ? .............................................................. 4 PROGRAMMEREN IN JAVA ................................................................................................................................. 4 Ontwerp van formulieren (frame).............................................................................................................. 5 Coderen ..................................................................................................................................................... 5
KENNISMAKING MET PROGRAMMEREN IN JBUILDER...................................................................... 6 2.1 BASISTERMEN EN CONCEPTEN IN JAVA ............................................................................................................. 6 2.1.1 Forms (frame)............................................................................................................................................ 6 2.1.2 Werkbalk of Toolbar.................................................................................................................................. 8 2.1.3 Frame aanpassen ...................................................................................................................................... 8 2.1.4 Componenten plaatsen .............................................................................................................................. 8 2.1.5 Eigenschappen aanpassen......................................................................................................................... 9 2.1.6 Property-Event-Pane of Inspektor........................................................................................................... 10 2.1.7 Properties ................................................................................................................................................ 11 2.1.8 Events ...................................................................................................................................................... 12 2.1.9 Bewaren van het project.......................................................................................................................... 12 2.1.10 Een stand alone versie maken dmv een uitvoerbare Jar-bestand............................................................ 13 2.1.11 Project bestanden en automatisch gegeneerde bestanden ...................................................................... 16 2.1.12 Help mogelijkheden van JBuilder ........................................................................................................... 18
3.
LAYOUT MANAGERS .................................................................................................................................... 21 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9
4.
WAAROM LAYOUT MANAGERS GEBRUIKEN ?.................................................................................................. 21 LAYOUT PROGRAMMA .................................................................................................................................... 21 BORDERLAYOUT ............................................................................................................................................. 23 GRIDLAYOUT ................................................................................................................................................. 24 XYLAYOUT .................................................................................................................................................... 25 PANELAYOUT ................................................................................................................................................. 26 CARDLAYOUT ................................................................................................................................................ 26 FLOWLAYOUT ................................................................................................................................................ 28 GRIDBAGLAYOUT .......................................................................................................................................... 29 OBJECTGEORIËNTEERD PROGRAMMEREN EN JAVA ..................................................................... 32
4.1 DENKEN IN OBJECTEN: EEN ANALOGIE ........................................................................................................... 32 4.2 OBJECTEN EN KLASSEN ................................................................................................................................... 32 4.3 ATTRIBUTEN (PROPERTIES) EN GEDRAG .......................................................................................................... 33 4.3.1 Attributen................................................................................................................................................. 33
4.3.2 Gedrag..................................................................................................................................................... 33 4.3.3 Een klasse maken..................................................................................................................................... 34 4.4 OVERERVING, INTERFACES EN PAKKETTEN ..................................................................................................... 37 4.4.1 Overerving............................................................................................................................................... 37 4.4.2 Hoe werkt overerving ? ........................................................................................................................... 38 4.4.3 Enkelvoudige en meervoudige overerving............................................................................................... 39 4.4.4 Interfaces en pakketten ............................................................................................................................ 39 4.4.5 Een subklasse maken ............................................................................................................................... 39 5.
ENKELE GRONDBEGINSELEN VAN JAVA .............................................................................................. 43 5.1 5.2 5.2.1 5.2.2 5.2.3 5.3 5.4 5.4.1 5.4.2 5.4.3 5.4.4 5.5 5.5.1 5.5.2 5.5.3 5.5.4 5.5.5 5.5.6 5.5.7 5.6 5.6.1 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6
6.
PROGRAMMEREN VAN EEN REKENMACHINE .................................................................................... 58 6.1 6.2 6.3 6.4 6.4.1 6.4.2
7.
STATEMENTS EN EXPRESSIES .......................................................................................................................... 43 VARIABELEN EN GEGEVENSTYPEN .................................................................................................................. 43 Variabelen declareren ............................................................................................................................. 43 Namen van variabelen............................................................................................................................. 44 Typen variabelen ..................................................................................................................................... 44 COMMENTAAR ................................................................................................................................................ 46 LITERALS ........................................................................................................................................................ 46 Numerieke literals ................................................................................................................................... 46 Boole-literals ........................................................................................................................................... 46 Teken-literals........................................................................................................................................... 46 String literals........................................................................................................................................... 47 EXPRESSIES EN OPERATOREN .......................................................................................................................... 47 Rekenkundige bewerkingen ..................................................................................................................... 47 Meer over toewijzingen ........................................................................................................................... 48 Verhogen en verlagen.............................................................................................................................. 48 Vergelijkingen ......................................................................................................................................... 49 Logische operatoren bit operatoren........................................................................................................ 49 De bewerkingsvolgorde van operatoren ................................................................................................. 49 Bewerkingen met strings.......................................................................................................................... 49 ARRAYS, VOORWAARDEN EN LUSSEN ............................................................................................................. 49 Arrays ...................................................................................................................................................... 49 Klasse String en StringBuffer ................................................................................................................. 52 if voorwaarden ........................................................................................................................................ 53 switch voorwaarden ................................................................................................................................ 54 for lussen ................................................................................................................................................. 56 while- en do lussen .................................................................................................................................. 56
INTERFACE OPBOUWEN ................................................................................................................................... 58 EVENT-MODEL IN JAVA 1.1............................................................................................................................. 61 EVENT-CREATIE D.M.V JBUILDER ................................................................................................................... 62 DRIE MOGELIJKE MANIEREN OM EVENTS AF TE HANDELEN ............................................................................. 65 Expliciete event activering....................................................................................................................... 65 Standard Adapter en Anonymous adapter............................................................................................... 67
BOUWEN VAN EEN ELEGANTE INTERFACE ......................................................................................... 69 7.1 MENU’S EN POPUP - MENU’S ........................................................................................................................... 69 7.1.1 De hoofdmenu-component (java.awt.MenuBar of javax.swing.JMenuBar)............................................ 69 7.2 BIJKOMENDE INFO VIA DIALOGS OPVRAGEN................................................................................................... 75 7.3 SWING DIALOG-MOGELIJKHEDEN .................................................................................................................... 78 7.3.1 Berichtvensters ........................................................................................................................................ 78 7.3.2 Bevestigingsdialoogvensters.................................................................................................................... 79 7.3.3 Invoervensters ......................................................................................................................................... 80 7.3.4 Keuzevensters .......................................................................................................................................... 81
8.
KLASSEN EN TOEPASSINGEN MAKEN .................................................................................................... 82 8.1 KLASSEN DEFINIËREN ..................................................................................................................................... 82 8.2 INSTANTIE- EN KLASSEVARIABELEN MAKEN ................................................................................................... 82 8.2.1 Instantievariabelen definiëren................................................................................................................. 82
8.2.2 Constanten............................................................................................................................................... 82 8.2.3 Klassevariabelen ..................................................................................................................................... 83 8.3 METHODEN MAKEN ........................................................................................................................................ 83 8.3.1 Het sleutelwoord this............................................................................................................................... 84 8.3.2 Geldigheidsgebied en methodedefinities ................................................................................................. 85 WELK GETAL WORDT HIER AFGEDRUKT ?................................................................................................... 85 8.3.3 Argumenten doorgeven aan methoden .................................................................................................... 85 8.3.4 Klassemethoden....................................................................................................................................... 86 8.3.5 Java-toepassingen maken........................................................................................................................ 87 8.4 METHODEN MAKEN MET DEZELFDE NAAM, MAAR MET ANDERE ARGUMENTEN (=OVERLOADING).................. 87 8.5 CONSTRUCTORMETHODEN .............................................................................................................................. 87 8.5.1 Eenvoudige constructors ......................................................................................................................... 88 8.5.2 Een andere constructor oproepen ........................................................................................................... 89 8.5.3 Constructors herdefiniëren...................................................................................................................... 89 8.5.4 Methoden onderdrukken (=overriding)................................................................................................... 90 8.5.5 De originele methode oproepen .............................................................................................................. 91 8.5.6 Constructors onderdrukken ..................................................................................................................... 92 9.
DATASETS, TEXT-BESTANDEN EN DATA-AWARE COMPONENTEN.............................................. 93 9.1 TEXT-BESTANDEN KOPPELEN AAN EEN DATASET ............................................................................................ 93 9.2 SCHEMA - BESTANDEN OM VELDEN WEER TE GEVEN ....................................................................................... 98 9.2.1 Het kan ook zonder een schema-file !...................................................................................................... 98 9.2.2 Master/detail verbindingen leggen.......................................................................................................... 99
10.
EXCEPTIONS .............................................................................................................................................. 101
10.1 JAVA-EXCEPTIONS ........................................................................................................................................ 101 10.2 EXCEPTIONS BEHEREN .................................................................................................................................. 102 10.2.1 Consistentiecontrole op exceptions ....................................................................................................... 102 10.2.2 Code beveiligen en exceptions ondervangen......................................................................................... 102 10.2.3 Uitéénlopende typen exceptions onderscheppen ................................................................................... 104 10.2.4 De finally-clause.................................................................................................................................... 104 11.
DEBUGGEN.................................................................................................................................................. 105
11.1 GRAMMATICA LESSEN .................................................................................................................................. 105 11.2 EEN DEBUG SESSIE STARTEN ......................................................................................................................... 106 11.3 EEN BREAKPOINT PLAATSEN ......................................................................................................................... 109 11.3.1 De breakpoint-lijst bekijken .................................................................................................................. 109 11.4 WAARDEN VAN VARIABELEN OPVRAGEN (EVALUATE/MODIFY).................................................................... 109 12.
INPUT EN OUTPUT .................................................................................................................................... 110
12.1 FILE INPUT EN OUTPUT .................................................................................................................................. 110 12.1.1 Tekstrepresentatie en character codering (character encoding) .......................................................... 110 12.2 DE FILE CLASS .............................................................................................................................................. 110 12.3 DE RANDOMACCESSFILE CLASS ................................................................................................................... 111 12.3.1 Streams, readers en writers................................................................................................................... 112 13.
DATABASE MOGELIJKHEDEN VAN JBUILDER ............................................................................... 117
13.1 JDBC VERSUS ODBC................................................................................................................................... 117 13.1.1 JDBC Driver Types ............................................................................................................................... 117 13.2 OPZETTEN VAN JDBC - ODBC CONNECTIE MET DE EMPLOYEE.GDB DATABASE .......................................... 119 13.3 DATABASE TOEPASSING MET GRID, STATUSBAR EN NAVIGATOR. ................................................................. 120 14. 14.1 14.2 14.3 15.
DATABASE BENADERING VIA DATA MODULE IN JBUILDER..................................................... 124 WAAROM EEN DATA-MODULE GEBRUIKEN ................................................................................................... 124 CASE-STUDY ................................................................................................................................................. 124 OPLOSSING ................................................................................................................................................... 124 APPENDIX A................................................................................................................................................ 129