Friday, July 16, 2010

JWindow

JWindow
Java Swing Tutorial Explaining the JWindow Component. JWindow is
Swing’s version of Window and is descended directly from that class. Like
Window, it uses BorderLayout by default. Almost all Swing components are
lightweight except JApplet, JFrame, JDialog, and JWindow.

JWindow Source Code


public class JWindowDemo extends JWindow {

 private int X = 0;
 private int Y = 0;
 public JWindowDemo() {
  setBounds(60, 60, 100, 100);
  addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0); // An Exit Listener
   }
  });
  // Print (X,Y) coordinates on Mouse Click
  addMouseListener(new MouseAdapter() {

   public void mousePressed(MouseEvent e) {
    X = e.getX();
    Y = e.getY();
    System.out.println("The (X,Y) coordinate of window is ("
      + X + "," + Y + ")");
   }
  });
  addMouseMotionListener(new MouseMotionAdapter() {

   public void mouseDragged(MouseEvent e) {
    setLocation(getLocation().x + (e.getX() - X),
      getLocation().y + (e.getY() - Y));
   }
  });
  setVisible(true);
 }
 public static void main(String[] args) {
  new JWindowDemo();
 }
}
Output


Java JWindow Hierarchy

javax.swing


Class JWindow 


java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Window
All Implemented Interfaces:

Accessible, ImageObserver, MenuContainer, Serializable

Direct Known Subclasses: 

BasicToolBarUI.DragWindow, Dialog, Frame, JWindow

JWindow Constructor


Window(Frame owner)


Constructs a new invisible window with the specified Frame as its owner.

Window(Window owner)


Constructs a new invisible window with the specified Window as its owner.

Window(Window owner, GraphicsConfiguration gc)


Constructs a new invisible window with the specified window as its owner and a
GraphicsConfiguration of a screen device.

JSwings - JFrames

JFrame
Java Swing Tutorial Explaining the JFrame class. The components added to the
frame are referred to as its contents; these are managed by the contentPane. To
add a component to a JFrame, we must use its contentPane instead.JFrame is a
Window with border, title and buttons. When JFrame is set visible, an event
dispatching thread is started. JFrame objects store several objects including a
Container object known as the content pane. To add a component to a JFrame, add
it to the content pane.

JFrame Features
It’s a window with title, border, (optional) menu bar and user-specified
components.

It can be moved, resized, iconified.

It is not a subclass of JComponent.

Delegates responsibility of managing user-specified components to a content
pane, an instance of JPanel.

Centering JFrame’s
By default, a Jframe is displayed in the upper-left corner of the screen. To
display a frame

at a specified location, you can use the setLocation(x, y) method in the JFrame
class. This

method places the upper-left corner of a frame at location (x, y).
The Swing API keeps improving with abstractions such as the
setDefaultCloseOperation method

for the JFrame

Crating a JFrame Window
Step 1: Construct an object of the JFrame class.
Step 2: Set the size of the Jframe.
Step 3: Set the title of the Jframe to appear in the title bar (title bar
will be blank if no title is set).


Step 4: Set the default close operation. When the user clicks the close
button, the program stops running.
Step 5: Make the Jframe visible.

How to position JFrame on Screen?
frame.setLocationRelativeTo( null );


JFrame Source Code


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFrameDemo {

 public static void main(String s[]) {
  JFrame frame = new JFrame("JFrame Source Demo");
  // Add a window listner for close button
  frame.addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  // This is an empty content area in the frame
  JLabel jlbempty = new JLabel("");
  jlbempty.setPreferredSize(new Dimension(175, 100));
  frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
  frame.pack();
  frame.setVisible(true);
 }
}
Output

Java JFrame Hierarchy

javax.swing


Class JFrame


java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Window

java.awt.Frame

javax.swing.JFrame

All Implemented Interfaces:

Accessible, ImageObserver, MenuContainer, RootPaneContainer, Serializable,
WindowConstants

JFrame Constructor


JFrame()


Constructs a new frame that is initially invisible.

JFrame(GraphicsConfiguration gc)


Creates a Frame in the specified GraphicsConfiguration of a screen device and a
blank title.

JFrame(String title)


Creates a new, initially invisible Frame with the specified title.

JFrame(String title, GraphicsConfiguration gc)


Creates a JFrame with the specified title and the specified
GraphicsConfiguration of a screen device.