KEMBAR78
Java GUI Programs | PDF | Typefaces | Software
0% found this document useful (0 votes)
10 views4 pages

Java GUI Programs

Uploaded by

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

Java GUI Programs

Uploaded by

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

1.

Mouse Handling Events Demo

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

public class MouseEventsDemo extends Frame implements MouseListener {


String msg = "";

public MouseEventsDemo() {
setSize(400, 300);
setTitle("Mouse Events Demo");
setVisible(true);
addMouseListener(this);
}

public void paint(Graphics g) {


g.drawString(msg, 100, 150);
}

public void mouseClicked(MouseEvent e) {


msg = "Mouse Clicked";
repaint();
}

public void mouseEntered(MouseEvent e) {


msg = "Mouse Entered";
repaint();
}

public void mouseExited(MouseEvent e) {


msg = "Mouse Exited";
repaint();
}

public void mousePressed(MouseEvent e) {


msg = "Mouse Pressed";
repaint();
}

public void mouseReleased(MouseEvent e) {


msg = "Mouse Released";
repaint();
}

public static void main(String[] args) {


new MouseEventsDemo();
}
}

2. Move Shapes with Arrow Keys

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

public class ShapeMover extends Frame implements KeyListener {


int x = 100, y = 100;

public ShapeMover() {
setSize(400, 400);
setTitle("Shape Mover");
setVisible(true);
addKeyListener(this);
}

public void paint(Graphics g) {


g.clearRect(0, 0, 400, 400);
g.setColor(Color.BLUE);
g.fillRect(x, y, 50, 50);
}

public void keyPressed(KeyEvent e) {


int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) x -= 10;
else if (key == KeyEvent.VK_RIGHT) x += 10;
else if (key == KeyEvent.VK_UP) y -= 10;
else if (key == KeyEvent.VK_DOWN) y += 10;
repaint();
}

public void keyReleased(KeyEvent e) {}


public void keyTyped(KeyEvent e) {}

public static void main(String[] args) {


new ShapeMover();
}
}

3. Personal Info Applet

import java.applet.*;
import java.awt.*;

public class PersonalInfoApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Name: John Doe", 20, 20);
g.drawString("Age: 20", 20, 40);
g.drawString("College: Benedict College", 20, 60);
}
}
// <applet code="PersonalInfoApplet.class" width="300" height="100"></applet>

4. Frame Showing Info on Button Check

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

public class PersonalInfoFrame extends Frame implements ActionListener {


Button showBtn;
Label infoLabel;

public PersonalInfoFrame() {
setLayout(new FlowLayout());
showBtn = new Button("Check Info");
infoLabel = new Label("");
add(showBtn);
add(infoLabel);

showBtn.addActionListener(this);
setSize(300, 150);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


infoLabel.setText("Name: John | Age: 20 | Course: BCA");
}

public static void main(String[] args) {


new PersonalInfoFrame();
}
}

5. Frame with Father and Mother Buttons

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

public class ParentInfoFrame extends Frame implements ActionListener {


Button fatherBtn, motherBtn;
Label infoLabel;

public ParentInfoFrame() {
setLayout(new FlowLayout());

fatherBtn = new Button("Father");


motherBtn = new Button("Mother");
infoLabel = new Label("");

add(fatherBtn);
add(motherBtn);
add(infoLabel);

fatherBtn.addActionListener(this);
motherBtn.addActionListener(this);

setSize(400, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == fatherBtn) {
infoLabel.setText("Father: Mr. Robert | Age: 45 | Engineer");
} else if (e.getSource() == motherBtn) {
infoLabel.setText("Mother: Mrs. Anna | Age: 42 | Teacher");
}
}

public static void main(String[] args) {


new ParentInfoFrame();
}
}

6. Font Message Display Window

import java.awt.*;

public class FontMessageWindow extends Frame {


public FontMessageWindow() {
setTitle("Welcome Window");
setSize(400, 200);
setVisible(true);
}

public void paint(Graphics g) {


Font customFont = new Font("Serif", Font.BOLD | Font.ITALIC, 24);
g.setFont(customFont);
g.setColor(Color.MAGENTA);
g.drawString("Welcome to Benedict College", 50, 100);
}

public static void main(String[] args) {


new FontMessageWindow();
}
}

You might also like