Вы находитесь на странице: 1из 6

ANS 1

An exception (or exceptional event) is a problem that arises during the execution of a
program. When an Exception occurs the normal flow of the program is disrupted and
the program/Application terminates abnormally, which is not recommended, therefore,
these exceptions are to be handled.
An exception can occur for many different reasons. Following are some scenarios where
an exception occurs.
 A user has entered an invalid data.
 A file that needs to be opened cannot be found.
 A network connection has been lost in the middle of communications or the JVM has run out of memory.
Some of these exceptions are caused by user error, others by programmer error, and
others by physical resources that have failed in some manner.
Based on these, we have three categories of Exceptions. You need to understand them to
know how exception handling works in Java.
 Checked exceptions − A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions.
These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.
For example, if you use FileReader class in your program to read data from a file, if the
file specified in its constructor doesn't exist, then aFileNotFoundException occurs, and the
compiler prompts the programmer to handle the exception.
Example
Live Demo

import java.io.File;

import java.io.FileReader;

public class FilenotFound_Demo {

public static void main(String args[]) {

File file = new File("E://file.txt");

FileReader fr = new FileReader(file);

If you try to compile the above program, you will get the following exceptions.
Output
C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
FileReader fr = new FileReader(file);
^
1 error

ANS 2
Main thread in Java
Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread,
and each thread defines a separate path of execution.
Main Thread
When a Java program starts up, one thread begins running immediately. This is usually called the main thread of our program, because it is the one that is executed when our program
begins.
Properties :
 It is the thread from which other “child” threads will be spawned.
 Often, it must be the last thread to finish execution because it performs various shutdown actions

Main thread is created automatically when program runs.


Child thread is created by the main thread.
Example:
public class mainchild implements Runnable {
Thread t1;
public mainchild(String a) {
t1 = new Thread(this, a);
t1.start();
}
public void run() {
for (int x = 1; x <= 3; x++) {
System.out.println(x + " this thread is "
+ Thread.currentThread().getName());
}
}
public static void main(String[] args) {
mainchild mch = new mainchild("Child ");
for (int x = 1; x <= 3; x++) {
System.out.println(x + " this thread is "
+ Thread.currentThread().getName());
}
}
}
ANS 3

We Know that java provides us the facility for both creating CUI and GUI Programs All the Previous
Topics are Related with the CUI But Applets Provides the ability to user for creating Graphical
Programs Like Creating Buttons, Creating Events such that Executing the Code when a user Clicks on
Button Etc. There are two type of Applets Like Stand Alone or either Local Applets.
The Applets those are Created on Client Machine are Called as Local Applets and the Applets Those are
located at Remote Computer but we are using them then they are Called as Remote Applets . Applets
are used for creating Graphical Programs
We can create our own applet by own design and embed them into web pages. Local applets are developed in a single system and it is stored in
a local system. The web page will search the local system directories, find the local applet and execute it. Execution of local applets doesn't
required any internet connection.
Specifying a Local Applet
<applet codebase="path" code="NewApplet.class" width=120 height=120 >
</apple>
In the above listing , the codebase attribute specifies a path name on your system for the local applet, whereas the code attribute
specifies the name of the byte-code file that contains the applet's code. The path specified in the codebase attribute is relative to the
folder containing the HTML document that references the applet.

Remote Applets

An remote applet is that which is developed by someone else and stored on a remote computer connected to the internet.
Specifying a Remote Applet
<applet
codebase="http://www.myconnect.com/applets/"
code="NewApplet.class"
width=120
height=120 >
</applet>
The only difference between Local Applet and Remote Applet is the value of the codebase attribute. In the first case, codebase specifies
a local folder, and in the second case, it specifies the URL at which the applet is located.
ANS 4

A Frame is a top-level window with a title and a border. The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using
the getInsets method. Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for
rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top), and has a size of width -
(insets.left + insets.right) by height - (insets.top + insets.bottom).

A frame, implemented as an instance of the JFrame class, is a window that has decorations such as a border, a title, and supports button components that close or iconify the window.
Applications with a GUI usually include at least one frame. Applets sometimes use frames, as well.
To make a window that is dependent on another window — disappearing when the other window is iconified, for example — use a dialog instead of frame.. To make a window
that appears within another window, use an internal frame.

Creating and Showing Frames

Here is a picture of the extremely plain window created by the FrameDemo demonstration application. You can find the source code in FrameDemo.java.

The following FrameDemo code shows how to create and set up a frame.

//1. Create the frame.


JFrame frame = new JFrame("FrameDemo");

//2. Optional: What happens when the frame closes?


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//3. Create components and put them in the frame.


//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

//4. Size the frame.


frame.pack();

//5. Show it.


frame.setVisible(true);

Here are some details about the code:

1. The first line of code creates a frame using a constructor that lets you set the frame title. The other
frequently used JFrame constructor is the no-argument constructor.
2. Next the code specifies what happens when your user closes the frame. The EXIT_ON_CLOSE operation exits the program when your user closes the frame. This behavior is
appropriate for this program because the program has only one frame, and closing the frame makes the program useless.
3. The next bit of code adds a blank label to the frame content pane. If you're not already familiar with content panes and how to add components to them, please readAdding
Components to the Content Pane.

For frames that have menus, you'd typically add the menu bar to the frame here using the setJMenuBar method. See How to Use Menus for details.

4. The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by
callingsetSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame
layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

This example does not set the frame location, but it is easy to do so using either the setLocationRelativeTo or setLocation method. For example,
the following code centers a frame onscreen:
frame.setLocationRelativeTo(null);

ANS 5

Abstract Window Toolkit(AWT)


AWT contains large number of classes and methods that allows you to create and manage graphical user interface ( GUI ) applications, such as windows, buttons,
scroll bars,etc. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. The tools provided by the AWT are
implemented using each platform's native GUI toolkit, hence preserving the look and feel of each platform. This is an advantage of using AWT.But the disadvantage
of such an approach is that GUI designed on one platform may look different when displayed on another platform.
AWT is the foundation upon which Swing is made i.e Swing is a set of GUI interfaces that extends the AWT. But now a days AWT is merely used because most GUI
Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature.
AWT Hierarchy

Component class
Component class is at the top of AWT hierarchy. Component is an abstract class that encapsulates all the attributes of visual component. A component object is
responsible for remembering the current foreground and background colors and the currently selected text font.

Container
Container is a component in AWT that contains another component like button, text field, tables etc.Container is a subclass of component class. Container class
keeps track of components that are added to another component.

Panel
Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or border. It is container that is used for holding components.

Window class
Window class creates a top level window. Window does not have borders and menubar.

Frame
Frame is a subclass of Window and have resizing canvas. It is a container that contain several different components like button, title bar, textfield, label etc. In Java,
most of the AWT applications are created using Frame window. Frame class has two different constructors,

Frame() throws HeadlessException

Frame(String title) throws HeadlessException

Creating a Frame
There are two ways to create a Frame. They are,

1. By Instantiating Frame class


2. By extending Frame class

Creating Frame Window by Instantiating Frame class


import java.awt.*;
public class Testawt
{
Testawt()
{
Frame fm=new Frame(); //Creating a frame.
Label lb = new Label("welcome to java graphics"); //Creating a label
fm.add(lb); //adding label to the frame.
fm.setSize(300, 300); //setting frame size.
fm.setVisible(true); //set frame visibilty true.
}
public static void main(String args[])
{
Testawt ta = new Testawt();
}
}

Creating Frame window by extending Frame class


package testawt;

import java.awt.*;
import java.awt.event.*;

public class Testawt extends Frame


{
public Testawt()
{

Button btn=new Button("Hello World");


add(btn); //adding a new Button.
setSize(400, 500); //setting size.
setTitle("StudyTonight"); //setting title.
setLayout(new FlowLayout()); //set default layout for frame.
setVisible(true); //set frame visibilty true.

public static void main (String[] args)


{
Testawt ta = new Testawt(); //creating a frame.
}
}

Вам также может понравиться