2 Java
2 Java
Each
program includes the source code and a description of its expected behavior and output.
This program creates a window with a single button. When the button is clicked, a small message dialog
box appears with the text "Button clicked!".
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
// Create a button
button.addActionListener(new ActionListener() {
@Override
});
frame.add(button);
frame.setVisible(true);
```
**Expected Result:**
A window titled "Simple Button App" will appear. Inside, there will be a button labeled "Click Me!".
When you click this button, a small pop-up window will appear with the title "Message" (or similar,
depending on OS) and the text "Button clicked!". Clicking "OK" on the pop-up will close it.
This program features a text field for entering a name and a "Submit" button. Upon clicking "Submit", a
message dialog displays a personalized greeting using the entered name.
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 200);
submitButton.addActionListener(new ActionListener() {
@Override
if (name.isEmpty()) {
} else {
JOptionPane.showMessageDialog(frame, "Hello, " + name + "!");
});
frame.add(nameLabel);
frame.add(nameField);
frame.add(submitButton);
frame.setVisible(true);
```
**Expected Result:**
A window titled "Name Input App" will appear. It will have the text "Enter your name:", followed by a
text field, and then a "Submit" button. You can type your name into the text field. When you click
"Submit", a pop-up dialog will appear saying "Hello, [Your Name]!". If you click "Submit" without
entering a name, an error message "Please enter your name!" will be displayed.
This program presents a drop-down list of colors. After selecting a color and clicking the "Show Color"
button, a message dialog will display the name of the selected color.
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 200);
frame.setLayout(new FlowLayout());
showColorButton.addActionListener(new ActionListener() {
@Override
});
frame.add(label);
frame.add(colorComboBox);
frame.add(showColorButton);
frame.setVisible(true);
```
**Expected Result:**
A window titled "Color Selector App" will appear. It will display "Select a color:" followed by a drop-
down list (combo box) containing "Red", "Green", "Blue", "Yellow", and "Orange". Next to it, there will
be a "Show Color" button. When you select a color from the drop-down and click "Show Color", a pop-
up dialog will appear stating "You selected: [Selected Color]!".
This program simulates a basic calculator. It includes a text field for displaying numbers and results, and
buttons for digits (0-9), arithmetic operations (+, -, \*, /), and a "Calculate" button. This implementation
handles simple two-operand operations.
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
private boolean newNumber = true; // Flag to indicate if a new number is being entered
public SimpleCalculatorApp() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLayout(new BorderLayout());
displayField.setHorizontalAlignment(JTextField.RIGHT);
frame.add(displayField, BorderLayout.NORTH);
String[] buttonLabels = {
};
button.addActionListener(new ButtonClickListener());
buttonPanel.add(button);
frame.add(buttonPanel, BorderLayout.CENTER);
frame.setVisible(true);
@Override
if (newNumber) {
currentInput = command;
newNumber = false;
} else {
currentInput += command;
displayField.setText(currentInput);
if (newNumber) {
currentInput = "0.";
newNumber = false;
} else if (!currentInput.contains(".")) {
currentInput += ".";
displayField.setText(currentInput);
lastOperation = command;
newNumber = true;
result = 0;
lastOperation = command;
newNumber = true;
} else {
lastOperation = command;
}
calculate();
currentInput = "";
result = 0;
lastOperation = "";
newNumber = true;
displayField.setText("0");
if (currentInput.isEmpty()) {
if (lastOperation.isEmpty()) {
result = currentValue;
} else {
switch (lastOperation) {
case "+":
result += currentValue;
break;
case "-":
result -= currentValue;
break;
case "*":
result *= currentValue;
break;
case "/":
if (currentValue != 0) {
result /= currentValue;
} else {
break;
displayField.setText(String.valueOf(result));
```
**Expected Result:**
A window titled "Simple Calculator" will appear. At the top, there's a display field showing "0". Below it,
there's a grid of buttons:
* Digits: 0-9
* Operations: +, -, \*, /
* Decimal point: .
* Equals: =
* Clear: C
You can click the digit buttons to enter numbers into the display. Clicking an operation button (+, -, \*, /)
will store the current number and operation. Clicking another number and then "=" will perform the
calculation and display the result. For example:
2. Click "+" -> display still shows "5" (or "0" if `newNumber` logic resets it, but the internal `result` is 5)
Clicking "C" will clear the display and reset the calculator. An error message "Cannot divide by zero!" will
appear if you attempt to divide by zero.