0% found this document useful (0 votes)
22 views13 pages

Java GUI Event Handling Examples

The document contains multiple Java applications demonstrating various GUI components and event handling using Swing. Key examples include mouse event handling, a traffic light simulator using checkboxes, radio button actions, and choice menus. Each application showcases different functionalities such as displaying messages based on user interactions and managing layout and components in a JFrame.

Uploaded by

yudupa009
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)
22 views13 pages

Java GUI Event Handling Examples

The document contains multiple Java applications demonstrating various GUI components and event handling using Swing. Key examples include mouse event handling, a traffic light simulator using checkboxes, radio button actions, and choice menus. Each application showcases different functionalities such as displaying messages based on user interactions and managing layout and components in a JFrame.

Uploaded by

yudupa009
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

package javaapplication35;

import [Link].*;

import [Link].*;

import [Link].*;

/**

* This program demonstrates handling of mouse events.

* It displays messages on the window for each mouse action.

*/

public class JavaApplication35 extends JPanel implements MouseListener,


MouseMotionListener {

int mx = 0, my = 0;

String msg = "";

// Constructor: Add event listeners

public JavaApplication35() {

setPreferredSize(new Dimension(400, 250));

addMouseListener(this);

addMouseMotionListener(this);

// Paint method to draw messages on screen

@Override

protected void paintComponent(Graphics g) {

[Link](g);

[Link]("Handling Mouse Events", 20, 20);

[Link](msg, 20, 50);


}

// MouseListener Methods

@Override

public void mouseClicked(MouseEvent e) {

mx = [Link]();

my = [Link]();

msg = "Mouse Clicked at (" + mx + ", " + my + ")";

repaint();

@Override

public void mousePressed(MouseEvent e) {

mx = [Link]();

my = [Link]();

msg = "Mouse Pressed at (" + mx + ", " + my + ")";

repaint();

@Override

public void mouseReleased(MouseEvent e) {

mx = [Link]();

my = [Link]();

msg = "Mouse Released at (" + mx + ", " + my + ")";

repaint();

@Override

public void mouseEntered(MouseEvent e) {


msg = "Mouse Entered the window";

repaint();

@Override

public void mouseExited(MouseEvent e) {

msg = "Mouse Exited the window";

repaint();

// MouseMotionListener Methods

@Override

public void mouseDragged(MouseEvent e) {

mx = [Link]();

my = [Link]();

msg = "Mouse Dragged at (" + mx + ", " + my + ")";

repaint();

@Override

public void mouseMoved(MouseEvent e) {

mx = [Link]();

my = [Link]();

msg = "Mouse Moved at (" + mx + ", " + my + ")";

repaint();

// Main method: to launch JFrame

public static void main(String[] args) {


[Link](() -> {

JFrame frame = new JFrame("Mouse Event Handling Example");

JavaApplication35 panel = new JavaApplication35();

[Link](JFrame.EXIT_ON_CLOSE);

[Link](panel);

[Link]();

[Link](null);

[Link](true);

});

package javaapplication37;

import [Link].*;

import [Link].*;

import [Link].*;
/**

* Traffic Light Simulator

* Demonstrates use of ItemListener and CheckboxGroup in Java.

*/

public class JavaApplication37 extends JFrame implements ItemListener {

JLabel lbl1, lbl2;

JPanel nPanel, cPanel;

CheckboxGroup cbg;

// Constructor

public JavaApplication37() {

setTitle("Traffic Light Simulator");

setSize(600, 400);

setLayout(new GridLayout(2, 1));

// --- TOP PANEL ---

nPanel = new JPanel(new FlowLayout());

lbl1 = new JLabel();

Font font = new Font("Verdana", [Link], 70);

[Link](font);

[Link](lbl1);

add(nPanel);

// --- BOTTOM PANEL ---

cPanel = new JPanel(new FlowLayout());

lbl2 = new JLabel("Select Lights");

Font fontR = new Font("Verdana", [Link], 20);

[Link](fontR);

[Link](lbl2);

// CheckboxGroup for radio-button-like behavior

cbg = new CheckboxGroup();

Checkbox rbn1 = new Checkbox("Red Light", cbg, false);


[Link]([Link]);

[Link](fontR);

[Link](this);

[Link](rbn1);

Checkbox rbn2 = new Checkbox("Orange Light", cbg, false);

[Link]([Link]);

[Link](fontR);

[Link](this);

[Link](rbn2);

Checkbox rbn3 = new Checkbox("Green Light", cbg, false);

[Link]([Link]);

[Link](fontR);

[Link](this);

[Link](rbn3);

add(cPanel);

// Close the window on exit

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

// --- Handle Checkbox Selection ---

@Override

public void itemStateChanged(ItemEvent i) {

Checkbox chk = [Link]();

String str = [Link]();

char choice = [Link](0); // R / O / G

switch (choice) {

case 'R':

[Link]("STOP");

[Link]([Link]);
break;

case 'O':

[Link]("READY");

[Link]([Link]);

break;

case 'G':

[Link]("GO");

[Link]([Link]);

break;

// --- Main Method ---

public static void main(String[] args) {

new JavaApplication37();

package javaapplication38;

import [Link].*;

import [Link].*;

class JavaApplication38

static JRadioButton yes;

static JRadioButton no;


static JTextField text;

// Driver function

public static void main(String args[])

// Create a frame

JFrame frame = new JFrame("Radio Button Frame");

[Link](500, 500);

[Link](null);

[Link]([Link]);

[Link](JFrame.EXIT_ON_CLOSE);

// Create a text field

text = new JTextField();

[Link](0, 0, 500, 50);

[Link](text);

// Create two radio buttons

yes = new JRadioButton("Yes");

no = new JRadioButton("No");

// Set positions

[Link](210, 100, 80, 60);

[Link](210, 200, 80, 60);

// Add buttons to frame

[Link](yes);

[Link](no);

// Display the frame

[Link](true);

}
package javaapplication39;

import [Link].*;

import [Link].*;

import [Link].*;

/**

* Program: Radio Button Action Example

* Description: Demonstrates how to handle radio button click events


using ActionListener.

*/
public class JavaApplication39 implements ActionListener {

static JRadioButton yes;

static JRadioButton no;

static JTextField text;

// Driver function (main method)

public static void main(String args[]) {

// Create a frame

JFrame frame = new JFrame("Radio Button Action");

[Link](500, 500);

[Link](null);

[Link]().setBackground([Link]);

[Link](JFrame.EXIT_ON_CLOSE);

// Create a text field

text = new JTextField();

[Link](50, 30, 400, 40);

[Link](text);

// Create two radio buttons

yes = new JRadioButton("Yes");

no = new JRadioButton("No");

// Set positions

[Link](200, 120, 100, 50);

[Link](200, 200, 100, 50);

// Add radio buttons to ButtonGroup (so only one is selected at a


time)

ButtonGroup group = new ButtonGroup();

[Link](yes);

[Link](no);

// Add buttons to frame


[Link](yes);

[Link](no);

// Create an object of the current class

JavaApplication39 obj = new JavaApplication39();

// Associate ActionListener with radio buttons

[Link](obj);

[Link](obj);

// Display the frame

[Link](true);

// Function to display which button was selected

@Override

public void actionPerformed(ActionEvent e) {

String b = [Link]();

[Link]("Label of Button Selected : " + b);

package javaapplication41;

import [Link].*;

import [Link].*;

import [Link].*;

public class JavaApplication41 {

// Constructor

JavaApplication41() {

// Creating a frame

JFrame f = new JFrame("TextArea Example");

// Creating a TextArea
TextArea area = new TextArea("Welcome to javatpoint");

// Creating a TextField

TextField t1 = new TextField("Hello");

[Link](10, 10, 150, 20);

[Link](t1);

// Setting location and size of TextArea

[Link](10, 40, 300, 300);

[Link](area);

// Frame properties

[Link](400, 400);

[Link](null);

[Link](JFrame.EXIT_ON_CLOSE);

[Link](true);

// Main method

public static void main(String args[]) {

new JavaApplication41();

}}

package javaapplication42;

import [Link].*;

import [Link].*;

public class JavaApplication42 {

// Constructor

JavaApplication42() {

// creating a frame

JFrame f = new JFrame("Choice Example");

// creating a choice (drop-down) component

Choice c = new Choice();

// setting the bounds of the choice menu


[Link](100, 100, 175, 75);

// adding items to the choice menu

[Link]("Ice Cream");

[Link]("Pizza");

[Link]("Muffins");

[Link]("Croissant");

[Link]("Choco Lava Cake");

// adding choice menu to the frame

[Link](c);

// setting default close operation

[Link](JFrame.EXIT_ON_CLOSE);

// setting size, layout, and visibility of frame

[Link](400, 400);

[Link](null);

[Link](true);

// main method

public static void main(String args[]) {

new JavaApplication42();

You might also like