JAVAEXT
1) FONT :-
import java.awt.*;
public class DisplayFont_1{
public static void main(String args[]){
Frame f = new Frame();
f.setVisible(true);
f.setTitle("My Frame");
f.setSize(600,300);
Graphics g = f.getGraphics();
g.setFont(new Font("Times New Roman", Font.PLAIN,20));
g.drawString("Times New Roman",50,50);
g.setFont(new Font("Arial",Font.BOLD,20));
g.drawString("Arial",50,100);
g.setFont(new Font("Caribri",Font.ITALIC,20));
g.drawString("Caribri",50,150);
}
}
2)GRAPHICS :-
import java.awt.*;
import javax.swing.*;
public class DisplayGraphics_2 extends Canvas{
public void paint(Graphics g) {
g.drawLine(50,50,180,50);
g.drawRect(50, 100,100,50);
g.drawOval(50,180,180, 100);
}
public static void main(String[] args) {
DisplayGraphics_2 m=new DisplayGraphics_2();
JFrame f=new JFrame();
f.add(m);
f.setSize(700,700);
f.setVisible(true);
}
}
3)FACE DRAWING:-
import javax.swing.*;
import java.awt.*;
public class FaceDrawing extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.fillOval(50, 50, 150, 150);
g.setColor(Color.BLACK);
g.fillOval(80, 90, 20, 20);
g.fillOval(150, 90, 20, 20);
g.drawArc(90, 120, 70, 40, 0, -180);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Human Face");
FaceDrawing face = new FaceDrawing();
frame.add(face);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
4)KEYBOARD EVENT
import java.awt.event.*;
import javax.swing.*;
public class KeyboardEventDemo extends JFrame implements KeyListener {
public KeyboardEventDemo() {
setTitle("Keyboard Event Demo");
setSize(400, 300);
addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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 KeyboardEventDemo();
}
}
5)LABEL BUTTON
import javax.swing.*;
import java.awt.event.*;
public class LabelButtonDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Label & Button Example");
JLabel label = new JLabel("Click the button!");
JButton button = new JButton("Click Me");
button.addActionListener(e -> label.setText("Button Clicked!"));
frame.setLayout(new java.awt.FlowLayout());
frame.add(label);
frame.add(button);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
6)LISTCOMBOBOX
import javax.swing.*;
import java.awt.event.*;
public class ListComboBoxDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("List & Combo Box Example");
String[] items = {"Apple", "Banana", "Cherry"};
JList<String> list = new JList<>(items);
JComboBox<String> comboBox = new JComboBox<>(items);
JButton button = new JButton("Show Selection");
button.addActionListener(e -> {
System.out.println("List Selected: " + list.getSelectedValue());
System.out.println("Combo Selected: " +
comboBox.getSelectedItem());
});
frame.setLayout(new java.awt.FlowLayout());
frame.add(new JScrollPane(list));
frame.add(comboBox);
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
7)MENU DEMO
import javax.swing.*;
import java.awt.event.*;
public class MenuDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Menu Example");
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
JMenuItem exit = new JMenuItem("Exit");
fileMenu.add(open);
fileMenu.add(exit);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
open.addActionListener(e -> System.out.println("Open Clicked"));
exit.addActionListener(e -> System.exit(0));
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
8)MOUSE EVENT
import java.awt.event.*;
import javax.swing.*;
public class MouseEventDemo extends JFrame implements MouseListener {
public MouseEventDemo() {
setTitle("Mouse Event Demo");
setSize(400, 300);
addMouseListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");
}
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at (" + e.getX() + ", " + e.getY() + ")");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered the Frame");
}
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited the Frame");
}
public static void main(String[] args) {
new MouseEventDemo();
}
}
9)MULTICASTING
import java.awt.event.*;
import javax.swing.*;
public class MulticastingDemo {
public static void main(String[] args) {
JButton btn = new JButton("Click Me");
JFrame frame = new JFrame("Multicasting Example");
btn.addActionListener(e -> System.out.println("Listener 1: Button Clicked"));
btn.addActionListener(e -> System.out.println("Listener 2: Button Clicked"));
frame.add(btn);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
10)MULTITHREAD
class MyRunnable extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
try {
Thread.sleep(500); // Pause for 500ms
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class MyThreadExample {
public static void main(String[] args) {
MyRunnable t1 = new MyRunnable();
MyRunnable t2 = new MyRunnable();
t1.start();
t2.start();
}
}
11)PARAMETER THREAD
import javax.swing.*;
public class ParameterFrame extends JFrame {
String message;
public ParameterFrame(String msg) {
message = msg;
setTitle("Parameter Example");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(java.awt.Graphics g) {
super.paint(g);
g.drawString(message, 50, 50);
}
public static void main(String[] args) {
new ParameterFrame("Hello, this is a parameter!");
}
}
12)RADIO CHECK BOX
import javax.swing.*;
import java.awt.event.*;
public class RadioCheckBoxDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Radio & Check Box Example");
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup group = new ButtonGroup();
group.add(male);
group.add(female);
JCheckBox checkBox = new JCheckBox("Accept Terms");
JButton button = new JButton("Show Selection");
button.addActionListener(e -> {
System.out.println("Gender: " + (male.isSelected() ? "Male" :
female.isSelected() ? "Female" : "None"));
System.out.println("Terms Accepted: " + (checkBox.isSelected() ?
"Yes" : "No"));
});
frame.setLayout(new java.awt.FlowLayout());
frame.add(male);
frame.add(female);
frame.add(checkBox);
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
13)TEXT INPUT
import javax.swing.*;
import java.awt.event.*;
public class TextInputDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Text Input Example");
JTextField textField = new JTextField(20);
JButton button = new JButton("Show Text");
button.addActionListener(e -> System.out.println("Entered Text: " +
textField.getText()));
frame.setLayout(new java.awt.FlowLayout());
frame.add(textField);
frame.add(button);
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
14)WINDOW EVENT
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowEventDemo extends JFrame implements WindowListener {
public WindowEventDemo() {
setTitle("Window Event Demo");
setSize(400, 300);
addWindowListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); }
public void windowOpened(WindowEvent e) {
System.out.println("Window Opened");
}
public void windowClosing(WindowEvent e) {
System.out.println("Window Closing");
}
public void windowClosed(WindowEvent e) {
System.out.println("Window Closed");
}
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 WindowEventDemo();
}
}
Comments
Post a Comment