Java Swing – Set JFrame Background Image

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);
    }

}

Java Swing – Set JPanel Background Color

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

import java.awt.Color;

public class SetJPanelBackgroundColor {

	public static void main(String[]args) {

		//Create panel using JPanel
		JPanel panel = new JPanel();

		//Create JFrame with title (Set JPanel background color)
		JFrame frame = new JFrame("Set JPanel background color");

		//Set color base on RGB
		//You can get RGB value for your color at "Color picker" at above
		//R=255
		//G=0
		//B=0
		Color color = new Color(255,0,0);

		//Set JPanel background color to color that you choose
		panel.setBackground(color);

		//Add JPanel into JFrame
		frame.add(panel);

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

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

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

}

Java Swing – Set JTextField Text Color

import javax.swing.JTextField;
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.FlowLayout;

public class SetJTextFieldTextColor {

	public static void main(String[]args) {

		//Create text field using JTextField
		JTextField textField = new JTextField(10);

		//Create JFrame with title (Set JTextField text color)
		JFrame frame = new JFrame("Set JTextField text color");

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

		//Set color base on RGB
		//You can get RGB value for your color at "Color picker" at above
		//R=255
		//G=0
		//B=0
		Color color = new Color(255,0,0);

		//Set JTextField text color to color that you choose
		textField.setForeground(color);

		//Add JTextField into JFrame
		frame.add(textField);

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

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

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

Page 1 of 212