Frames

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

public class FontExample extends JFrame {
    public FontExample() {
        setTitle("Font Example");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setFont(new Font("Serif", Font.BOLD, 20));
        g.drawString("Serif Bold", 50, 50);
        
        g.setFont(new Font("SansSerif", Font.ITALIC, 20));
        g.drawString("SansSerif Italic", 50, 100);
        
        g.setFont(new Font("Monospaced", Font.PLAIN, 20));
        g.drawString("Monospaced Plain", 50, 150);
    }

    public static void main(String[] args) {
        new FontExample().setVisible(true);
    }
}

2) -- draw shapes lines

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

public class ShapeExample extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(50, 50, 200, 50);
        g.drawRect(50, 70, 100, 50);
        g.drawOval(50, 150, 50, 50);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new ShapeExample());
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


3)------ paint mode 
import java.awt.*;
import javax.swing.*;

public class PaintModeExample extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(50, 50, 100, 50);
        g.setXORMode(Color.WHITE);
        g.fillRect(70, 70, 100, 50);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new PaintModeExample());
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

4)----------window events 

import java.awt.*;
import java.awt.event.*;

public class WindowExample extends Frame implements WindowListener {
    public WindowExample() {
        addWindowListener(this);
        setSize(300, 200);
        setVisible(true);
    }

    public void windowOpened(WindowEvent e) { System.out.println("Window Opened"); }
    public void windowClosing(WindowEvent e) { System.out.println("Window Closing"); System.exit(0); }
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) { System.out.println("Window Minimized"); }
    public void windowDeiconified(WindowEvent e) { System.out.println("Window Restored"); }
    public void windowActivated(WindowEvent e) { System.out.println("Window Activated"); }
    public void windowDeactivated(WindowEvent e) { System.out.println("Window Deactivated"); }

    public static void main(String[] args) {
        new WindowExample();
    }
}

5)--------mouse events 
import java.awt.*;
import java.awt.event.*;

public class MouseExample extends Frame implements MouseListener {
    public MouseExample() {
        addMouseListener(this);
        setSize(300, 200);
        setVisible(true);
    }

    public void mouseClicked(MouseEvent e) { System.out.println("Mouse Clicked"); }
    public void mousePressed(MouseEvent e) { System.out.println("Mouse Pressed"); }
    public void mouseReleased(MouseEvent e) { System.out.println("Mouse Released"); }
    public void mouseEntered(MouseEvent e) { System.out.println("Mouse Entered"); }
    public void mouseExited(MouseEvent e) { System.out.println("Mouse Exited"); }

    public static void main(String[] args) {
        new MouseExample();
    }
}


6)----- keyboard events

import java.awt.*;
import java.awt.event.*;

public class KeyboardExample extends Frame implements KeyListener {
    public KeyboardExample() {
        addKeyListener(this);
        setSize(300, 200);
        setVisible(true);
    }

    public void keyPressed(KeyEvent e) { System.out.println("Key Pressed: " + e.getKeyChar()); }
    public void keyReleased(KeyEvent e) { System.out.println("Key Released: " + e.getKeyChar()); }
    public void keyTyped(KeyEvent e) { System.out.println("Key Typed: " + e.getKeyChar()); }

    public static void main(String[] args) {
        new KeyboardExample();
    }
}

7)-----multicasting

import java.awt.*;
import java.awt.event.*;

public class MulticastExample extends Frame implements ActionListener {
    Button button;

    public MulticastExample() {
        button = new Button("Click Me");
        button.addActionListener(this);
        add(button);
        setSize(300, 200);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("Button Clicked");
    }

    public static void main(String[] args) {
        new MulticastExample();
    }
}

8)--- first frame

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

public class MyFrame {
    public static void main(String args[]) {
        JFrame f = new JFrame();
        f.setVisible(true);
        f.setTitle("First Frame");
        f.setSize(500, 300);
        JLabel l1 = new JLabel("My firsttt frame");
        f.add(l1);
        l1.setFont(new Font("Verdana", Font.BOLD, 34));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

9)------ focus  events 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FocusEventExample extends JFrame implements FocusListener {
    JTextField textField;

    FocusEventExample() {
        setTitle("Focus Event Example");
        setSize(300, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        textField = new JTextField(15);
        textField.addFocusListener(this);

        add(new JLabel("Click inside the text field:"));
        add(textField);

        setVisible(true);
    }

    public void focusGained(FocusEvent e) {
        textField.setBackground(Color.YELLOW);
    }

    public void focusLost(FocusEvent e) {
        textField.setBackground(Color.WHITE);
    }

    public static void main(String[] args) {
        new FocusEventExample();
    }
}

10)---------labels

import javax.swing.*;

public class LabelExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Label Example");
        JLabel label = new JLabel("Hello, this is a JLabel!");
        
        frame.add(label);
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}



Comments

Popular posts from this blog

JAVAEXT