Example : A Swing GUI program in java to create & display button on frame.
import javax.swing.*;  //importing swing package
import java.awt.*;     //importing awt package
class Example
{
	Example() 
	{
		JFrame jf = new JFrame("Swing Frame");	//Creating a JFrame with name Swing Frame
		JButton btn = new JButton("Submit");//Creating a Button named Submit 
		jf.add(btn);             		//Adding submit button to the frame
		jf.setLayout(new FlowLayout());        //Setting default layout FlowLayout 
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	//To close the frame.
		jf.setSize(450, 450);            	//Making display Frame size
		jf.setVisible(true);            	//Make frame visibility true
	}
	public static void main(String[] args)
	{
		new Example();
	}
}
Example : A Swing GUI program in java to show background image in Jframe.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class BackgroundImage extends JFrame
{	
	public BackgroundImage()
	{
	  setTitle("Background Color/Image in JFrame");
	  setSize(500,400);  //Initial Jframe Size
	  setLocationRelativeTo(null);
	  setDefaultCloseOperation(EXIT_ON_CLOSE);   //Optional
	  setVisible(true);

	  setLayout(new BorderLayout());
	  setContentPane(new JLabel(new ImageIcon("C:\\New Folder\\aa.jpg")));
	  setLayout(new FlowLayout());
	
	  // For Refresh the Image, Not optional	 
	  setSize(400,400);   //After Refreshment of Image Size
	}
	public static void main(String args[])
	{
	  new BackgroundImage();
	}
}

----------  OR  -----------

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

class BackgroundImage extends JFrame
{
	public BackgroundImage()
	{
	  setTitle("Background Image in JFrame");
	  setSize(500,400);
	  setLocationRelativeTo(null);
	  setDefaultCloseOperation(EXIT_ON_CLOSE);   //Optional
	  setVisible(true);

	  setLayout(new BorderLayout());
	  JLabel background=new JLabel(new ImageIcon("C:\\aa.jpg"));
	  add(background);
	  background.setLayout(new FlowLayout());
	
	  // For Refresh the Image, Not optional rather necessary
	  setSize(400,400);   //After Refreshment of Image Size	
	}
	public static void main(String args[])
	{
	  new BackgroundImage();
	}
}

 380 total views,  1 views today

Categories: Java

0 Comments

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.