Posts

Panel Control

Panel Control - Panel is called as the group of controls.  import java.awt.*; class panelc { panelc() {    Frame f1 = new Frame();   Panel p1 = new Panel();      //setting the property of Panel[Changing BackGround Color]   p1.setBackground(Color.yellow);     //set the properties (left,top,width, height )   p1.setBounds(40,80,200,200);   Button b1 = new Button("click me");   //set the properties   b1.setBounds(50,100,80,30);   //set the background color of Button   b1.setBackground(Color.red);   //add button to panel   p1.add(b1);   //add panel to the Frame   f1.add(p1); //set properties of frame  f1.setSize(500,500);  f1.setLayout(null);  f1.setVisible(true); } public static void main(String args[]) {   panelc pl1 = new panelc(); } }

Menu Bar Control

 import java.awt.*; class menub  { menub() {    Frame f1 = new Frame("This is an example of menu bar");    MenuBar mb = new MenuBar();    Menu menu1 = new Menu("FILE");       MenuItem i1 = new MenuItem("New");       MenuItem i2  = new MenuItem("Save");    MenuItem i3 = new MenuItem("Save As");    //add Menu items to Menu object (menu1)    menu1.add(i1);    menu1.add(i2);    menu1.add(i3);      //add Menu to Menu Bar    mb.add(menu1);  // set menu bar to frame   f1.setMenuBar(mb);   //properties of frame    f1.setSize(400,400);   f1.setLayout(null);   f1.setVisible(true);   }  public static void main(String args[]) {   menub m1 = new menub(); } }

Creating Control Programs

  1. TextField- TextField is used for singleLine input. PROGRAM- import java.awt.*; class text1  {  text1() {    Frame f1 = new Frame("e.g TextBox");     TextField t1 = new TextField("Enter your name");   t1.setBounds(50,100,200,30);   f1.add(t1);  f1.setSize(400,400);  f1.setLayout(null);  f1.setVisible(true); }  public static void main(String args[]) {    text1 te1 = new text1(); } } 2. TextArea- TextArea is used for MultiLine input. PROGRAM- import java.awt.*; class text2  {  text2() {     Frame f1 = new Frame("Text area example");     TextArea ta1 = new TextArea("WELCOME TO JAVA");         ta1.setBounds(10,30,300,300);     f1.add(ta1);    f1.setSize(400,400);   f1.setLayout(null);   f1.setVisible(true); } public static void main(String args[]) {    text2 ta2 = new text2(); } } 3. Checkbox-  Checkbox is used for...

Creating button control

Frame Creation-  Frame can be created using two methods-  1. Inherites Frame class.  2. Create the object of Frame class.  Here we will create button using Frame(through second method)-  import java.awt.*;  class first  {  first()  {  //Create an object of Frame  Frame F1= new Frame();  //Create an object of button Button b1= new Button ("click me");   // Set the attribute of button //setBounds (left,top,width,height); b1.setBounds(50,80,80,80);   //add() is used to add the button control to Frame F1.add(b1);  //Call the functions using Frame's object F1.setSize(400,400);  F1.setLayout(null);  F1.setVisible(true);   }  public static void main(String args[])  {  first Fst1= new first();  }  }

AWT

AWT Stands for Abstract Window Toolkit.  It is an API. It is used to create graphical user interface/ tools. How to include library for using awt-  import java.awt.*;  * includes all functions and utilities under awt library. Inherites the Frame to your driver class/ main class-  class first extends Frame {  // Name of class is first } Use of constructor- (inside the driver class) class first extends Frame { first()  {  } } Creating Frame - We need to create Frame inorder to create tools. Firstly, Frame is created inside it we create tools.   class first extends Frame {  first()   {   setSize(400,400);   setLayout(null);   setVisible(true);    }   } Here creates 3 functions inside the constructor -  1.setSize(height,width);  2.setLayout(null);  3.setVisible(true);   Call the Constructor- (in the main method) class first extends Frame {  first()...

String Handling

  String - It is a collection of characters.  Program-  public class str { public static void main(String args[])  { String data; data = "Mynameiskhan"; System.out.println(data);  } }    Functions used in String handling- -> used for output 1. charAt(index) - it is used to display a character in the string, by giving the appropriate index.   ex- String data1 = "Chitransh";  data1.charAt(2); -> i 2. codePointAt(index) - it is used to display the ascii value of given character. ex- String data1 = "Chitransh";  data1.codePointAt(3);  -> 116(ascii code of t) 3. codePointBefore(index) - giving the ascii code of the character before the given index of the character. ex- String data1= "Chitransh";  data1.codePointBefore(3); -> 105(ascii code of i) // index is given 3 but displays the output for index position 2.  4. compareTo() - it returns the values in the form of 0(if true) or non zero(if false). ex-String...

File Handling

  File-  file is a location in a computer system where the information is stored. Types of file- 1. System file - exe files(run by the system) 2. User file - txt,.java,.c (all user defined files)  File handling- manipulation with the file like creation, updation, deletion or etc. Two properties of file- 1. Name 2. Extension Works on two methods-  1. file will be created. 2. it is already existed. How to create a file-  step 1: we have to include a package to create a file. import java.io.File; step 2: Create an object of File. File f1 = new File(); step 3: pass the name of file in the parameter. File f1 = new File("Hi.txt"); step 4: check wheather the file will be created or already existed using createNewFile(). step 5: Put the whole program inside try catch block. Inorder to catch the Exception we have to include -  import java.io.*; (*- symbolises all utilities) Program - import java.io.File; import java.io.*; public class app {  public static void ...