Showing posts with label put. Show all posts
Showing posts with label put. Show all posts

Put image on JLabel

Complete source code below will show you, how to put image on JLabel.



JLabelImage.jpg


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


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

import java.awt.Toolkit;

public class JLabelImage
{
public static void main(String[]args)
{
//Create image icon from your image file
//You must change location for your image file
//My image file is locate at : C:\Documents and Settings\evergreen\Desktop
ImageIcon ii=new ImageIcon("C:\\Documents and Settings\\evergreen\\Desktop\\JLabelImage.jpg");

//Create a label with your image file
JLabel label=new JLabel(ii);

//Create a window using JFrame with title ( Put image on JLabel )
JFrame frame=new JFrame("Put image on JLabel");

//Add created label into JFrame
frame.add(label);

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

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

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


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

Put JRadioButton into group

Complete source code below will show you, how to create group for JRadioButton.

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


import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;

import java.awt.GridLayout;

public class PutJRadioButtonIntoGroup
{
public static void main(String[]args)
{
//Create five radio button that will be put into a group
JRadioButton firstRadioButton=new JRadioButton("First radio button");
JRadioButton secondRadioButton=new JRadioButton("Second radio button");
JRadioButton thirdRadioButton=new JRadioButton("Third radio button");
JRadioButton fourthRadioButton=new JRadioButton("Fourth radio button");
JRadioButton fifthRadioButton=new JRadioButton("Fifth radio button");

//Create a button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

//Add all radio button into created group
bg.add(firstRadioButton);
bg.add(secondRadioButton);
bg.add(thirdRadioButton);
bg.add(fourthRadioButton);
bg.add(fifthRadioButton);

//Create a window using JFrame with title ( Put JRadioButton into group )
JFrame frame=new JFrame("Put JRadioButton into group");

//Set JFrame layout to grid layout
frame.setLayout(new GridLayout(5,1));

//Add all created button into group
frame.add(firstRadioButton);
frame.add(secondRadioButton);
frame.add(thirdRadioButton);
frame.add(fourthRadioButton);
frame.add(fifthRadioButton);

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

//Set JFrame size
frame.setSize(300,150);

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


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

Put JButton into JFrame

Source code below will show you, how to add a JButton into JFrame. In java, we will use JButton to create button.

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


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

public class AddJButtonIntoJFrame
{
public static void main(String[]args)
{
//Create a JButton with text CLICK ME
JButton button=new JButton("CLICK ME");

//Create a JFrame that will be a window
//It's title is, Put JButton into JFrame
JFrame frame=new JFrame("Put JButton into JFrame");

//Add JButton into JFrame
frame.add(button);

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

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

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


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

Put JLabel into JFrame

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

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


import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.SwingConstants;

public class AddJLabelIntoJFrame
{
public static void main(String[]args)
{
//Create a JLabel with text HI that we want to add into JFrame
//SwingConstants.CENTER will make HI locate at the center of JLabel
JLabel label=new JLabel("HI",SwingConstants.CENTER);

//Create a JFrame that will be a window
//Window title is, Put JLabel into JFrame
JFrame frame=new JFrame("Put JLabel into JFrame");

//Add JLabel into JFrame
frame.add(label);

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

//Set size for JFrame in pixels
//Width=400 pixels
//Height=100 pixels
frame.setSize(400,100);

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


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

Put component into JWindow

Source code below will show you how to add a component into JWindow. In this case we will use button as component that we want to add into JWindow.

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


import javax.swing.JWindow;
import javax.swing.JButton;

import java.awt.BorderLayout;

import java.awt.Container;

public class PutComponentIntoJWindow
{
public static void main(String[]args)
{
//Create a button.
//Button will be a component that we will add into JWindow
JButton button=new JButton("MY BUTTON");

//Create a JWindow
JWindow window=new JWindow();

//When you want to add component into JWindow you must get it Container first
//After that you can add component into JWindow using it.
Container contain=window.getContentPane();

//Set container layout to BorderLayout
contain.setLayout(new BorderLayout());

//Add button into JWindow
contain.add(button,BorderLayout.CENTER);

//Set JWindow size
window.setSize(400,400);

//Make JWindow visible
window.setVisible(true);
}
}


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

Note : Thanks for someone that show me some light. It is wrong when we want to add component into JWindow like example(For example : window is variable for JWindow, it wrong when we want to add component into JWindow...we do like this window.add(button)).We must get it's container first, and after that we will add component into JWindow using container that we get.You can see source code above after i repaired it.

Put image as jframe background

Put image as jframe background


bg.jpg



Source code below will make a jframe with bg.jpg as it's background image.

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


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

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

public class PutImageOnJFrame extends JPanel
{
public PutImageOnJFrame()
{
setOpaque(false);
setLayout(new FlowLayout());
}

public static void main(String[]args)
{
JFrame myFrame=new JFrame("Put Image");
JButton button1=new JButton("Sample 1");
JButton button2=new JButton("Sample 2");
PutImageOnJFrame c=new PutImageOnJFrame();
c.add(button1);
c.add(button2);
myFrame.add(c);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setVisible(true);
}

public void paint(Graphics g)
{ //IT DEPEND ON YOUR PICTURE AND PUT IT'S LOCATION IN
Image a=Toolkit.getDefaultToolkit().getImage("E:\\_JAVA_\\---SOURCE CODE---\\bg.jpg");
g.drawImage(a,0,0,getSize().width,getSize().height,this);
super.paint(g);
}

//THANKS FOR WATCHING
}


********************************************************
JUST COMPILE AND EXECUTE IT.
Note : Before that, you must know your image location. Copy and paste your image location into Image a=Toolkit.getDefaultToolkit().getImage(PASTE_IN_THIS);
********************************************************

RELAXING NATURE VIDEO