Saturday, August 24, 2013

JFrame

JFrame is top-level window in Swing package. It is the extended version of Frame in Awt package. Unlike Frame, JFrame has its child container called contentPane in which all non-menu components are placed in. By using JFrame, you can write only a line of code to close the window created by the JFrame by invoking the setDefaultCloseOperation(int operation) method.

JFrame constructors:
-JFrame()
creates a new invisible window.
-Frame(String title)
creates anew invisible window with specified title.

Its useful methods:
-getContentPane()
returns the contentPane object of the window.
-setDefaultCloseOperation(int operation)
sets the defaut close operation of the window.
-setJMenuBar(JMenuBar menu)
sets the menu bar to the window.
-setIconImage(Image img)
sets the image icon of the window.

Example:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;


class Show extends JFrame{
Show(String title){
setTitle(title);
setSize(400,300);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image img=Toolkit.getDefaultToolkit().getImage("d:/icon.png");
setIconImage(img);
Container contpane=getContentPane();
//code add components to the content pane
setVisible(true);
}
}

public class JFrameSwing {
public static void main(String[] args){
new Show("JFrame");
}
}

JFrame Swing

No comments:

Post a Comment