Welcome to java einfach ● objektorientiert ● verteilt ● interpretierend ● robust ● secure ● architekturneutral ● portabel ● schnell ● parallel(multitheded) ● 1 Vielfalt von Java-Anwendungen Android AndroidApp App Java Java Applikation Applikation Applet Applet Desktopapplikation Desktopapplikation mit mitGUI GUI(AWT/Swing) (AWT/Swing) Applikationsserver Applikationsserver (ohne (ohneUI) UI) JavaBeans JavaBeans (Javaenterprise) (Javaenterprise) Servlet Servlet Konsolapplikation Konsolapplikation(ohne (ohneGUI) GUI) JavaFX JavaFXApplikation Applikation 2 Erstes Java-Programm Dateiname: Klassenname.java Indiesem Fall: HelloOne.java public public class class HelloOne HelloOne {{ public public static static void void main(String main(String args[]) args[]) {{ System.out.println("welcome System.out.println("welcome to to java"); java"); System.out.println("today System.out.println("today is is "+new "+new java.util.Date()); java.util.Date()); }} }} 3 Kommandozeilenargumente public public class class HelloEcho HelloEcho {{ public public static static void void main(String main(String args[]) args[]) {{ for(int for(int i=0;i<args.length;i++) i=0;i<args.length;i++) System.out.println(args[i]); System.out.println(args[i]); // // oder oder for(String for(String s:args) s:args) System.out.println(s); System.out.println(s); System.exit(0); System.exit(0); }} }} 4 Mit Graphic User Interface (AWT) import import java.awt.*; java.awt.*; import import java.awt.event.*; java.awt.event.*; import import java.util.*; java.util.*; public public class class HelloTwo HelloTwo extends extends Panel Panel {{ Label Label l1=new l1=new Label("welcome Label("welcome to to java",Label.CENTER); java",Label.CENTER); Label Label l2=new l2=new Label("today Label("today is is "+new "+new Date(),Label.CENTER); Date(),Label.CENTER); Button Button close=new close=new Button("Close"); Button("Close"); }} public public HelloTwo() HelloTwo() {{ .. .. .. }} public public static static void void main(String main(String args[]) args[]) {{ .. .. .. }} 5 Der Constructor public public HelloTwo() HelloTwo() {{ setFont(new setFont(new Font("System", Font("System", Font.PLAIN,24)); Font.PLAIN,24)); setLayout(new setLayout(new GridLayout(3,1,0,10)); GridLayout(3,1,0,10)); add(l1); add(l1); add(l2); add(l2); add(close); add(close); close.addActionListener(new close.addActionListener(new ActionListener() ActionListener() {public {public void void actionPerformed(ActionEvent actionPerformed(ActionEvent e) e) {System.exit(0);}}); {System.exit(0);}}); }} Hier entsteht eine inner class, in diesem Fall eine anonomous class 6 Die main-Function public publicstatic staticvoid voidmain(String main(Stringargs[]) args[]) {{ Frame Framef=new f=newFrame("first Frame("firstDesktop DesktopApplication"); Application"); HelloTwo HelloTwohh=new =newHelloTwo(); HelloTwo(); f.add(h); f.add(h); f.setVisible(true); f.setVisible(true); f.pack(); f.pack(); }} 7 Das Applet import import java.applet.*; java.applet.*; import import java.awt.*; java.awt.*; public public class class HelloThree HelloThree extends extends Applet Applet {{ public public void void init() init() {{ Panel Panel h=new h=new HelloTwo(); HelloTwo(); add(h); add(h); }} Ein Applet wird immer in eine html-Seite }} eingebunden und mittels Browser oder appletviewer ausgeführt. <html> <html> <body> <body> <applet <appletcode=HelloThree code=HelloThreewidth=940 width=940height=540></applet> height=540></applet> </body> </body> </html> </html> 8 Verbesserte Version: public publicclass classHelloTwoBetter HelloTwoBetterextends extendsPanel Panel {{ Label Labell1=new l1=newLabel("welcome Label("welcometotojava",Label.CENTER); java",Label.CENTER); Label l2=new Label("today is "+new Label l2=new Label("today is "+newDate(),Label.CENTER); Date(),Label.CENTER); public publicHelloTwoBetter() HelloTwoBetter() {{ setFont(new setFont(newFont("System", Font("System",Font.PLAIN,24)); Font.PLAIN,24)); setLayout(new GridLayout(3,1,0,10)); setLayout(new GridLayout(3,1,0,10)); Button ist hier in der main-fkt. add(l1); add(l1); Somit taucht er bei Verwendung add(l2); add(l2); }} Des Panels als Component im Applet nicht mehr auf public publicstatic staticvoid voidmain(String main(Stringargs[]) args[]) {{ Frame Framef=new f=newFrame("first Frame("firstDesktop DesktopApplication"); Application"); HelloTwoBetter h =new HelloTwoBetter(); HelloTwoBetter h =new HelloTwoBetter(); f.add(h,BorderLayout.CENTER); f.add(h,BorderLayout.CENTER); Button Buttonclose=new close=newButton("Close"); Button("Close"); close.addActionListener(new close.addActionListener(newActionListener() ActionListener() {public void actionPerformed(ActionEvent {public void actionPerformed(ActionEvente){System.exit(0);}}); e){System.exit(0);}}); f.add(close,BorderLayout.SOUTH); f.add(close,BorderLayout.SOUTH); f.setVisible(true); f.setVisible(true); f.pack(); f.pack(); }} }} 9