Supported Libraries in Java:
Awt (Abstract Window Toolkit)
e.g., import java.awt.*;
Components in awt are:
Button.
Frame.
TextField.
Label.
List.
And many more..
Basically these components are called as “Heavy weight components” because they pick the component (button, label etc.) from an underlying operating system environment.Swing (Starts with ‘J’ Letter)
e.g., import javax.swing.*;
Components in swing are:
JButton.
JFrame.
JTextField.
JLabel.
JList.
And many more..
These components are called as “Light Weight components” because they are java’s own.GUI Creation Steps:
Imports required packages.
import java.awt.*;
import javax.swing.*;
JFrame myFrame = new JFrame();
Get the components area of top level container.
Container con = null;
con = myFrame.getContentPane();
con.setLayout(new FlowLayout());//Later we discuss about Layout Managers
JButton helloButton = new JButton("Hello");
con.add(helloButton);
myFrame.setSize(250,100);
myFrame.setVisible(true);
Two approaches generally adopt by the programmer/developer.
Approach One through Composition (Through creating JFrame object):
public class GUIComposition {
JFrame myFrame = null;
Container con = null;
public GUIComposition() {
myFrame = new JFrame();
con = myFrame.getContentPane();
……….
myFrame.setSize(250,100);
myFrame.setVisible(true);
}
}public class GUIInheritance extends JFrame {
Container con = null;
public GUIInheritance() {
con = getContentPane();
……….
setSize(250,100);
setVisible(true);
}}
Layout Managers:
The layout managers automatically place components on the screen, you don’t have to explicitly define the x-axis and y-axis coordinates of components to place it on container.
Java gives us many layout managers, following are some commonly used:
FlowLayout()
GridLayout()
BorderLayout()
FlowLayout():
Places components in a line as long as they fit, then start the next line.
Uses “best judgment” in spacing components. Centers by default.
Let’s each component assume its natural (preferred) size.
Often used for placing buttons on panels.
JFrame myFrame = new JFrame();
con = myFrame.getContentPane();
con.setLayout (new FlowLayout() );
JButton b1 = new JButton("Next Slide");
JButton b2 = new JButton("Previous Slide");
JButton b3 = new JButton("Back to Start");
JButton b4 = new JButton("Last Slide");
JButton b5 = new JButton("Exit");
con.add(b1);
con.add(b2);
con.add(b3);
con.add(b4);
con.add(b5);GridLayout():
Splits the panel into a grid with given number of rows and columns.
Places components into the grid cells.
Forces the size of each component to occupy the whole cell.
Allows additional spacing between cells.
JFrame myFrame = new JFrame();
con = myFrame.getContentPane(); con.setLayout (new GridLayout(3 , 2) ); //three rows, two columns
JButton b1 = new JButton("Next Slide");
JButton b2 = new JButton("Previous Slide");
JButton b3 = new JButton("Back to Start");
JButton b4 = new JButton("Last Slide");
JButton b5 = new JButton("Exit");
con.add(b1);
con.add(b2);
con.add(b3);
con.add(b4);
con.add(b5);BorderLayout():
Divides the area into five regions.
Adds a component to the specified region
Forces the size of each component to occupy the whole region.
con = myFrame.getContentPane();
con.setLayout (new BorderLayout() );
JButton b1 = new JButton("Next Slide");
JButton b2 = new JButton("Previous Slide");
JButton b3 = new JButton("Back to Start");
JButton b4 = new JButton("Last Slide");
JButton b5 = new JButton("Exit");
con.add(b1, BorderLayout.NORTH);
con.add(b2, BorderLayout.SOUTH);
con.add(b3, BorderLayout.EAST);
con.add(b4, BorderLayout.WEST);
con.add(b5, BorderLayout.CENTER);
0 comments:
Post a Comment