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();
	}
}Example : A Swing GUI program in Java to show a message on loading the JFrame.
import javax.swing.*;
public class SwingExample 
 {
    public static void main(String[] args) 
     {
        // Create a JFrame (window) with a message
        JFrame frame = new JFrame("Swing Example");
        // Create a JLabel
        JLabel label = new JLabel("Hello, World!");
        // Create a JButton
        JButton button = new JButton("Click to show Message");
        // Create a JPanel
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(button);
        // Add the panel to the created frame
        frame.getContentPane().add(panel);
        // Set the size of the frame
        frame.setSize(300, 200);
        // Set the default close operation
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Make the frame visible
        frame.setVisible(true);
    }
}
 
0 Comments