Sunday, May 18, 2014

how to use JFrame in java?





An extended version of java.awt.Frame that adds support for the JFC/Swing component architecture. The default layout of JFrame is BorderLayout.  Unlike a Frame, a JFrame has some notion of how to respond when the user attempts to close the window. The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int).

To use JFrame in program:-

GuiWindow.java

import javax.swing.*;
class GuiWindow
{
private JButton btnLogin,btnCancl;
void go()
{
JFrame jf=new JFrame();
jf.setTitle("JFrame ex");
jf.getContentPane().setBackground(java.awt.Color.RED);
jf.setLayout(null);
btnCancl=new JButton("Cancel");
btnLogin=new JButton("Login");
btnCancl.setBounds(5,10,80,30);
btnLogin.setBounds(95,10,80,30);
jf.add(btnCancl); jf.add(btnLogin);
jf.setSize(300,200);
jf.setVisible(true);
jf.setResizable(false); 
jf.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
//JFrame.EXIT_ON_CLOSE
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); 
}

public static void main(String args[])
{
GuiWindow gw=new GuiWindow();
gw.go(); 
} 
}

Here is the Output:-