0% found this document useful (0 votes)
3 views4 pages

Answer Assignment 5 -OOP II

The document contains three Java Swing applications that demonstrate different functionalities. The first application allows users to perform basic arithmetic operations with buttons that change the background color. The second application converts Celsius to Fahrenheit and vice versa using text fields, while the third application changes the background color of the window based on button clicks.

Uploaded by

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

Answer Assignment 5 -OOP II

The document contains three Java Swing applications that demonstrate different functionalities. The first application allows users to perform basic arithmetic operations with buttons that change the background color. The second application converts Celsius to Fahrenheit and vice versa using text fields, while the third application changes the background color of the window based on button clicks.

Uploaded by

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

1)

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

class ButtonEvent extends JFrame implements ActionListener {


JButton addButton, subtractButton, multiplyButton, divideButton;

public ButtonEvent() {
// Set up the frame
setTitle("Button Event Demo");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

// Create buttons
addButton = new JButton("ADD");
subtractButton = new JButton("SUBTRACT");
multiplyButton = new JButton("MULTIPLY");
divideButton = new JButton("DIVIDE");

// Add buttons to the frame


add(addButton);
add(subtractButton);
add(multiplyButton);
add(divideButton);

// Register listeners
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {


if (ae.getSource() == addButton) {
getContentPane().setBackground(Color.BLUE);
System.out.println("CALL ADD METHOD");
} else if (ae.getSource() == subtractButton) {
getContentPane().setBackground(Color.RED);
System.out.println("CALL SUBTRACT METHOD");
} else if (ae.getSource() == multiplyButton) {
getContentPane().setBackground(Color.GREEN);
System.out.println("CALL MULTIPLY METHOD");
} else if (ae.getSource() == divideButton) {
getContentPane().setBackground(Color.YELLOW);
System.out.println("CALL DIVIDE METHOD");
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
public void run() {
ButtonEvent frame = new ButtonEvent();
frame.setVisible(true);
}
});
}
}
----------------------------------------------------------------------------
2)

// TextFieldDemo.java: Convert Celsius to Fahrenheit and vice versa


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextFieldDemo extends JFrame implements ActionListener {
private JTextField jtfCelsius = new JTextField(10);
private JTextField jtfFahrenheit = new JTextField(10);
/** Main method */
public static void main(String[] args) {
TextFieldDemo frame = new TextFieldDemo();
frame.pack();
frame.setTitle("TextFieldDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public TextFieldDemo() {
// Panel p1 to hold labels
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 1));
p1.add(new JLabel("Celsius"));
p1.add(new JLabel("Fahrenheit"));
// Panel p2 to hold text fields
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(2, 1));
p2.add(jtfCelsius);
p2.add(jtfFahrenheit);
// Add p1 and p2 to the frame
getContentPane().add(p1, BorderLayout.WEST);
getContentPane().add(p2, BorderLayout.CENTER);
// Set horizontal alignment to RIGHT for text fields
jtfCelsius.setHorizontalAlignment(JTextField.RIGHT);
jtfFahrenheit.setHorizontalAlignment(JTextField.RIGHT);
// Register listener
jtfCelsius.addActionListener(this);
jtfFahrenheit.addActionListener(this);
}
/** Handle ActionEvent */
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jtfCelsius) {
double celsius =
Double.parseDouble(jtfCelsius.getText().trim());
double fahrenheit = (9.0 / 5.0) * celsius + 32.0;
jtfFahrenheit.setText(String.valueOf(fahrenheit));
}
else {
double fahrenheit =
Double.parseDouble(jtfFahrenheit.getText().trim());
double celsius = (5.0 / 9.0) * (fahrenheit - 32.0);
jtfCelsius.setText(String.valueOf(celsius));
}
}
}
---------------------------------------------------------------------------
3)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ColorChangeApplet extends JFrame implements ActionListener {


JButton redButton, greenButton, blueButton, yellowButton, cyanButton;

public ColorChangeApplet() {
setTitle("Color Change App");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Set layout to flow layout


setLayout(new FlowLayout());

// Create buttons and set labels


redButton = new JButton("Red");
greenButton = new JButton("Green");
blueButton = new JButton("Blue");
yellowButton = new JButton("Yellow");
cyanButton = new JButton("Cyan");

// Add buttons to frame


add(redButton);
add(greenButton);
add(blueButton);
add(yellowButton);
add(cyanButton);

// Add action listeners to buttons


redButton.addActionListener(this);
greenButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
cyanButton.addActionListener(this);

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {


// Get the source of the action
JButton clickedButton = (JButton)ae.getSource();

// Change background color based on the button clicked


String color = clickedButton.getText();
switch(color) {
case "Red":
getContentPane().setBackground(Color.RED);
break;
case "Green":
getContentPane().setBackground(Color.GREEN);
break;
case "Blue":
getContentPane().setBackground(Color.BLUE);
break;
case "Yellow":
getContentPane().setBackground(Color.YELLOW);
break;
case "Cyan":
getContentPane().setBackground(Color.CYAN);
break;
default:
break;
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ColorChangeApplet();
}
});
}
}

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