Showing posts with label jframe. Show all posts
Showing posts with label jframe. Show all posts

Set JFrame decoration to MetalLookAndFeel decoration



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


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

public class SetJFrameDecorationToMetalLookAndFeel
{
public static void main(String[]args)
{
//Set JFrame decoration to current look and feel decoration
//You can try to make this line as comment and
//after that try compile and execute it to see it's differential
JFrame.setDefaultLookAndFeelDecorated(true);

JButton myButton=new JButton("Click On Me");

JFrame myFrame=new JFrame("Set JFrame window decoration to current look and feel");
myFrame.setLayout(new FlowLayout());

myFrame.add(myButton);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


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

Message box appear in JFrame

Complete source code below will show you, how to make message box appear in a container.

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


import javax.swing.JOptionPane;

import javax.swing.JFrame;

public class MessageBoxAppearInJFrame
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Message box appear in JFrame )
JFrame frame=new JFrame("Message box appear in JFrame");

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

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

//Make JFrame locate at center on screen
frame.setLocationRelativeTo(null);

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


//Pop up a message box with text ( I am a message dialog ) in created JFrame
JOptionPane.showMessageDialog(frame,"I am a message dialog");
}
}


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

Set JTextArea background color to JFrame default color

Complete source code below will show you, how to set JTextArea background color same to JFrame default color.

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


import javax.swing.*;

import java.awt.Color;

public class SetJTextAreaBackgroundColorToJFrameColor
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JTextArea background color to JFrame color )
JFrame frame=new JFrame("Set JTextArea background color to JFrame color");

//Get JFrame background color
Color jframeColor=frame.getBackground();

//Create a text area with initial text ( Put your text here )
JTextArea textArea=new JTextArea("Put your text here");

//Set text area background color same to JFrame background color
textArea.setBackground(jframeColor);

//Add created text area into JFrame
frame.add(textArea);

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

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

//Make JFrame locate at center on screen
frame.setLocationRelativeTo(null);

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


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

Easiest way to make JFrame center

Complete source code below will show you, the easiest way to make JFrame get to the center of screen by only one line of code. We will use method setLocationRelativeTo(null) to make our JFrame get to the center. Remember, when you want to use this code, you must put it after you set JFrame size.

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


import javax.swing.JFrame;

public class EasiestWayToMakeJFrameCenter
{
public static void main(String[]args)
{
JFrame frame=new JFrame("Easiest way to make JFrame center");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);

//setLocationRelativeTo(null) will make JFrame get to the center
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}


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

Get default JFrame background color

Complete source code below will show you, how to get default JFrame background color and after that, it will print RED,GREEN,BLUE(RGB) value for the color.

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


import javax.swing.JFrame;

import java.awt.Color;

public class GetDefaultJFrameBackgroundColor
{
public static void main(String[]args)
{
//Create a window using JFrame
JFrame frame=new JFrame();

//Get JFrame background color using method getBackground()
Color frameBackgroundColor=frame.getBackground();

//Get RED value from color
int redValue=frameBackgroundColor.getRed();

//Get GREEN value from color
int greenValue=frameBackgroundColor.getGreen();

//Get BLUE value from color
int blueValue=frameBackgroundColor.getBlue();

//Print RGB value that we get
System.out.println("R : "+redValue);
System.out.println("G : "+greenValue);
System.out.println("B : "+blueValue);

//You can check what color that match the RGB value that we get using 'Color picker' at above
}
}


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

Draw grid on JFrame

Complete source code below will show you, how to draw grid on a JFrame.

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


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawGridOnJFrame extends JFrame
{
//Constructor
public DrawGridOnJFrame()
{
//Set JFrame title to ( Draw grid on JFrame )
setTitle("Draw grid on JFrame");

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
setSize(400,400);

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

//Make JFrame visible
setVisible(true);
}


/**
*To draw grid line on JFrame we will override it's paint method.
*We will draw grid with size 10 pixels width and 10 pixels height for each grid box
**/
public void paint(Graphics g)
{
//Override paint method in superclass
super.paint(g);

//Get current JFrame width
int frameWidth=getSize().width;

//Get current JFrame height
int frameHeight=getSize().height;

int temp=0;

//Draw vertical grid line with spacing between each line equal to 10 pixels
while(temp<frameWidth)
{
temp=temp+10;
g.drawLine(temp,0,temp,frameHeight);
}

temp=0;

//Draw horizontal grid line with spacing between each line equal to 10 pixels
while(temp<frameHeight)
{
temp=temp+10;
g.drawLine(0,temp,frameWidth,temp);
}
}

//Main method
public static void main(String[]args)
{
DrawGridOnJFrame dgojf=new DrawGridOnJFrame();
}
}


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

JFrame fullscreen

Source code below will show you, how to set JFrame to full screen follow to current computer screen

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


import javax.swing.JFrame;

import java.awt.Toolkit;

public class FullScreenJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Full Screen JFrame )
JFrame frame=new JFrame("Full Screen JFrame");

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

//Set JFrame size to full screen size follow current screen size
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height);

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


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

Get JFrame size

Source code below will create a JFrame with a button in it. You can know current JFrame size by click the button. You also can try resize the JFrame first, and after that click the button.

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


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

import java.awt.FlowLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GetJFrameSize extends JFrame
{
//Create ActionListener for listen when someone click button
ActionListener al=new ActionListener()
{
//Overwrite abstract method ( actionPerformed ) in ActionListener class
public void actionPerformed(ActionEvent event)
{
//Show message box that contain information about current JFrame width and height in pixels
JOptionPane.showMessageDialog(null,"JFrame size in pixels is : \n"+"WIDTH : "+getSize().width+"\n"+"HEIGHT : "+getSize().height);
}
};

public GetJFrameSize()
{
//Create JFrame title ( Resize me and click button )
super("Resize me and click button");

//Set JFrame layout to FlowLayout
setLayout(new FlowLayout());

//Create a button with text ( Click me to see current JFrame size )
JButton button=new JButton("Click me to see current JFrame size");

//Add ActionListener to the button
button.addActionListener(al);

//Add the button into created JFrame
add(button);

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

//Set initial JFrame size to :
//WIDTH = 500 pixels
//HEIGHT = 500 pixels
setSize(500,500);

//Make JFrame visible
setVisible(true);
}

public static void main(String[]args)
{
GetJFrameSize gjfs=new GetJFrameSize();
}
}


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

Set JFrame size

Source code below will show you, how to set JFrame size

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


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

public class SetSizeForJFrame
{
public static void main(String[]args)
{
//Receive user input for JFrame width
String jframeWidth=JOptionPane.showInputDialog(null,"Put JFrame width in pixels here");

//Receive user input for JFrame height
String jframeHeight=JOptionPane.showInputDialog(null,"Put JFrame height in pixels here");

//Create a JFrame
JFrame frame=new JFrame("Set JFrame size");

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

//Set JFrame size base on user input
frame.setSize(Integer.parseInt(jframeWidth),Integer.parseInt(jframeHeight));

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

Set JFrame title

Source code below will show how to set JFrame title

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


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

public class SetJFrameTitle
{
public static void main(String[]args)
{
//Create input dialog that will use to receive JFrame title
String jframeTitle=JOptionPane.showInputDialog(null,"Put JFrame title here");

//Create a JFrame
JFrame frame=new JFrame();

//Set title for JFrame
//Method setTitle() is use to set JFrame title
frame.setTitle(jframeTitle);

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

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

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


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

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
**********************************************************************

Draw square in JFrame

Source code below will show you, how to draw a square in a JFrame. For your information, square has 4 equal side. It mean it has 4 side with same length.

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


import javax.swing.JFrame;

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

public class DrawSquareInJFrame extends JFrame
{
public DrawSquareInJFrame()
{
//Set JFrame title
super("Draw A Square 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 square outline
g.drawRect(50,50,100,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 square with RED
g.fillRect(50,50,100,100);
}

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


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

Draw oval in JFrame

Source code below will show you, how to draw an oval in a JFrame

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


import javax.swing.JFrame;

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

public class DrawOvalInJFrame extends JFrame
{
public DrawOvalInJFrame()
{
//Set JFrame title
super("Draw An Oval 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 oval outline
g.drawOval(50,50,100,300);

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

//fill oval with RED
g.fillOval(50,50,100,300);
}

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


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

Draw circle in JFrame

Source code below will show you, how to draw a circle in a JFrame

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


import javax.swing.JFrame;

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

public class DrawCircleInJFrame extends JFrame
{
public DrawCircleInJFrame()
{
//Set JFrame title
super("Draw A Circle 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 circle outline
g.drawOval(50,50,100,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 circle with RED
g.fillOval(50,50,100,100);
}

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


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

Draw line in JFrame

Source code below will draw a line from top left corner of a JFrame until bottom right corner of the JFrame.

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


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawLineInJFrame extends JFrame
{
public DrawLineInJFrame()
{
//Set JFrame title
super("Draw A Line 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 line in JFrame from top left corner until bottom right corner
g.drawLine(0,0,getSize().width,getSize().height);
}

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


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

Draw image in JFrame

Source code below will show you, how to draw image in JFrame. Image that will be use is drawImageIntoJFrame.jpg like below


drawImageIntoJFrame.jpg


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


import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;

public class DrawImageOnJFrame extends JFrame
{
Image imageToBeDraw;
ImageIcon ii;

public DrawImageOnJFrame()
{
//set JFrame title
super("Draw Image On JFrame");

//Get image. You must change image location follow to image location in your computer.
imageToBeDraw=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\drawImageIntoJFrame.jpg");

//Create an ImageIcon object
ii=new ImageIcon(imageToBeDraw);

//set close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size follow the image size
setSize(ii.getIconWidth(),ii.getIconHeight());

//make you JFrame cannot resizable
setResizable(false);

//make JFrame visible. So we can see it.
setVisible(true);
}

public void paint(Graphics g)
{
//This will draw drawImageIntoJFrame.jpg into JFrame
g.drawImage(imageToBeDraw,0,0,this);
}

public static void main(String[]args)
{
DrawImageOnJFrame diojf=new DrawImageOnJFrame();
}
}


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

Add JPanel into JFrame

Source code below will show you, how to add a JPanel into JFrame.

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


import javax.swing.JPanel;
import javax.swing.JFrame;

public class AddJPanelIntoJFrame
{
public static void main(String[]args)
{
//Create a panel
JPanel panel=new JPanel();

//Create a window using JFrame with title ( Add JPanel into JFrame )
JFrame frame=new JFrame("Add JPanel into JFrame");

//add created panel into JFrame
frame.add(panel);

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

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


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

RELAXING NATURE VIDEO