Java simple counter

Source below will show you, how to create simple counter in java.

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


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Font;

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

public class JavaSimpleCounter extends JLabel implements ActionListener
{
JButton buttonReset=new JButton("RESET");
JButton buttonCount=new JButton("CLICK HERE");
int countValue=0;
JFrame frame;
Font fontForNumber=new Font("Tahoma",Font.BOLD,80);

public JavaSimpleCounter()
{
frame=new JFrame("JAVA SIMPLE COUNTER");
frame.setLayout(new BorderLayout());
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,2));
buttonCount.addActionListener(this);
buttonReset.addActionListener(this);
panel.add(buttonCount);
panel.add(buttonReset);
frame.add(this,BorderLayout.CENTER);
frame.add(panel,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,200);
frame.setVisible(true);
}

public void paint(Graphics g)
{
g.setFont(fontForNumber);
g.setColor(Color.BLACK);
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.WHITE);
g.drawString(Integer.toString(countValue),10,80);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==buttonCount)
{
countValue++;
frame.repaint();
}

else if(event.getSource()==buttonReset)
{
Graphics panelGraphics=getGraphics();
panelGraphics.setFont(fontForNumber);
panelGraphics.setColor(Color.BLACK);
panelGraphics.fillRect(0,0,getSize().width,getSize().height);
panelGraphics.setColor(Color.RED);
panelGraphics.drawString(Integer.toString(0),10,80);
countValue=0;
frame.repaint();
}
}

public static void main(String[]args)
{
JavaSimpleCounter jsc=new JavaSimpleCounter();
}
}


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

Remove component from JFrame during runtime

Source code below will show you, how to remove component from a JFrame during program runtime. Component that will be use in source code below is a group of JButton.

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


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

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Component;

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

public class RemoveComponentOnJFrameAtRuntime extends JFrame implements ActionListener
{
JPanel panel;

public RemoveComponentOnJFrameAtRuntime()
{
super("Remove component on JFrame during runtime");
setLayout(new BorderLayout());

panel=new JPanel();
panel.setLayout(new FlowLayout());

JButton button1=new JButton("BUTTON 1");
JButton button2=new JButton("BUTTON 2");
JButton button3=new JButton("BUTTON 3");
JButton button4=new JButton("BUTTON 4");
JButton button5=new JButton("BUTTON 5");

panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);

add(panel,BorderLayout.CENTER);

JButton removeButton=new JButton("Click here to remove button");
removeButton.addActionListener(this);

add(removeButton,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
Component[]storeAllButtonInPanel=panel.getComponents();

if(storeAllButtonInPanel.length!=0)
{
panel.remove(storeAllButtonInPanel.length-1);
panel.revalidate();
validate();
repaint();
}

else
{
JOptionPane.showMessageDialog(this,"No button to remove");
}
}

public static void main(String[]args)
{
RemoveComponentOnJFrameAtRuntime rcojfat=new RemoveComponentOnJFrameAtRuntime();
}
}


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

Java default look and feel

Default look and feel is Metal look and feel. Below is an image that show how Metal look and feel looks like.

Add component on JFrame during runtime

Source code below will show you how to add a component into a JFrame during program runtime. Component that will be use in this program, is JButton.

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


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

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

import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class AddComponentOnJFrameAtRuntime extends JFrame implements ActionListener
{
JPanel panel;

public AddComponentOnJFrameAtRuntime()
{
super("Add component on JFrame at runtime");
setLayout(new BorderLayout());
panel=new JPanel();
panel.setLayout(new FlowLayout());
add(panel,BorderLayout.CENTER);
JButton button=new JButton("CLICK HERE");
add(button,BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
panel.add(new JButton("Button"));
panel.revalidate();
validate();
}

public static void main(String[]args)
{
AddComponentOnJFrameAtRuntime acojfar=new AddComponentOnJFrameAtRuntime();
}
}


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

Close JFrame when mouse click release in JFrame

Source code below will create a JFrame. After that, click in the JFrame, hold for a few seconds, and after that release your mouse click. The JFrame that created in this program will be close and this application will terminate.

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


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseReleased
{
public static void main(String[]args)
{
//Create mouse listener that will listen when mouse released in JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseReleased(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK )
JFrame frame=new JFrame("CLICK ON ME,HOLD FOR A FEW SECOND,RELEASE YOUR CLICK");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

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

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

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


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

Close JFrame when cursor exit from JFrame

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


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameWhenMouseExit
{
public static void main(String[]args)
{
//Create mouse listener that will listen when cursor exit JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseExited(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY )
JFrame frame=new JFrame("GET YOUR CURSOR INTO JFRAME, AFTER THAT MOVE IT AWAY");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

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

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

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


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

Close JFrame using mouse hover

Source code below will show you, how to create JFrame that can close, when you hover your cursor on it.

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


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseHover
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone hover on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseEntered(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( HOVER ON THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("HOVER ON THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to 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
**********************************************************************

Close JFrame using mouse click

Source code below will show you, how to close JFrame when click on it.

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


import javax.swing.JFrame;

import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CloseJFrameUsingMouseClick
{
public static void main(String[]args)
{
//Create mouse listener that will listen when someone click on JFrame
MouseListener ml=new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
//Put JFrame close code here
System.exit(0);
}
};

//Create JFrame with title ( CLICK IN THE BOX BELOW !!!!!!!!! )
JFrame frame=new JFrame("CLICK IN THE BOX BELOW !!!!!!!!!");

//Add mouse listener to JFrame
frame.addMouseListener(ml);

//Set default close operation to 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
*****************************************************************************

Close JFrame using keyboard

Source code below will show you how to close JFrame using keyboard press. In this source code we will use "Esc" key. So when someone press "Esc" key, and at the same time JFrame focused, the JFrame will close and this program will exit.

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


import javax.swing.JFrame;

import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class CloseJFrameUsingKeyboard
{
public static void main(String[]args)
{
//Create a KeyListener that can listen when someone press Esc key on keyboard
//You can change for what key that you want, by change value at:
//VK_ESCAPE
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
{
//If someone click Esc key, this program will exit
if(evt.getKeyCode()==KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
};

//Create a JFrame with title ( CLOSE JFRAME USING KEYBOARD )
JFrame frame=new JFrame("CLOSE JFRAME USING KEYBOARD");

//add Key Listener to JFrame
frame.addKeyListener(kl);

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

Get all JFrame in program

Source code below will show you how to get all JFrame that contains in a program

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


import javax.swing.JFrame;

import java.awt.Frame;

public class GetAllJFrameInAnApplication
{
public static void main(String[]args)
{
//Create JFrame with title ( HAHAHAHA )
JFrame frame1=new JFrame("HAHAHAHA");

//Create JFrame with title ( HEHEHEHE )
JFrame frame2=new JFrame("HEHEHEHE");

//Create JFrame with title ( HUHUHUHU )
JFrame frame3=new JFrame("HUHUHUHU");

//Create JFrame with title ( HIHIHIHI )
JFrame frame4=new JFrame("HIHIHIHI");

//Create JFrame with title ( ZZZZZZZZ )
JFrame frame5=new JFrame("ZZZZZZZZ");

//Create array from type Frame.
//Store all JFrame into this array.
Frame[]allJFrame=Frame.getFrames();

//Print each JFrame title in this application
for(int i=0; i<allJFrame.length; i++)
{
System.out.println("JFrame "+(i+1)+" :"+allJFrame[i].getTitle());
}
}
}


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

RELAXING NATURE VIDEO