JWindow

javax.swing
Class JWindow

java.lang.Object
 java.awt.Component
    java.awt.Container
        java.awt.Window
            javax.swing.JWindow
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, RootPaneContainer, Serializable

JWindow is like a JFrame, but without borders or menu bar. A generic term for both a JFrame and JDialog. it can accept keystrokes.

You don’t use add( child ); you use getContentPane().add( child ). This is especially infuriating since the compiler cannot catch the error. Similarly instead of setLayout( layoutManager ) you use getContentPane().setLayout( layoutManager ).

import java.awt.*;
//Create a Draggable Window

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

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

JWindow

Read about JFrame,

https://smoothknowledge.com/2017/10/31/jframe/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.