CS4273: Distributed System Technologies and Programming ILecture 2: Introduction to Java and GUI
CS4273: Distributed System Technologies and Programming I Lecture 2: Introduction to Java and GUI
Important References for Java ManualsWebsiteforJavaSwing(GUl)WebsiteforJavaSwingEventhandlerWebsiteforJava2StandardEditionSampleProgramsSource codes of demo programs are in unix system: ~jia/www/java/
Important References for Java Manuals • Web site for Java Swing (GUI) • Web site for Java Swing Event handler • Web site for Java 2 Standard Edition • Sample Programs Source codes of demo programs are in unix system: ~jia/www/java/
Java: an Object-Oriented Programming LanguageObject-Oriented ProgrammingEncapsulationInheritancePolymorphism
Java: an Object-Oriented Programming Language Object-Oriented Programming: • Encapsulation • Inheritance • Polymorphism
Class and Method Definitionpublic class dummyButton extends JApplet {class consists offields andString font;int style, size,methods:private JButton bold, italic;class class-name[extends superclass]public void init[implements interface] bold = new JButton("BOLD");add(bold);variable declarations;add(italic = new JButton("ITALIC");method declarations;font ="Helvetica";style =Font.PLAIN;size = 48;method declaration:[modifiers] returnT method (args)public void paint(Graphics g) (super.paint(g);statements;g.setFont(new Font(font, style, size);1fg.drawString("Hello Java!", 50, 100),class butnhandler implements ActionListenerpublic void actionPerformed (ActionEvent e)
Class and Method Definition • class consists of fields and methods: class class-name [extends superclass] [implements interface] { variable declarations; method declarations; } • method declaration: [modifiers] returnT method (args) { statements; } public class dummyButton extends JApplet { String font; int style, size; private JButton bold, italic; public void init() { bold = new JButton("BOLD"); add(bold); add(italic = new JButton("ITALIC")); font = "Helvetica"; style = Font.PLAIN; size = 48; } public void paint(Graphics g) { super.paint(g); g.setFont(new Font(font, style, size)); g.drawString("Hello Java!", 50, 100); } class butnhandler implements ActionListener { public void actionPerformed (ActionEvent e) { .}
Object Initialization and ConstructorAll variables must be initialized before use. Java automatically sets someinitialvaluesforvariablesoftheclass,butnotvariablesinmethodsA constructor is a special method, with the same name as the class, forinitialization. Java provides an empty & no-argument constructor if a classdoes not have one.class Rabbit {int Age,Rabbit (int Age)(//constructorthis.Age =Age;//differentiate from theAge in the parametervoid run(int duration,boolean zigzag)...)class RabbitGamepublic static void main(stringl args)Rabbitbunny=newRabbit(3);
Object Initialization and Constructor • All variables must be initialized before use. Java automatically sets some initial values for variables of the class, but not variables in methods. • A constructor is a special method, with the same name as the class, for initialization. Java provides an empty & no-argument constructor if a class does not have one. class Rabbit { int Age; Rabbit (int Age) { //constructor this.Age = Age; }//differentiate from the Age in the parameter void run(int duration,boolean zigzag){ . } } class RabbitGame { public static void main(string[] args) { Rabbit bunny = new Rabbit(3); . }