Get JFrame title

Source code below will show how to get JFrame title.

********************************************************************
COMPLETE SOURCE CODE FOR : GetJFrameTitle.java
********************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GetJFrameTitle
{
public static void main(String[]args)
{
//Create a JFrame with title ( My first window )
JFrame frame=new JFrame("My first window");

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,500);

//Make JFrame visible
frame.setVisible(true);

//Create a message box that show JFrame title
//Method getTitle() will get JFrame title
JOptionPane.showMessageDialog(frame,"MY NAME IS : "+frame.getTitle());
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Determine when JFrame moved

Source code below will show you, how to determine when a JFrame is moved

*****************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJFrameIsMoved.java
*****************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentListener;

public class DetermineWhenAJFrameIsMoved extends JFrame
{
//Create ComponentListener that will listen when JFrame is moving
ComponentListener cl=new ComponentAdapter()
{
public void componentMoved(ComponentEvent event)
{
//Location that will show is base on top left corner of JFrame
JOptionPane.showMessageDialog(null,"New location with :\n"+"X-coordinate : "+getLocation().getX()+"\n"+"Y-coordinate : "+getLocation().getY());
}
};

public DetermineWhenAJFrameIsMoved()
{
super("TRY MOVE ME");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);

//Add ComponentListener to JFrame
addComponentListener(cl);
}

public static void main(String[]args)
{
DetermineWhenAJFrameIsMoved dwajfir=new DetermineWhenAJFrameIsMoved();
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Determine when JFrame resized

Source code below will show you, how to determine when JFrame re sized.

***********************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJFrameIsResized.java
***********************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentListener;

public class DetermineWhenAJFrameIsResized extends JFrame
{
//Create ComponentListener that will listen when JFrame is resizing
ComponentListener cl=new ComponentAdapter()
{
public void componentResized(ComponentEvent event)
{
JOptionPane.showMessageDialog(null,"New size with :\n"+"WIDTH : "+getSize().width+"\n"+"HEIGHT : "+getSize().height);
}
};

public DetermineWhenAJFrameIsResized()
{
super("TRY RESIZE ME");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);

//Add ComponentListener to JFrame
addComponentListener(cl);
}

public static void main(String[]args)
{
DetermineWhenAJFrameIsResized dwajfir=new DetermineWhenAJFrameIsResized();
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Draw rectangle in JFrame

Source code below will show how to draw rectangle in JFrame.

**********************************************************************
COMPLETE SOURCE CODE FOR : DrawRectangleInJFrame.java
**********************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawRectangleInJFrame extends JFrame
{
public DrawRectangleInJFrame()
{
//Set JFrame title
super("Draw A Rectangle In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw rectangle outline
g.drawRect(50,50,300,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill rectangle with RED
g.fillRect(50,50,300,100);
}

public static void main(String[]args)
{
DrawRectangleInJFrame dlijf=new DrawRectangleInJFrame();
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You