0% found this document useful (0 votes)
65 views57 pages

Java (Full Course)

The document contains code snippets demonstrating various Java concepts such as inheritance, polymorphism, abstraction, and encapsulation. It includes examples of using abstract classes, interfaces, static and non-static methods, overriding methods, and copying objects. The code snippets show how to work with files, exceptions, audio playback, and dynamic input to demonstrate polymorphic behavior.

Uploaded by

tayzarlwintun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views57 pages

Java (Full Course)

The document contains code snippets demonstrating various Java concepts such as inheritance, polymorphism, abstraction, and encapsulation. It includes examples of using abstract classes, interfaces, static and non-static methods, overriding methods, and copying objects. The code snippets show how to work with files, exceptions, audio playback, and dynamic input to demonstrate polymorphic behavior.

Uploaded by

tayzarlwintun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Table of Contents

Java (Full course)................................................................................................................................................ 1

GUI................................................................................................................................................................... 16

Auston java....................................................................................................................................................... 17

Java (Full course)


48. Audio

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public static void main(String[] args) throws UnsupportedAudioFileException,


IOException, LineUnavailableException {

Scanner scanner = new Scanner(System.in);


File file = new File("file_example_WAV_2MG.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);

String response = "";


while (!response.equals("Q")){
System.out.println("P = play, S = stop, R = reset, Q = quit");
System.out.print("Enter your chocie: ");
response = scanner.next();
response = response.toUpperCase();
switch (response){
case ("P"):
clip.start();
break;
case ("S"):
clip.stop();
break;
case ("R"):
clip.setMicrosecondPosition(0);
break;
case ("Q"):
clip.close();
break;
default:
System.out.println("Not a vaild response");
break;

}
System.out.println("Byeee!");

47. FileReader

try {
FileReader fileReader = new FileReader("Paper.txt");
int data = fileReader.read();
while (data != -1){
System.out.print((char)data);
data = fileReader.read();
}
fileReader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

46. FileWriter

try {
FileWriter fileWriter = new FileWriter("Paper.txt");
fileWriter.write("Thii is heading\n this is subheading");
fileWriter.append("\n(A paper by Tayzar)");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}

45. File class

File file = new File("/Users/macbook/Downloads/Secerct_file.txt");


if (file.exists()){
System.out.println("This file exist");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.isFile());
file.delete();
}
else {
System.out.println("This file does not exist");
}

44. Exception
Scanner scanner = new Scanner(System.in);

try{
System.out.print("Enter a whole number to divide: ");
int x = scanner.nextInt();

System.out.print("Enter a whole number to divide by: ");


int y = scanner.nextInt();

double z = x/y;
System.out.println("The answer is " +z);
}
catch (ArithmeticException e){
System.out.println("you can't divde a zero!");
}
catch (InputMismatchException e){
System.out.println("PLEASE Enter a whole number!");
}
catch (Exception e){
System.out.println("Someting went wrong");
}
finally {
scanner.close();
}
43. Dynamic Polymorphism

Animal animal;
Scanner scanner = new Scanner(System.in);
System.out.println("What animal you want?");
System.out.print("Press 1.Dog or 2.Cat: ");
int choice = scanner.nextInt();

if (choice == 1){
animal = new Dog();
animal.speak();
}
else if (choice == 2){
animal = new Cat();
animal.speak();
}
else {
animal = new Animal();
animal.speak();
}
public class Animal {

public void speak(){


System.out.println("Animal goes *brrrr*");
}
}
public class Dog extends Animal{

@Override
public void speak(){
System.out.println("Dog goes *bark*");
}
}
public class Cat extends Animal{

@Override
public void speak(){
System.out.println("Cat goes *Meow*");
}
}
42. polymorphish

Car car = new Car();


Bicycles bicycles = new Bicycles();
Boat boat = new Boat();

Vehicles[] racers = {car, bicycles, boat};

for (Vehicles x : racers){


x.go();
}

public class Vehicles {

void go(){

}
}
public class Car extends Vehicles{
@Override
void go(){
System.out.println("This car is moving");
}
}

public class Boat extends Vehicles{


@Override
void go(){
System.out.println("This boat is moving");
}
}

public class Bicycles extends Vehicles{


@Override
void go(){
System.out.println("This bicycle is moving");
}
}

41. intrfaces
Fish fish = new Fish();
fish.flee();
fish.hunt();

Hawl hawl = new Hawl();


hawl.hunt();

Rabbit rabbit = new Rabbit();


rabbit.flee();

public class Fish implements Prey,Predator{


@Override
public void hunt() {
System.out.println("This fish it hunting the smaller fish");
}

@Override
public void flee() {

System.out.println("This fish is fleeing form larger fish");

}
}

public class Hawl implements Predator{

@Override
public void hunt() {
System.out.println("*It is hunting*");
}
}
public class Rabbit implements Prey {
@Override
public void flee() {
System.out.println("*this is fleeing*");
}
}

public interface Prey {

void flee();
}
public interface Predator {
void hunt();
}

40. copy objects

Car car1 = new Car("BLue", "Prototype", 123);


// Car car2 = new Car("Pink", "Real", 12234);
//car2.copy(car1);

Car car2 = new Car(car1);

System.out.println(car1);
System.out.println(car2);

System.out.println(car1.getMake());
System.out.println(car1.getMake());
System.out.println(car1.getYear());

System.out.println(car2.getMake());
System.out.println(car2.getMake());
System.out.println(car2.getYear());

public class Car {

private String make;


private String model;
private int year;

Car(String make, String model, int year){


this.setMake(make);
this.setModel(model);
this.setYear(year);
}
Car(Car x){
this.copy(x);

}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public int getYear(){
return year;
}

public void setMake(String make){


this.make = make;
}
public void setModel(String model){
this.model = model;
}
public void setYear(int year){
this.year = year;
}

public void copy(Car x){


this.setMake(x.getMake());
this.setModel(x.getModel());
this.setYear(x.getYear());
}
}

39. Encapsulation

Car car = new Car("BLue", "Prototype", 123);


car.setYear(2022);
System.out.println(car.getMake());
System.out.println(car.getMake());
System.out.println(car.getYear());

public class Car {

private String make;


private String model;
private int year;

Car(String make, String model, int year){


this.setMake(make);
this.setModel(model);
this.setYear(year);
}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public int getYear(){
return year;
}

public void setMake(String make){


this.make = make;
}
public void setModel(String model){
this.model = model;
}
public void setYear(int year){
this.year = year;
}
}
37.abstraction

Car2 car2 = new Car2();


car2.go();

public abstract class Vehicle {

abstract void go();


}

public class Car2 extends Vehicle{

@Override
void go(){
System.out.println("This car is moving");
}

36. Super keywords

Hero hero =new Hero("Tayzar", 23, "Everything");


System.out.println(hero.toString());

public class Person {


String name;
int age;

Person(String name, int age){


this.name = name;
this.age = age;
}
public String toString(){
return this.name + "\n" + this.age + "\n";
}
}

public class Hero extends Person{

String power;
Hero(String name, int age, String power){
super(name,age);
this.power = power;
}
public String toString() {
return super.toString() + this.power + "\n";
}
}

35. Methods overriding

Animal animal = new Animal();


animal.speak();
dog dog = new dog();
dog.speak();

public class Animal {

void speak(){
System.out.println("Animal speaks");
}

}
public class dog extends Animal{
@Override
void speak(){
System.out.println("The dogs *bark*");
}

34. Inheritant

Bicycle bicycle = new Bicycle();


bicycle.go();
Car2 car = new Car2();
car.stop();

System.out.println(car.doors);
System.out.println(bicycle.pedals);

public class Vehicle {

double speed;

void go(){
System.out.println("This vehicle is moving");
}
void stop(){
System.out.println("This vehicle is stopped");

}
}
public class Car2 extends Vehicle{

int wheels = 4;
int doors = 4;
}
public class Bicycle extends Vehicle{

int wheels = 2;
int pedals = 2;
}
33. Static keywords

Friends friend1 = new Friends("Java");


Friends friend2 = new Friends("HTLM");
Friends friend3 = new Friends("C");
Friends friend4 = new Friends("C++");

// System.out.println(Friends.numbersOfFriends);
Friends.displaysFriends();

public class Friends {


String name;
static int numbersOfFriends;

Friends(String name){
this.name = name;
numbersOfFriends++;

}
static void displaysFriends(){
System.out.println("You have "+numbersOfFriends+ " friends");
}
}

32. Objects passing

Garage garage = new Garage();


Car1 car1 = new Car1("BMW");
Car1 car2 = new Car1("Tesla");

garage.park(car1);
garage.park(car2);

public class Garage {

void park (Car1 abc){


System.out.println(abc.name +" is parked in the garage");
}
}
public class Car1 {
String name;
Car1(String name){
this.name = name;
}
}

31. Arrays of objects

// Food[] refrigerator = new Food[3];

Food food1 = new Food("Pizza");


Food food2 = new Food("humbager");
Food food3 = new Food("hotdog");

Food[] refrigerator = {food1, food2, food3};


// refrigerator[0] = food1;
// refrigerator[1] = food2;
//refrigerator[2] = food3;

System.out.println(refrigerator[0].name);
System.out.println(refrigerator[1].name);
System.out.println(refrigerator[2].name);

// System.out.println(food1.name);

public class Food {


String name;
Food(String name){
this.name = name;
}
}

30. toString methods

car car = new car();

// System.out.println(car.toString());
System.out.println(car);
public class car {

String make = "England";


String model = "Tokyo";
int year = 2021;
String color = "Black";
double price = 500000.00;

public String toString(){

// String myString = make+"\n"+model+"\n"+year+"\n"+color+"\n"+price;


//return myString; or
return make+"\n"+model+"\n"+year+"\n"+color+"\n"+price;
}

29. overloaded Constructors

Pizza pizza = new Pizza("Thicc curst", "Tomato", "Golden Egg");

System.out.println("Here are the ingredients of your pizza: ");


System.out.println(pizza.bread);
System.out.println(pizza.cheese);
System.out.println(pizza.saucse);
System.out.println(pizza.topping);

public class Pizza {

String bread;
String saucse;
String cheese;
String topping;

Pizza(String bread, String saucse){

this.bread = bread;
this.saucse = saucse;
}
Pizza(String bread, String saucse, String cheese){

this.bread = bread;
this.saucse = saucse;
this.cheese = cheese;
}
Pizza(String bread, String saucse, String cheese, String topping){

this.bread = bread;
this.saucse = saucse;
this.cheese = cheese;
this.topping = topping;
}
}

28. variable scope

DiceRoller diceRoller = new DiceRoller();

public class DiceRoller {

// [Global]
Random random;
int number;
DiceRoller(){
random = new Random();
number = 0;
roll();

}
void roll(){
number = random.nextInt(6)+1;
System.out.println(number);
}
}
/*
// [Local]
DiceRoller(){
Random random = new Random();
int number = 0;
roll(random, number);

}
void roll(Random random, int number){
number = random.nextInt(6)+1;
System.out.println(number);
}
*/

/*
27. Constructors
Human human1 = new Human("Jhon", 12, 23.4);
Human human2 = new Human("Hagai", 34, 234.4);
System.out.println(human1.age);
System.out.println(human2.age);
human1.eat();
human2.drink();

public class Human {

String name;
int age;
double weight;

Human(String name, int age, double weight){


this.name = name;
this.age = age;
this.weight = weight;
}

void eat(){
System.out.println(this.name +" is eating");
}
void drink(){
System.out.println(this.name+" is drinking *brup*");
}

26. objects(OOP)

car myCar = new car();


System.out.println(myCar.color);

myCar.drive();
myCar.brake();

public class car {

String make = "England";


String model = "Tokyo";
int year = 2021;
String color = "Black";
double price = 500000.00;

void drive (){


System.out.println("You drive the car");
}
void brake(){
System.out.println("You steps on the brake");
}

25. final Keywords

final double PI = 3.142;


System.out.println(PI);

24. printf

int age = 12;


char symbol = '!';
double gpa = 2.3;
String name = "Tayzar";
boolean Male = true;

//[Coversion Characters]
// System.out.printf("%d %c %f %s %b", age, symbol, gpa, name, Male);

//[Width]
//System.out.printf("Hello %10s", name);

//[Precision]
//System.out.printf("You have %.2f", gpa);

//[flag]
System.out.printf("You have %,f", gpa);
//10f -10f -f +f 020f

23. Overloaded Methods

public class Main {


public static void main(String args[]){

add(1, 2);
add(1,2,3);
add(1,2,3,4);

}
static int add(int x, int y){
int z = x + y;
System.out.println("This is the overloaded method #1");
System.out.println(z);
return z;
}
static int add(int x, int y, int a){
int z = x + y + a;
System.out.println("This is the overloaded method #2");
System.out.println(z);
return z;
}
static int add(int x, int y, int a, int b){
int z = x + y + a + b;
System.out.println("This is the overloaded method #3");
System.out.println(z);
return z;
}
static double add(double x, double y){
double z = x + y;
System.out.println("This is the overloaded method #4");
System.out.println(z);
return z;
}
static double add(double x, double y, double a){
double z = x + y + a;
System.out.println("This is the overloaded method #5");
System.out.println(z);
return z;
}
static double add(double x, double y, double a, double b){
double z = x + y + a + b;
System.out.println("This is the overloaded method #6");
System.out.println(z);
return z;
}
}

22. Methods

hello("Power Range", 12);


}

static void hello(String name, int age){


System.out.println("Hello "+name + ". You are " +age);
}

public class Main {

public static void main(String args[]) {

add(2,4);
}

static int add(int x, int y){


int z = x + y;
System.out.println(z);
return z;
}
}
21. For each loop

// String[] animals = {"cat", "dogs", "bird", "rat"};

ArrayList<String> animals = new ArrayList<String>();


animals.add("cat");
animals.add("birds");
for (String i : animals){
System.out.println(i);
}

18. Wrapper classes

Integer a = 123;
Double d = 2.3;
Boolean b = true;
String s = "Hey";
Character c = '=';

19. Array List

ArrayList<String> food = new ArrayList<String>();


food.add("Power_Food");
food.add("Strength");
food.add("Water");
food.set(0, "Sleep"); // same as add
food.remove(2);
food.clear();
for(int i = 0; i < food.size(); i++){
// size not length
System.out.println(food.get(i));
}

20. 2D array List

ArrayList<String> bakeryList = new ArrayList<String>();


bakeryList.add("Pasta");
bakeryList.add("Sushi");
bakeryList.add("Pizza");

// System.out.println(bakeryList);
// System.out.println(bakeryList.get(0));

ArrayList<String> productList = new ArrayList<String>();


productList.add("Tomatos");
productList.add("Banana");
productList.add("Potato");

ArrayList<String> drinkList = new ArrayList<String>();


drinkList.add("Coffee");
drinkList.add("Bubble_tea");

combine

ArrayList<ArrayList<String>> groceryList = new ArrayList();


ArrayList<String> bakeryList = new ArrayList<String>();
bakeryList.add("Pasta");
bakeryList.add("Sushi");
bakeryList.add("Pizza");

ArrayList<String> productList = new ArrayList<String>();


productList.add("Tomatos");
productList.add("Banana");
productList.add("Potato");

ArrayList<String> drinkList = new ArrayList<String>();


drinkList.add("Coffee");
drinkList.add("Bubble_tea");

groceryList.add(productList);
groceryList.add(drinkList);
groceryList.add(bakeryList);

// System.out.println(groceryList);
System.out.println(groceryList.get(0).get(0));

17. String functions

String name = "Tayzar";

// boolean result = name.equalsIgnoreCase("tayzar");


// requals mean exactly equal
// char result = name.charAt(1);
// int result = name.indexOf("y");
boolean result = name.isEmpty();

System.out.println(result);

16. 2D arrays

String[][] currentListOfMedicince ={
{"Bitarmin", "Biojustsis", "Batmintin"},
{"Arenba", "Amberma", "Aquain"},
{"Cetinmin", "Cellgrow", "Cattalim"},
{"Digging", "Dulingo", "Doover"}
};

for (int i = 0; i<currentListOfMedicince.length; i++){


System.out.println();
for (int j = 0; j < currentListOfMedicince[i].length; j++){
System.out.print(currentListOfMedicince[i][j]+ " ");
}
}

15. Arrays

String[] subjects = {"Math", "Eng", "Science", "Chemical"};


//subjects [0] = "Myn";
System.out.println(subjects[0]);

String[] subjects = new String[3];

subjects[0] = "Math";
subjects[1] = "Science";
subjects[2] = "Chemical";

for (int i = 0; i < subjects.length; i++){


System.out.println(subjects[i]);

14. nested loop

Scanner scanner = new Scanner(System.in);


int rows;
int columes;
String symbol = "";

System.out.print("Enter # of rows: ");


rows = scanner.nextInt();
System.out.print("Enter # of columes: ");
columes = scanner.nextInt();
System.out.print("Enter a symbol to use: ");
symbol = scanner.next();

for (int i = 1; i <= rows; i++){


System.out.println();
for (int j = 1; j <= columes; j++){
System.out.print(symbol);
}
}

for (int i = 1; i <= 5; i++){


System.out.println();
for (int j = i; j <= 5; j++){
System.out.print(j);
}
}

for (int i = 1; i <= 5; i++){


System.out.println();
for (int j = i; j <= 5; j++){
System.out.print(i);
}
}

13.for loop

// execute limited amount of time

for (int i = 10; i >= 0;){


// for (int i = 0; i <= 10; i++)
// for (int i = 10; i >= 0; i--)
// for (int i = 10; i >= 0; i-=2)
System.out.println(i);
i-=2;
}

System.out.println("Happy new year.");

12.While loop

Scanner scanner = new Scanner(System.in);


String name = "";

while (name.isBlank()){
// excutes the lines of code as long as that conditons still remain true
System.out.print("Enter your name: ");
name = scanner.nextLine();

System.out.println("Hello, Welcome " +name);

do{
// excutes the lines of code as long as that conditons still remain true
System.out.print("Enter your name: ");
name = scanner.nextLine();

}while (name.isBlank());

11. Logical Operators

int temp;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your temp: ");
temp = scanner.nextInt();
System.out.println(temp);

if (temp >= 30 && temp <= 20){


System.out.println("It is nice outisde!");
}
else if (temp <= 10 || temp <=0){
System.out.println("It is freezing out there!");
}
else if (temp != 15 && temp != 25){
System.out.println("Same as ususal");
}
else {
System.out.println("it is ranning!");
}

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the lower q or upper Q to quite");
String respond = scanner.next();
if(respond.equals("q") || respond.equals("Q")){

System.out.println("You have quit the game");

}
else {
System.out.println("You are still in the game");
}

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the lower q or upper Q to quite: ");
String respond = scanner.next();
if(!respond.equals("q") && !respond.equals("Q")){
System.out.println("You are still in the game");
}
else {
System.out.println("You have quit the game");
}

2. Variables

int age = 23;


System.out.println("I am " + age);
String name = "Tayzar";
System.out.println("Hello " +name);

3. Swap Two variables

String x = "Water";
String y = "Acid";
String temp;

// Switcing the value


temp = x;
x = y;
y = temp;

// x = y Acid
// y = x Water
System.out.println("x: " +x);
System.out.println("y: " +y);

4. User input

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Your Name: ");


String name = scanner.nextLine();
// \n is gone
System.out.print("Enter Your age: ");
int age = scanner.nextInt();
// \n does not go away
scanner.nextLine();
// \n now it is gone

System.out.print("Enter your favorite food: ");


String food = scanner.nextLine();
// \n is gone

System.out.println("Hello, Welcome to the programming world " +name);


System.out.println("You are " +age+ "years old");
System.out.println("Wow! your favorite food is " +food);

5. Experssion

//expression = operands & operators


// operands = values, variables, numbers, quantity
// operators = + - * / %

// int appleSlices = 4;
// double appleSlices = 4;
// appleSlices = appleSlices + 1;
// appleSlices += 1;
// appleSlices ++; // just one
// appleSlices = appleSlices / 3;
// System.out.println(appleSlices);

7. Math class

double a = 23.45;
double b = -30.0;
double c = Math.sqrt(a);
//max, min, abs, sqrt, pow, round, ceil, floor

System.out.println(c);
double x,y,z;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the side x: ");
x = scanner.nextDouble();
System.out.print("Enter the side y: ");
y = scanner.nextDouble();
// hypotenuse formula c = squaroot of a square + b square
z = Math.sqrt((x*x)+(y*y));
System.out.println("The hypotenuse is: " +z);
scanner.close();

8.Random numbers

Random random = new Random();


// int x = random.nextInt(6); // limit 0 - 5
//int x = random.nextInt(6)+1; // limit now 0 - 6
// double y = random.nextDouble(); // 0 to 1
boolean z = random.nextBoolean();
System.out.println(z);

9. if statements

int typesOfGuess = 1;
// Scanner scanner = new Scanner(System.in);
// System.out.println("Enter between 1 to 3");
// System.out.println("Enter your number: ");

if (typesOfGuess == 1){
System.out.println("You are a VIP guess!");
}
else if(typesOfGuess == 2){
System.out.println("You are an entertainer.");
}
else if(typesOfGuess == 3){
System.out.println("You are just a normal guess.");
}
else {
System.out.println("You are not allowed to go in.");
}

10.Switch statments

int gpa = 3;

switch (gpa) {
case 2:
System.out.println("You did normal!");
break;
case 3:
System.out.println("You did average!");
break;
case 4:
System.out.println("You did awesome!");
break;
case 5:
System.out.println("You did ellcentent!");
break;
default:
System.out.println("Keep it up!");
}

GUI

69. mouse listener

new mouselistener();

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

public class mouselistener extends JFrame implements MouseListener {

JLabel label;
ImageIcon a;
ImageIcon b;
ImageIcon c;
ImageIcon d;

mouselistener() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLayout(new FlowLayout());

label = new JLabel();


label.addMouseListener(this);
a = new ImageIcon("a.jpg");
b = new ImageIcon("b.jpg");
c = new ImageIcon("c.png");
d = new ImageIcon("d.png");

label.setIcon(a);

this.add(label);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

@Override
public void mouseClicked(MouseEvent e) {

@Override
public void mousePressed(MouseEvent e) {
label.setIcon(b);

@Override
public void mouseReleased(MouseEvent e) {
label.setIcon(c);

@Override
public void mouseEntered(MouseEvent e) {
label.setIcon(d);

@Override
public void mouseExited(MouseEvent e) {
label.setIcon(a);
}
}

JLabel label;

mouselistener(){

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLayout(null);

label = new JLabel();


label.setBounds(0,0, 100, 100);
label.setBackground(Color.red);
label.setOpaque(true);
label.addMouseListener(this);

this.add(label);
this.setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
//System.out.println("You clicked the mouse ");
}

@Override
public void mousePressed(MouseEvent e) {
System.out.println("You pressed the mouse ");
label.setBackground(Color.black);
}

@Override
public void mouseReleased(MouseEvent e) {
System.out.println("You released the mouse ");
label.setBackground(Color.blue);

@Override
public void mouseEntered(MouseEvent e) {
System.out.println("You entered the component ");
label.setBackground(Color.yellow);
}

@Override
public void mouseExited(MouseEvent e) {
System.out.println("You existed the component ");
label.setBackground(Color.red);

}
}

68. key listener

new keyListener();

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

public class keyListener extends JFrame implements KeyListener {

JLabel label;
ImageIcon icon;

keyListener(){

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.addKeyListener(this);
this.setLayout(null);

icon = new ImageIcon("rocket.png");

label = new JLabel();


label.setBounds(0,0,100,100);
label.setIcon(icon);
//label.setBackground(Color.red);
//label.setOpaque(true);

this.getContentPane().setBackground(Color.black);
this.add(label);
this.setVisible(true);
}

@Override
public void keyTyped(KeyEvent e) {

switch (e.getKeyChar()){
case 'a' :
label.setLocation(label.getX()-10, label.getY());
break;
case 'w' :
label.setLocation(label.getX(), label.getY()-10);
break;
case 's' :
label.setLocation(label.getX()+10, label.getY());
break;
case 'd' :
label.setLocation(label.getX(), label.getY()+10);
break;
}

@Override
public void keyPressed(KeyEvent e) {

switch (e.getKeyCode()){
case 37 :
label.setLocation(label.getX()-10, label.getY());
break;
case 38 :
label.setLocation(label.getX(), label.getY()-10);
break;
case 39 :
label.setLocation(label.getX()+10, label.getY());
break;
case 40 :
label.setLocation(label.getX(), label.getY()+10);
break;
}

}
@Override
public void keyReleased(KeyEvent e) {

System.out.println("YOu released key "+e.getKeyChar());


System.out.println("YOu released code "+e.getKeyCode());

}
}

67. color Chooser

new colorselecter();

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

public class colorselecter extends JFrame implements ActionListener {

JButton button;
JLabel label;

colorselecter() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());

button = new JButton("Pick a color");


button.addActionListener(this);

label = new JLabel();


label.setBackground(Color.white);
label.setText("This is a text");
label.setFont(new Font("MV Boli", Font.PLAIN, 100));
label.setOpaque(true);

this.add(label);
this.add(button);
this.pack();
this.setVisible(true);

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == button){

JColorChooser colorChooser = new JColorChooser();

Color color = JColorChooser.showDialog(null, "Pick a color", Color.black);

label.setForeground(color);

// label.setBackground(color);
}
}
}

66.Jfile choicer class

new fileselecter();

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class fileselecter extends JFrame implements ActionListener {

JButton button;

fileselecter(){

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
button = new JButton("Select file");
button.addActionListener(this);

this.add(button);
this.pack();
this.setVisible(true);

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == button){
JFileChooser fileChooser = new JFileChooser();

fileChooser.setCurrentDirectory(new File("."));

int respones = fileChooser.showOpenDialog(null);


//int respones = fileChooser.showSaveDialog(null);

if (respones == fileChooser.APPROVE_OPTION){

File file = new File(fileChooser.getSelectedFile().getAbsolutePath());

System.out.println(file);
}

}
}
}

65. Menu Bar

new menu();

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

public class menu extends JFrame implements ActionListener {

JMenuBar menuBar;
JMenu fileMenu;
JMenu editMenu;
JMenu helpMenu;
JMenuItem loadItem;
JMenuItem saveItem;
JMenuItem exitItem;
ImageIcon saveIcon;
ImageIcon loadIcon;
ImageIcon existIcon;

menu(){

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLayout(new FlowLayout());

saveIcon = new ImageIcon("save.png");


loadIcon = new ImageIcon("floder.png");
existIcon = new ImageIcon("exist.png");

menuBar = new JMenuBar();


fileMenu = new JMenu("File");
editMenu = new JMenu("Edit");
helpMenu = new JMenu("Help");

loadItem = new JMenuItem("Load");


saveItem = new JMenuItem("Save");
exitItem = new JMenuItem("Exist");

loadItem.addActionListener(this);
saveItem.addActionListener(this);
exitItem.addActionListener(this);

loadItem.setIcon(loadIcon);
saveItem.setIcon(saveIcon);
exitItem.setIcon(existIcon);
fileMenu.setMnemonic(KeyEvent.VK_F); // Alt + f to file
editMenu.setMnemonic(KeyEvent.VK_E); // Alt + e to file
helpMenu.setMnemonic(KeyEvent.VK_H); // Alt + h to file

saveItem.setMnemonic(KeyEvent.VK_S); // S to save
loadItem.setMnemonic(KeyEvent.VK_L); // L to load
exitItem.setMnemonic(KeyEvent.VK_E); //E tp to exist

fileMenu.add(loadItem);
fileMenu.add(saveItem);
fileMenu.add(exitItem);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);

this.setJMenuBar(menuBar);
this.setVisible(true);

}
@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == loadItem){
System.out.println("Loaded file");
}
if (e.getSource() == exitItem){
System.exit(0);
}
if (e.getSource() == saveItem){
System.out.println("saved file");
}

}
}

64. progress bar

new progressbar();

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

public class progressbar {

JFrame frame = new JFrame();


JProgressBar bar = new JProgressBar(0,500);

progressbar(){

bar.setValue(0);
bar.setBounds(0,0,420, 50);
bar.setStringPainted(true);
bar.setFont(new Font("MV Boli", Font.BOLD, 25));
bar.setForeground(Color.red);
bar.setBackground(Color.BLACK);

frame.add(bar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 420);
frame.setLayout(null);
frame.setVisible(true);

fill();

}
public void fill(){

int counter = 500;


while(counter>0){
bar.setValue(counter);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter -= 1;
}
bar.setString("HP out");
}

}
63. Slider
new Slider();

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

public class Slider implements ChangeListener {


JFrame frame;
JLabel label;
JPanel panel;
JSlider slider;

Slider(){

frame = new JFrame("Slider");


label = new JLabel();
panel = new JPanel();
slider = new JSlider(0, 100, 50);

slider.setPreferredSize(new Dimension(400, 200));

slider.setPaintTicks(true);
slider.setMinorTickSpacing(10);

slider.setPaintTrack(true);
slider.setMajorTickSpacing(25);

slider.setPaintLabels(true);
slider.setFont(new Font("MV Boli", Font.PLAIN, 15));
label.setFont(new Font("MV Boli", Font.PLAIN, 25));

//slider.setOrientation(SwingConstants.HORIZONTAL);
slider.setOrientation(SwingConstants.VERTICAL);

label.setText("°C = "+ slider.getValue());

slider.addChangeListener(this);
panel.add(slider);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
@Override
public void stateChanged(ChangeEvent e) {

label.setText("°C = "+ slider.getValue());

}
}

62.Combo BOX

new combobox();

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

public class combobox extends JFrame implements ActionListener{

JComboBox comboBox;
combobox() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());

String[] animals = {"Dogs", "Cat", "Bird"};

comboBox = new JComboBox(animals);


comboBox.addActionListener(this);
// comboBox.addItem("turtle");
// comboBox.setEditable(true);
//System.out.println(comboBox.getItemCount());
// comboBox.insertItemAt("pig", 0);
// comboBox.setSelectedIndex(0);
// comboBox.removeItem("Cat");
// comboBox.removeItemAt(0);
// comboBox.removeItemListener();

this.add(comboBox);
this.pack();
this.setVisible(true);

}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == comboBox)
{
System.out.println(comboBox.getSelectedItem());
}
}
}

61. Radio Button

new RadioButton();

public class RadioButton extends JFrame implements ActionListener {

JRadioButton pizzaButton;
JRadioButton bumgerButton;
JRadioButton hotdogButton;

ImageIcon pizzaIcon;
ImageIcon burgerIcon;
ImageIcon hotdogIcon;

RadioButton(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());

pizzaButton = new JRadioButton("pizza");


bumgerButton = new JRadioButton("burger");
hotdogButton = new JRadioButton("hotdog");

pizzaIcon = new ImageIcon("pizza.png");


burgerIcon = new ImageIcon("burger.jpg");
hotdogIcon = new ImageIcon("hotdog.png");

pizzaButton.addActionListener(this);
bumgerButton.addActionListener(this);
hotdogButton.addActionListener(this);

ButtonGroup group = new ButtonGroup();


group.add(pizzaButton);
group.add(bumgerButton);
group.add(hotdogButton);

pizzaButton.setIcon(pizzaIcon);
bumgerButton.setIcon(burgerIcon);
hotdogButton.setIcon(hotdogIcon);

this.add(pizzaButton);
this.add(bumgerButton);
this.add(hotdogButton);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == pizzaButton){
System.out.println("You order pizza");
}
else if (e.getSource()== bumgerButton){
System.out.println("You order burger");
}
else if(e.getSource() == hotdogButton){
System.out.println("You order hotdog");
}

}
}

60. Check Box

new CheckBox();

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CheckBox extends JFrame implements ActionListener {

JButton button;
JCheckBox checkBox;

CheckBox(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());

button = new JButton("Sumit");


button.addActionListener(this);

checkBox = new JCheckBox();


checkBox.setText("I am not a robot");
checkBox.setFocusable(false);
checkBox.setFont(new Font(null, Font.BOLD, 23));

this.add(button);
this.add(checkBox);
this.pack();
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == button){
System.out.println(checkBox.isSelected());
}

}
}

59. Text field

new TextField();

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextField extends JFrame implements ActionListener {

JButton button;
JTextField textField;

TextField(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());

button = new JButton("Sumit");


button.addActionListener(this);

textField = new JTextField();


textField.setPreferredSize(new Dimension(250,40));
textField.setFont(new Font("Concsal", Font.PLAIN, 23));
textField.setForeground(Color.green);
textField.setBackground(Color.BLACK);
textField.setCaretColor(Color.WHITE);
textField.setText("Username");
//textField.setEditable(false);

this.add(button);
this.add(textField);
this.pack();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == button){
// button.setEnabled(false);
System.out.println("Hello " +textField.getText());

}
}
}

58.Joption Pane
// JOptionPane.showMessageDialog(null, "This is Non-display", "Title",
JOptionPane.PLAIN_MESSAGE );
// JOptionPane.showMessageDialog(null, "This is Non-display", "Title",
JOptionPane.INFORMATION_MESSAGE );
// JOptionPane.showMessageDialog(null, "This is Non-display", "Title",
JOptionPane.QUESTION_MESSAGE );
//JOptionPane.showMessageDialog(null, "This is Non-display", "Title",
JOptionPane.WARNING_MESSAGE );
// JOptionPane.showMessageDialog(null, "This is Non-display", "Title",
JOptionPane.ERROR_MESSAGE );
// int answer = JOptionPane.showConfirmDialog(null, "What is your fucking name
you little bitch?", "Title", JOptionPane.YES_NO_CANCEL_OPTION );
// System.out.println(JOptionPane.showConfirmDialog(null, "What is your fucking
name you little bitch?", "Title", JOptionPane.YES_NO_CANCEL_OPTION ));
// String name = JOptionPane.showInputDialog("What is your name?: ");
// System.out.println("Hello "+name);

ImageIcon icon = new ImageIcon("images.jpg");


String[] respones = {"To be honest I don't die!", "No I ain't diying today!",
"*died*"};

JOptionPane.showOptionDialog(
null,
"I am going to fucking kill you",
"Answer:",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
icon,
respones,
0);
}

57. Open a new GUI window

new LanchPage();

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

public class LanchPage implements ActionListener {


JFrame frame = new JFrame();
JButton button = new JButton("New Window");
LanchPage(){

button.setBounds(100, 160, 200, 40);


button.setFocusable(false);
button.addActionListener(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(null);
frame.add(button);
frame.setVisible(true);

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()== button){
frame.dispose();
NewWindow myWindow = new NewWindow();
}

}
}

public class NewWindow {

JFrame frame = new JFrame();


JLabel label = new JLabel("Hello Madafaka!");

NewWindow(){

label.setBounds(0, 0, 200, 100);


label.setFont(new Font(null, Font.PLAIN, 25));

frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
}

56.Layered Pane

JLabel label1 = new JLabel();


label1.setOpaque(true);
label1.setBackground(Color.RED);
label1.setBounds(50, 50, 200, 200);

JLabel label2 = new JLabel();


label2.setOpaque(true);
label2.setBackground(Color.GREEN);
label2.setBounds(100, 100, 200, 200);

JLabel label3 = new JLabel();


label3.setOpaque(true);
label3.setBackground(Color.BLUE);
label3.setBounds(150, 150, 200, 200);

JLayeredPane layeredPane = new JDesktopPane();


layeredPane.setBounds(0,0, 500, 500);

layeredPane.add(label1, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(label2, Integer.valueOf(2));
layeredPane.add(label3, JLayeredPane.DRAG_LAYER);

JFrame frame = new JFrame("JLayereddPane");


frame.add(layeredPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(500, 500));
frame.setLayout(null);
frame.setVisible(true);

55. Grid Layout

JFrame frame = new JFrame();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new GridLayout(3,3, 10, 10));
frame.add(new Button("1"));
frame.add(new Button("2"));
frame.add(new Button("3"));
frame.add(new Button("4"));
frame.add(new Button("5"));
frame.add(new Button("6"));
frame.add(new Button("7"));
frame.add(new Button("8"));
frame.add(new Button("9"));

frame.setVisible(true);

54. Flow Layout

JFrame frame = new JFrame();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));

JPanel panel1 = new JPanel();


panel1.setPreferredSize(new Dimension(250,250));
panel1.setBackground(Color.BLUE);
panel1.setLayout(new FlowLayout());

panel1.add(new Button("1"));
panel1.add(new Button("2"));
panel1.add(new Button("3"));
panel1.add(new Button("4"));
panel1.add(new Button("5"));
panel1.add(new Button("6"));
panel1.add(new Button("7"));
panel1.add(new Button("8"));
panel1.add(new Button("9"));

frame.add(panel1);
frame.setVisible(true);

53. Border Layout

public static void main(String[] args) {

JFrame frame = new JFrame();


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new BorderLayout(10,10));
frame.setVisible(true);

JPanel panel1 = new JPanel();


JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();

panel1.setBackground(Color.lightGray);
panel2.setBackground(Color.BLACK);
panel3.setBackground(Color.BLUE);
panel4.setBackground(Color.GREEN);
panel5.setBackground(Color.YELLOW);

panel1.setPreferredSize(new Dimension(100, 100));


panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));

//---------------------sub panles------------------------------

JPanel panel6 = new JPanel();


JPanel panel7 = new JPanel();
JPanel panel8 = new JPanel();
JPanel panel9 = new JPanel();
JPanel panel10 = new JPanel();

panel6.setBackground(Color.RED);
panel7.setBackground(Color.MAGENTA);
panel8.setBackground(Color.DARK_GRAY);
panel9.setBackground(Color.PINK);
panel10.setBackground(Color.WHITE);

panel5.setLayout(new BorderLayout());

panel6.setPreferredSize(new Dimension(50, 50));


panel7.setPreferredSize(new Dimension(50, 50));
panel8.setPreferredSize(new Dimension(50, 50));
panel9.setPreferredSize(new Dimension(50, 50));
panel10.setPreferredSize(new Dimension(50, 50));

panel5.add(panel6,BorderLayout.NORTH);
panel5.add(panel7,BorderLayout.WEST);
panel5.add(panel8,BorderLayout.EAST);
panel5.add(panel9,BorderLayout.SOUTH);
panel5.add(panel10,BorderLayout.CENTER);

//-----------------sub panles-----------------------------
frame.add(panel1,BorderLayout.NORTH);
frame.add(panel2,BorderLayout.WEST);
frame.add(panel3,BorderLayout.EAST);
frame.add(panel4,BorderLayout.SOUTH);
frame.add(panel5,BorderLayout.CENTER);

52. buttons

new subFram();

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

public class subFram extends JFrame implements ActionListener{

JButton button;
JLabel label;
ImageIcon icon;
ImageIcon icon2;

subFram(){

icon = new ImageIcon("Do.jpg");


icon2 = new ImageIcon("images.jpg");

label = new JLabel();


label.setIcon(icon2);
label.setBounds(250, 250, 200, 200);
label.setVisible(false);

button = new JButton();


button.setBounds(100, 100, 250, 100);
button.addActionListener(this);
button.setText("Start");
button.setFocusable(false);
button.setIcon(icon);
button.setVerticalTextPosition(JButton.CENTER);
//button.setHorizontalTextPosition(JButton.BOTTOM);
button.setIconTextGap(-100);
button.setForeground(Color.cyan);
button.setBackground(Color.lightGray);
button.setBorder(BorderFactory.createEtchedBorder());

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(800, 800);
this.setVisible(true);
this.add(button);
this.add(label);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button){
label.setVisible(true);

---------------------------------------------------------------------

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

public class subFram extends JFrame implements ActionListener {


JButton button;
subFram(){

button = new JButton();


button.setBounds(200, 100, 100, 50);
button.addActionListener(this);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(800, 800);
this.setVisible(true);
this.add(button);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button){
System.out.println("Boo");
button.setEnabled(false);

}
}
-----------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class subFram extends JFrame implements ActionListener {

JButton button;
ImageIcon icon;
JLabel label;

subFram(){

icon = new ImageIcon("images.jpg");

label = new JLabel();


label.setIcon(icon);
label.setBounds(250, 250, 200, 200);
label.setVisible(false);
button = new JButton();
button.setBounds(100, 100, 250, 100);
button.addActionListener(this);
button.setText("Start");
button.setFocusable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setLayout(null);
this.setVisible(true);
this.add(button);
this.add(label);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button){
label.setVisible(true);
}
}

51.panels

ImageIcon icon = new ImageIcon("love.png");

JLabel label = new JLabel();


label.setText("Hi");
label.setIcon(icon);
//label.setVerticalTextPosition(JLabel.TOP);
// label.setHorizontalAlignment(JLabel.LEFT);
label.setBounds(100, 100, 240, 240);

JPanel redPanel = new JPanel();


redPanel.setBackground(Color.red);
redPanel.setBounds(0,0,250,250);
redPanel.setLayout(null);
// redPanel.setLayout(new BorderLayout());

JPanel bluePanel = new JPanel();


bluePanel.setBackground(Color.blue);
bluePanel.setBounds(250,0,250,250);
bluePanel.setLayout(null);
// bluePanel.setLayout(new BorderLayout());

JPanel greenPanel = new JPanel();


greenPanel.setBackground(Color.green);
greenPanel.setBounds(0,250,500,250);
greenPanel.setLayout(null);
// greenPanel.setLayout(new BorderLayout());

JFrame frame = new JFrame();


frame.setSize(700, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setVisible(true);
greenPanel.add(label);
frame.add(redPanel);
frame.add(bluePanel);
frame.add(greenPanel);

50. labels

ImageIcon icon = new ImageIcon("anime.jpg");


JFrame Frame = new JFrame();
JLabel label = new JLabel(); // create a label
Border border = BorderFactory.createLineBorder(Color.green, 3);

label.setText("How are you?");// set text of label


// or JLabel label = new JLabel("How are you?");
label.setIcon(icon);
label.setHorizontalTextPosition(JLabel.CENTER); // Center, Left, Right of
imageicon
label.setVerticalTextPosition(JLabel.TOP); //Top, Center, Button of imageicon
label.setForeground(Color.WHITE); // change front color
// label.setForeground(new Color(0, 0, 0));
label.setFont(new Font("MV Boli", Font.BOLD, 24)); //front size
label.setIconTextGap(100); // or -25; set gap of text to image
label.setBackground(Color.BLACK);
label.setOpaque(true); // display background color
label.setBorder(border);
label.setHorizontalAlignment(JLabel.CENTER);//set horizontal position of
icon+text
label.setVerticalAlignment(JLabel.TOP); //set vertical position of icon+text withthin
label
// label.setBounds(100,0,250,250); //set x and y positions as well as dimensions

Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Frame.setSize(500,500);
// Frame.setLayout(null);
Frame.setVisible(true);
Frame.add(label);
Frame.pack();
// Frame.getContentPane().setBackground(Color.BLACK);

49. GUI

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

public class MyFrame extends JFrame {

MyFrame(){

this.setTitle("This is Title"); // set title of frame


this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exist out ot
application
this.setResizable(false); // prevent frame from being resized
this.setSize(420,420); //Set x-demension and y-demension
this.setVisible(true); //make frame

ImageIcon image = new ImageIcon("anime.jpg"); // crate logoicon


this.setIconImage(image.getImage()); // change icon of frame
this.getContentPane().setBackground(Color.BLACK); // change background color
}
}

MyFrame myFrame = new MyFrame();


or
new MyFrame();

JFrame Frame = new JFrame(); // create a frame


Frame.setTitle("This is Title"); // set title of frame
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exist out ot
application
Frame.setResizable(false); // prevent frame from being resized
Frame.setSize(420,420); //Set x-demension and y-demension
Frame.setVisible(true); //make frame

ImageIcon image = new ImageIcon("anime.jpg"); // crate logoicon


Frame.setIconImage(image.getImage()); // change icon of frame
Frame.getContentPane().setBackground(Color.BLACK); // change background color
// Frame.getContentPane().setBackground(new Color(0, 0, 0)); or 0xFFFFFFF //
change background color customize

6. GUI intro (Graphical user interface)

String name = JOptionPane.showInputDialog("Enter your name");


// JOptionPane.showMessageDialog(null, "Hello " +name);

int age = Integer.parseInt(JOptionPane.showInputDialog("Enter


Your age"));
// JOptionPane.showMessageDialog(null, "You are " +age+ " years
old");

double height =
Double.parseDouble(JOptionPane.showInputDialog("Enter Your height"));
// JOptionPane.showMessageDialog(null, "You are " +height+ " cm
tall");

JOptionPane.showMessageDialog(null, "Hello " +name);


JOptionPane.showMessageDialog(null, "You are " +age+ " years
old");
JOptionPane.showMessageDialog(null, "You are " +height+ " cm
tall");

Auston java
int days = 1000;
int seconds = 864_00; // one day's seconds
int lightspeed = 186000;
long distance;
long conver_seconds;

conver_seconds = seconds * days;


distance = lightspeed * conver_seconds;
System.out.println("In " +days+ " there are " +conver_seconds+ " seconds");
System.out.println("There are "+distance + " miles in "+ days+ " days");
double pi = 3.142;
double area;
double r = 10.8;
area = pi * (Math.pow(r, 2));
System.out.println(area);

double balance = 234567.898;


Scanner scanner = new Scanner(System.in);
System.out.print("Enter \"1\" to Deposite and \"2\" to Withdraw: ");
int choice = scanner.nextInt();
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();

switch (choice){
case 1:
balance += amount;
break;
case 2:
balance -= amount;
}

System.out.println("Your balance is " +balance);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy