0% found this document useful (0 votes)
18 views26 pages

3rd Phase

The document contains multiple Java code examples demonstrating various functionalities including input handling with Scanner, StringTokenizer, BufferedReader, and GUI components like Frame, Button, TextField, and layout managers. Each example illustrates different concepts such as event handling, key and mouse listeners, and layout management in Java AWT. The code snippets are structured to provide practical implementations for beginners learning Java programming.

Uploaded by

loyal4615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

3rd Phase

The document contains multiple Java code examples demonstrating various functionalities including input handling with Scanner, StringTokenizer, BufferedReader, and GUI components like Frame, Button, TextField, and layout managers. Each example illustrates different concepts such as event handling, key and mouse listeners, and layout management in Java AWT. The code snippets are structured to provide practical implementations for beginners learning Java programming.

Uploaded by

loyal4615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

1. import java.util.

*;

public class ScannerClassExample

public static void main(String args[])

String input = "Java is a OOPs programming language.";

// Create a Scanner object for the input string

Scanner scanner = new Scanner(input);

// Default delimiter is whitespace

System.out.println("Tokens using default delimiter:");

while (scanner.hasNext())

{ System.out.println(scanner.next());

scanner.close();

//Using scanner class methods to read string,int,float

System.out.println("--------Enter Your Details--");

Scanner in = new Scanner(System.in);


System.out.print("Enter your name: ");

String name = in.nextLine();

System.out.println(" Name: “ +name);

System.out.print("Enter your age: ");

int i = in.nextInt();

System.out.println("Age: " + i);

System.out.print("Enter your salary: ");

double d = in.nextDouble();

System.out.println("Salary: " + d);

in.close();

}}
2. import java.util.*;

class StringTokenizerDemo {

public static void main(String args[]) {

int n;

int sum = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter integers with one

space gap:");

String s = sc.nextLine();

StringTokenizer st = new StringTokenizer(s, " ");

while (st.hasMoreTokens()) {

String temp = st.nextToken();


n = Integer.parseInt(temp);

System.out.println(n);

sum = sum + n;

System.out.println("sum of the integers is: "

+ sum);

}
3. import java.io.*;

class BRRead {

public static void main(String args[]) throws IOException

char c;

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

System.out.println("Enter characters, 'q' to quit.");

// read characters

do {

c = (char) br.read();

System.out.println(c);

} while(c != 'q');}}
4. import java.awt.*;

import java.awt.event.*;

public class ButtonExample {

public static void main(String[] args) {

Frame f=new Frame("Button Example");

TextField tf=new TextField();

tf.setBounds(50,50, 150,20);

Button b=new Button("Click Here");

b.setBounds(50,100,60,30);

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

tf.setText("Welcome to Javatpoint."); }
} );

f.add(b);f.add(tf);

f.setSize(400,400);

f.setVisible(true);

}}
5. import java.awt.*;

import java.awt.event.*;

public class TextFieldExample extends Frame implements ActionListener

TextField tf1,tf2,tf3;

Button b1,b2;

TextFieldExample(){

tf1=new TextField(); tf1.setBounds(50,50,150,20);

tf2=new TextField(); tf2.setBounds(50,100,150,20);

tf3=new TextField(); tf3.setBounds(50,150,150,20);

tf3.setEditable(false); b1=new Button("+");

b1.setBounds(50,200,50,50);

b2=new Button("-");

b2.setBounds(120,200,50,50);

b1.addActionListener(this);

b2.addActionListener(this);

add(tf1);add(tf2);add(tf3);add(b1);add(b2);

setSize(300,300);
setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e)

String s1=tf1.getText();

String s2=tf2.getText();

int a=Integer.parseInt(s1);

int b=Integer.parseInt(s2);

int c=0;

if(e.getSource()==b1)

c=a+b;

}else if(e.getSource()==b2)

c=a-b;

String result=String.valueOf(c);

tf3.setText(result);

public static void main(String[] args) {

new TextFieldExample();

}
6. import java.awt.*;

public class CheckboxGroupExample

CheckboxGroupExample(){

Frame f= new Frame("CheckboxGroup Example");

CheckboxGroup cbg = new CheckboxGroup();

Checkbox checkBox1 = new Checkbox("C++", cbg, false);

checkBox1.setBounds(100,100, 50,50);

Checkbox checkBox2 = new Checkbox("Java", cbg, true);

checkBox2.setBounds(100,150, 50,50);

f.add(checkBox1);

f.add(checkBox2);
f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(

String args[])

new CheckboxGroup

Example();

}
7. .import java.awt.*;

import java.awt.event.*;

public class ChoiceExample

ChoiceExample(){

Frame f= new Frame();

Label label = new Label();

label.setAlignment(Label.CENTER);

label.setSize(400,100);

Button b=new Button("Show");

b.setBounds(200,100,50,20);
Choice c=new Choice();

c.setBounds(100,100, 75,75);

c.add("C"); c.add("C++");

c.add("Java"); c.add("PHP");

c.add("Android");

f.add(c);f.add(label); f.add(b);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

b.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent e) {

String data = "Programming language

Selected: "+ c.getItem(c.getSelectedIndex());

label.setText(data); } }); }

public static void main(String args[]) {

new ChoiceExample(); }}
8. import java.awt.*;

/* We have extended the Frame class

here, thus our class "SimpleExample"

would behave like a Frame */

public class FirstFrame extends Frame{

FirstFrame()

Button b=new Button("Button!!");

// setting button position on screen

b.setBounds(50,50,50,50);

//adding button into frame

add(b);

//Setting Frame width and height


setSize(300,300);

//Setting the title of Frame

setTitle("This is my First AWT example");

/* By default frame is not visible so we are

setting the visibility to true to make it

visible.*/

setVisible(true);

public static void main(String args[]){

// Creating the instance of Frame

FirstFrame fr=new FirstFrame();}}


9. import java.awt.*;

import java.awt.event.*;

public class KeyListenerExample extends Frame

implements KeyListener{

Label l;

TextArea area;

KeyListenerExample(){

l=new Label();

l.setBounds(20,50,100,20);

area=new TextArea();

area.setBounds(20,80,300, 300);

area.addKeyListener(this);

add(l);add(area);

setSize(400,400);

setLayout(null);
setVisible(true); }

public void keyPressed(KeyEvent e) {

l.setText("Key Pressed");

public void keyReleased(KeyEvent e) {

l.setText("Key Released"); }

public void keyTyped(KeyEvent e) {

l.setText("Key Typed"); }

public static void main(String[] args) {

new KeyListenerExample();}}
10. .import java.awt.*;

import java.awt.event.*;

public class MouseListenerExample extends Frame

implements MouseListener{

Label l;

MouseListenerExample(){

addMouseListener(this);

l=new Label();

l.setBounds(20,50,100,20);

add(l);

setSize(300,300);

setLayout(null);

setVisible(true);

}
public void mouseClicked(MouseEvent e) {

l.setText("Mouse Clicked"); }

public void mouseEntered(MouseEvent e) {

l.setText("Mouse Entered"); }

public void mouseExited(MouseEvent e) {

l.setText("Mouse Exited"); }

public void mousePressed(MouseEvent e) {

l.setText("Mouse Pressed"); }

public void mouseReleased(MouseEvent e) {

l.setText("Mouse Released"); }

public static void main(String[] args) {

new MouseListenerExample(); } }
11. import java.awt.*;

public class MyFlowLayout{

Frame f;

MyFlowLayout(){

f=new Frame();

Button b1=new Button("1");

Button b2=new Button("2");

Button b3=new Button("3");

Button b4=new Button("4");

Button b5=new Button("5");

f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));

//setting flow layout of right alignment

f.setSize(300,300);

f.setVisible(true);

public static void main(String[] args)

new MyFlowLayout();

}
12. import java.awt.*;

public class BorderLayoutDemo {

Frame f;

BorderLayoutDemo(){

f=new Frame();

Button b1=new Button("Hello");;

Button b2=new Button("Students");;

Button b3=new Button("To");;

Button b4=new Button("Welcome ");;

Button b5=new Button("Java Online class ");;

f.add(b1,BorderLayout.NORTH);

f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);

f.add(b4,BorderLayout.WEST);

f.add(b5,BorderLayout.CENTER);

f.setSize(300,300);

f.setVisible(true);

public static void main(String[] args)

new BorderLayoutDemo();}}
13. import java.awt.*;

import java.awt.event.*;

class CardLayoutExample1 extends Frame implements

ActionListener

CardLayout card = new CardLayout(20,20);

CardLayoutExample1()

setLayout(card);

Button Btnfirst = new Button("first ");

Button BtnSecond = new Button ("Second");

Button BtnThird = new Button("Third");


add(Btnfirst,"Card1"); // first card to button1

add(BtnSecond,"Card2");

add(BtnThird,"Card3");

Btnfirst.addActionListener(this);

BtnSecond.addActionListener (this);

BtnThird.addActionListener(this); }

public void actionPerformed(ActionEvent e)

{ // call the next card

card.next(this);

}}

class CardLayoutExample

{ public static void main(String args[])


{ CardLayoutExample1 frame = new CardLayoutExample1();

frame.setTitle("CardLayout in Java Example");

frame.setSize(300,300);

frame.setResizable(false);

frame.setVisible(true);

You might also like