You are on page 1of 19

Applet program:-Maximum of 3 number

import java.awt.*; import java.applet.*; import java.awt.event.*; /*<applet code="largestno class width=100 height=200> </applet>*/
5/4/12

public void actionPerformed(ActionEvent ae) { repaint(); } public void paint { String s1,s2,s3; g.drawString("Enter three no",10,10); s1=t1.getText(); 5/4/12
Enter three number 0 0 Ma Ma x (Graphics g)x 0 Largest No: 0

Moving Banner program import java.awt.*; import java.applet.*; public class SampleBanner extends Applet imple ments Runnable{ String str = "This is a simple Banner "; Thread t ; boolean b; public void init() { setBackground(Color.gray); setForeground(Color.yellow); }
5/4/12

public void paint(Graphics g) g.drawRect(1,1,300,150); g.setColor(Color.yellow); g.fillRect(1,1,300,150); g.setColor(Color.red); g.drawString(str, 1, 150); }

This is a simple Banner This is a simple Banner

5/4/12

1. 2.

Java programs can run on a wide range of architecture and host native operating system. 3. Java beans are java class or program component build beans to make your program more reusable or configurable software component. 4. Many of the core class specially the visual component in the awt and swing packages are beans. 5. Applet are client side programming that is why applets are called thick or fat client where as servlets are server side programming and are called thick client . 6. Typically a servlet is used to process a html form submitted by web client return on html response page . 7. A variation on servlets are java server pages (jsp) are written in html but contain java code snippets included in speed tags . 8. Application server compile jsp into servlet ,a process called page computation and then run the resulting servlet on the server side. 9. Think of a servlet as java class with embedded html and a jsp as a html document with embedded java . 10. A popular practice is to use servlet to service request from html and jsp to return html responses. 11.5/4/12 EJB(Enterprise Java Beans )are server side component used in distributed enterprise environment.

Important Points Java is more than just a programming language it is also platform.

The java -2 platform comes in three edition Standard edition. Enterprise edition. Macro edition J2SE: 1. It provides a complete java language and core class, provides the API required by any program that runs in a single JVM . 2. It also contain the API that allows a set of java program running on different JVM to communicate with each other in a client server on peer to peer topology.J2SE is basis for two edition J2EE: 1.J2EE primarily design for server side java component that participate in web or enterprise edition 2.J2EE component includes servlet and jsp used in web application EJB used In enterprise application . 3.J2EE design different kind of container and each container has a JVM . e.g.> the web browser provides the container for an applet.servlet and jsp run in web container and EJ B run in EJB container . J2ME: It design for embedded devise and consumer products such as pda smart card and mobile phone The JRE is highly optimized to allow limited i/o capabilities processing power and memory space.J2ME 5/4/12 technology includes tool kits for wireless devise ,database access

Layout Manager:
Layout manager automate positioning of components within containers. Two interfaces java.awt.LayoutManager and java .awt.LayoutManager 2 provide the base for all layout manager classes. Layout Manager 2 interface is an extension of layout manager .These interfaces define the method necessary for arranging component objects inside container s. Interface LayoutManager LayoutManager2 class BorderLayout BoxLayput CardLayout flowLayout GridLayout

Note: Where you can a new container component you should call the method set Layout() for it by specifying an object of one of the class that implement layout manager .Each container class has default layout manager but it is best to set LayoutManager explicitly.
5/4/12

1.Border Layout

4.Card Layout

2.Flow Layout 5.Box Layout

3.Grid Layout

5/4/12

Border layout:
If you dont fill all areas the empty one collapse so that they take up no space and the other areas expands to fill the container. When you add a container to container using the border layout you specify in which areas of layout to place the component by using one of the constant NORTH ,WEST, CENTER, EAST, SOUTH that define in border layout . By default Jwindow,jframe,jdialog have Border Layout . import java.awt.*; import javax.swing.*; public class A extends JFrame { public A (String title){ super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) JLabel greeting=new JLabel("happy new year 2011"JLabel.CENTER); getContentPane().add(greeting,BorderLayout.CENTER); setSize(300,200); setVisible(true); } 5/4/12

Example of JSlider
import java.awt.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class fontSlider extends JFrame implements ChangeListener { Jlabel text=new JLabel("sample text",JLabel.CENTER); JPane p= new JPane(new BorderLayout()); p.setBorder(BorderFactory.createTitleBorder("font size")); JSlider JS=new JSlider(); p.add(JS,BorderLayout.CENTER); getContentPane().add(p.BorderLayout.SOUTH); public fontSlider (string title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(text,BorderLayout.NORTH); setSize(300,400); setVisible(true); }

5/4/12

int initvalue=text.getFont().get size(); JS.setMaximum(0); JS.setMaximum(100); JS.setValue(initvalue); JS.setMajorTickSpacing(10); JS.setMinorTickSpacing(5); JS.setPaintLabels(true); JS.setPaintTicks(true); JS.addChangeListener(this); } public void stateChanged(ChangeEvent e) { int newsize=js.getValue(); Font obj=text.getFont(); Font newobj=new Font(obj.getName(),obj.getStyle(),newsize); text.setFont(new obj); repaint(); } public static void main(String args[]) { new fontSlider("example of slider"); } }

5/4/12

Event Handling
The awt Api include the package java.awt.event, in which u will find the classes that encapsulate the event . All class in this package that defines events are sub class of awt Event ,which is the sub class of java.util.event object Event object is the super class of the all java that define event .The swing API separates its event related class into a package called javax.swing.event.Most swing event class are direct sub class of event object not awt. Example of AWT event classes: 1.Adjustment Event 2.Component Event 3.Container Event 4.Focus Event 5.InputEvent 6.Item Event 7.Key event 5/4/12 8.Mouse Event

Example of swing Event Classes: 1.Change event 2.InternalFrameEvent 3.ListSelectionEvent 4.MenuEvent 5.TableModeEvent 6.TreesSelectionEvent Listener Interface: The awt and swing api define a set of interface called listener .Each kind of event has listener interface for e.g. textListener interface correspond to the text event class .The only method in the textListener interface is TextValuechanges ().the structure of the listener Interface is regular and predictable. Associated Listener Adapter Class EventClass Interface Nil ComponentEvent Component Listener Nil ActionEvent ActionListener MouseAdapter MouseEvent MouseListener Nil TextEvent TextListener Nil ChangeEvent ChangeListener GeneralSyntax 5/4/12 XXXEvent XXXAdapter

Method General Syntax: VoidXXX----(XXXEvent e) e.g. ActionPerformed(ActionEvent e) MouseClicked(MouseEvent e) MouseRelesed(MouseEvent e) MousePressed(MouseEvent e) MouseEntered(MouseEvent e) MouseExited(MouseEvent e) Note: 1.All Listener method have return type void. 2.All Listener method take one argument an object of an event class . 3.The Listener interface in java.awt.event all extends the interface java.util.event. 4.You can implement a listener interface directly for listener interface with more than one method you can alternatively implement the listener indirectly by extending its adapter class. 5.It makes no sense to have an adapter class for an interface with only one method because the reason to extend an adapter class is to provide fewer method implementation than the interface define .

The adapter provides empty implementation of all the method in the interface ,the advantage of the adapter class that extend them can implement only the handler of interest and inherit the empty implementation of all other methods. The adapter class extends the java.lang.object. 5/4/12

Example of JTextField and JTextArea


import java.awt.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.nio.*; import java.nio.channels.*; import java.io.*; public class js extends JFrame { public js(String t) { super(t); final JTextArea upper=new JTextArea(); final JTextArea lower=new JTextArea(upper.getDocument()); JScrollPane UpperScroll=new JScrollPane(upper); JScrollPane lowerScroll=new JScrollPane(lower); JSplitPane sp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,UpperScroll,lowerScrol l); final JTextField f=new JTextField(); 5/4/12 f.setToolTipText ("Enter File Name:");

public void actionPerformed(ActionEvent e) { if(f.getText()!=null) try{ FileInputStream fis=new FileInputStream(f.getText()); FileChannel fc=fis.getChannel(); ByteBuffer bb=ByteBuffer.allocate((int)fc.size()); fc.read(bb); upper.setText(new String(bb.array())); fc.close(); fis.close(); } catch(IOException ioe) {}}} }; JPanel entry=new JPanel(new BorderLayout()); entry.add(new JLabel("File"),BorderLayout.WEST); entry.add(f,BorderLayout.CENTER); getContentPane().add(entry,BorderLayout.N ORTH); getContentPane().add(sp,BorderLayout.CENT ER); setSize(500,400); f.requestFocus(); setVisible(true); 5/4/12 }

import java.awt.*; import java.awt.event.*; public class js extends Frame { public static void main(String args[]) { Frame F=new Frame("MyFrame"); MenuBar mb=new MenuBar(); F.setMenuBar(mb); Menu mfile=new Menu("File"); Menu medit=new Menu("Edit"); mb.add(mfile); mb.add(medit); MenuItem mnew=new Menu("new"); MenuItem mopen=new Menu("Open"); MenuItem mclose=new Menu("Close"); MenuItem mcut=new Menu("cut"); mfile.add(mnew); mfile.add(mopen); mfile.add(mclose); 5/4/12

Example of MenuBar and Menu

mfile.add(mcut); mclose.setEnabled(false); CheckboxMenuItem mprint=new CheckboxMenuItem("Print"); mfile.add(mprint); Menu mfont=new Menu("font"); mfile.add(mfont); mfont.add("Arial"); mfont.add("time new roman"); mfont.add("contry book"); F.setSize(200,300); F.setVisible(true); }

5/4/12

Program to print Methods of a class


import java.lang.reflect.*; Class A { Public static void main(String args[]) { Try { Class c=class.forName(args[0]); Method m[]=c.getDeclaredMethods(); For(int i=0;i<m.length;i++) System.out.println(m[i].toString()); } Catch(Exception e){ System.out.println(e); } }

5/4/12

You might also like