0% found this document useful (0 votes)
20 views

Chappter 5 Java Package

User-defined packages allow creating custom packages and classes. The document discusses how to create a package directory, define a package, and use classes from a custom package in another file. It also provides examples of AWT and Swing GUI programming in Java.

Uploaded by

abrhamashenafi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Chappter 5 Java Package

User-defined packages allow creating custom packages and classes. The document discusses how to create a package directory, define a package, and use classes from a custom package in another file. It also provides examples of AWT and Swing GUI programming in Java.

Uploaded by

abrhamashenafi3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

User-defined packages

These are the packages that are defined by the user.


First we create a directory myPackage (name should be
same as the name of the package). Then create the
MyClass inside the directory with the first statement
being the package names.

// Name of the package must be same as the directory


// under which this file is saved
package myPackage;

public class MyClass


{
public void getNames(String s)
{
System.out.println(s);
}
Now we can use the MyClass class in our program.
/* import 'MyClass' class from 'names' myPackage */
import myPackage.MyClass;
public class PrintName
{
public static void main(String args[])
{
// Initializing the String variable
// with a value
String name = "GeeksforGeeks";

// Creating an instance of class MyClass in


// the package.
MyClass obj = new MyClass();

obj.getNames(name);
}
}
Java AWT Example:

To create simple AWT example, you need a frame. There


are two ways to create a GUI using Frame in AWT.
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)

AWT Example by Inheritance


Let's see a simple example of AWT where we are inheriting
Frame class. Here, we are showing Button component on
the Frame.
// main method
public static void main(String args[]) {

// creating instance of Frame class


AWTExample1 f = new AWTExample1();

}
Java Swing tutorial is a part of Java Foundation Classes
(JFC) that is used to create window-based applications.
It is built on the top of AWT (Abstract Windowing
Toolkit) API and entirely written in java.Unlike AWT,
Java Swing provides platform-independent and lightweight
components.
The javax.swing package provides classes for java swing
API such as JButton, JTextField, JTextArea, JRadioButton,
JCheckbox, JMenu, JColorChooser etc.
No. Java AWT Java Swing

1) AWT components are platform-dependent. Java swing components are


platform-independent.

2) AWT components are heavyweight. Swing components are lightweight.

3) AWT doesn't support pluggable look and feel. Swing supports pluggable look and
feel.

4) AWT provides less components than Swing. Swing provides more powerful
components such as tables, lists,
scrollpanes, colorchooser,
tabbedpane etc.

5) AWT doesn't follows MVC(Model View Swing follows MVC.


Controller) where model represents data, view
represents presentation and controller acts as
an interface between model and view.
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


b.setBounds(130,100,100, 40);//x axis, y axis, width, height

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
For more ...........................Read..........More
Edit menu code for Notepad
JButton class
OpenDialog Box
JRadioButton class
Notepad
JTextArea class
Puzzle Game
JComboBox class
Pic Puzzle Game
JTable class
Tic Tac Toe Game
JColorChooser class
BorderLayout
JProgressBar class
GridLayout
JSlider class
FlowLayout
Digital Watch
CardLayout
Graphics in swing
Displaying image

You might also like