Nothing Special   »   [go: up one dir, main page]

AJP Assignment (22203A0041)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Advance JAVA PROGRAMMING Subject Code:22517

Semester:5th Semester Course: Computer Engineering

Laboratory No: Name of Subject Teacher: Prasad Koyande

Name of Student: Vishakha Gite Roll Id: 22203B0035

Assignment No1
1. Design a Login Interface: Create a visually appealing login interface using Java AWT. Include
fields for the username and password, along with a "Login" button. Customize the layout and add
icons or labels for a better user experience.

Ans:

a] Code:

import java.awt.*;

class GridBagLayoutExample extends Frame

GridBagLayoutExample()

Label lblName = new Label("Username");

TextField txtName =new TextField(10);

Label lblPass = new Label("Password");

TextField txtPass =new TextField(10);

txtPass.setEchoChar('*');

Button btnSubmit = new Button("Login");

setLayout(new GridBagLayout());
GridBagConstraints gc =new GridBagConstraints();

add(lblName,gc,0,0,1,1,0,0);
add(txtName,gc,1,0,1,1,0,20);

add(lblPass,gc,0,1,1,1,0,0);

add(txtPass,gc,1,1,1,1,0,20);

add(btnSubmit,gc,0,2,2,1,0,20);

void add(Component comp,GridBagConstraints gc,int x,int y,int w,int h,int wx,int wy)

gc.gridx = x;

gc.gridy = y;

gc.gridwidth = w;

gc.gridheight= h;

gc.weightx = wx;

gc.weighty = wy;

add(comp,gc);

class GridBagLayoutJavaExample

public static void main(String args[])

GridBagLayoutExample frame = new GridBagLayoutExample();

frame.setTitle("GridBagLayout in Java Example");

frame.setSize(300,200);

frame.setVisible(true);

}
}
b] Output:

2. Create a Registration Form Layout: Design a registration form using Java AWT components.
The form should include fields like Name, Email, Password, and Confirm Password, organized
neatly with appropriate labels and spacing.

Ans:

a] Code:

import java.awt.*;

class GridBagLayoutExample extends Frame

GridBagLayoutExample()

Label lblName = new Label("Username");

TextField txtName =new TextField(10);

Label lblEmail = new Label("Email");

TextField txtEmail =new TextField(10);

Label lblPass = new Label("Password");

TextField txtPass =new TextField(10);

txtPass.setEchoChar('*');

Label lblConfirmPass = new Label("Confirm Password");


TextField txtConfirmPass =new TextField(10);

txtConfirmPass.setEchoChar('*');

Button btnSubmit = new Button("Register");

setLayout(new GridBagLayout());

GridBagConstraints gc =new GridBagConstraints();

add(lblName,gc,0,0,1,1,0,0);

add(txtName,gc,1,0,1,1,0,20);

add(lblEmail,gc,0,1,1,1,0,0);

add(txtEmail,gc,1,1,1,1,0,20);

add(lblPass,gc,0,3,1,1,0,0);
add(txtPass,gc,1,3,1,1,0,20);

add(lblConfirmPass,gc,0,4,1,1,0,0);

add(txtConfirmPass,gc,1,4,1,1,0,20);

add(btnSubmit,gc,0,5,2,1,0,20);

void add(Component comp,GridBagConstraints gc,int x,int y,int w,int h,int wx,int wy)

gc.gridx = x;

gc.gridy = y;

gc.gridwidth = w;

gc.gridheight= h;

gc.weightx = wx;

gc.weighty = wy;

add(comp,gc);

class GridBagLayoutJavaExample
{

public static void main(String args[])

GridBagLayoutExample frame = new GridBagLayoutExample();

frame.setTitle("GridBagLayout in Java Example");

frame.setSize(300,200);

frame.setVisible(true);

}
b] Output:

3. Design a Customizable Color Picker: Develop a color picker application using Java AWT. Users
should be able to select colors from a palette and see the preview of the selected color in a
designated area.

Ans:

a] Code:

import java.awt.*;

import java.awt.event.*;

public class ColorPicker extends Frame implements ActionListener {

Panel colorPreview;

public ColorPicker() {

setTitle("Customizable Color Picker");


setLayout(new FlowLayout());

colorPreview = new Panel();

colorPreview.setBackground(Color.WHITE);

colorPreview.setPreferredSize(new Dimension(200, 200));

add(colorPreview);

addColorButton("Red", Color.RED);

addColorButton("Green", Color.GREEN);

addColorButton("Blue", Color.BLUE);

addColorButton("Yellow", Color.YELLOW);

addColorButton("Orange", Color.ORANGE);

addColorButton("Black", Color.BLACK);

setSize(250, 400);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);
}

});

private void addColorButton(String name, Color color) {

Button button = new Button(name);

button.setBackground(color);

button.addActionListener(this);

add(button);

public void actionPerformed(ActionEvent ae) {

Button source = (Button) ae.getSource();


colorPreview.setBackground(source.getBackground());

public static void main(String[] args) {

new ColorPicker();

b] Output:

4. Create a Responsive Grid Layout: Design a grid-based layout using Java AWT where the
components rearrange themselves when the window is resized. Use AWT Layout Managers to
ensure a responsive design.

Ans:

a] Code:

import java.awt.*;

import java.awt.event.*;

public class SimpleGridLayout extends Frame {

public SimpleGridLayout() {

setTitle("Simple Grid Layout");

setLayout(new GridLayout(3, 3, 5, 5));


for (int i = 1; i <= 9; i++) {

Button button = new Button("Button " + i);

add(button);

setSize(400, 400);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

public static void main(String[] args) {

new SimpleGridLayout();

}
b] Output:

5. Design a Calculator Interface: Create an aesthetically pleasing interface for a basic calculator
using Java AWT. Focus on the alignment of buttons, colors, and font styles to make the interface
user-friendly.

Ans:

a] Code:
import java.awt.*;

import java.awt.event.*;

public class Calculator extends Frame implements ActionListener {

private TextField display;

private String currentInput = "";

private String operator = "";

private double firstOperand = 0;

private boolean isOperatorSet = false;

public Calculator() {

// Set the frame title

setTitle("Calculator");

// Set the layout manager for the frame

setLayout(new BorderLayout());

// Create and configure display

display = new TextField();

display.setFont(new Font("Arial", Font.BOLD, 24));

display.setEditable(false);

add(display, BorderLayout.NORTH);
// Create panel for buttons and set its

layout Panel buttonPanel = new Panel();

buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));


// Define button labels

String[] buttonLabels = {

"7", "8", "9", "/",

"4", "5", "6", "*",

"1", "2", "3", "-",

"C", "0", "=", "+"

};

// Create and add buttons

for (String label : buttonLabels) {

Button button = new Button(label);

button.setFont(new Font("Arial", Font.BOLD, 18));

button.addActionListener(this);

buttonPanel.add(button);

// Add button panel to frame

add(buttonPanel, BorderLayout.CENTER);

// Set frame size and make it visible

setSize(300, 400);

setVisible(true);
// Add window closing event

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

}
});

@Override

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if (command.charAt(0) >= '0' && command.charAt(0) <= '9')

{ // If the command is a digit, add it to the current input

currentInput += command;

display.setText(currentInput);

} else if (command.charAt(0) == 'C') {

// Clear everything

currentInput = "";

operator = "";

firstOperand = 0;

isOperatorSet = false;

display.setText("");

} else if (command.charAt(0) == '=') {

// Calculate result

try {
double secondOperand = Double.parseDouble(currentInput);

double result = performCalculation(firstOperand, secondOperand,

operator); display.setText(String.valueOf(result));

currentInput = String.valueOf(result);

operator = "";

isOperatorSet = false;
} catch (Exception ex) {

display.setText("Error");

} else {

// Handle operators

if (isOperatorSet) {

// If an operator is already set, replace it with the new operator

operator = command;

display.setText(firstOperand + " " + operator + " " +

currentInput); } else {

// Set first operand and operator

firstOperand = Double.parseDouble(currentInput);

operator = command;

currentInput = "";

isOperatorSet = true;

display.setText(firstOperand + " " + operator);

private double performCalculation(double num1, double num2, String op) {


switch (op) {

case "+":

return num1 + num2;

case "-":

return num1 - num2;

case "*":
return num1 * num2;

case "/":

if (num2 != 0) {

return num1 / num2;

} else {

throw new ArithmeticException("Division by

zero"); }

default:

return 0;

public static void main(String[] args) {

new Calculator();

}
b] Output:

6. Build a Custom Dialog Box: Design a custom dialog box using Java AWT. The dialog should
contain multiple components like labels, buttons, and text fields, arranged in a visually appealing
manner.

Ans:
a] Code:

import java.awt.*;

import java.awt.event.*;

public class CustomDialogExample extends Frame implements ActionListener

{ private Button openDialogButton;

public CustomDialogExample() {

setTitle("Custom Dialog Example");

setLayout(new FlowLayout());

openDialogButton = new Button("Open Dialog");

openDialogButton.addActionListener(this);

add(openDialogButton);

setSize(300, 200);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

public void actionPerformed(ActionEvent e) {

if (e.getSource() == openDialogButton) {

createCustomDialog();

}
}

private void createCustomDialog() {

Dialog dialog = new Dialog(this, "Custom Dialog", true);


dialog.setLayout(new GridLayout(4, 2, 10, 10));

Label nameLabel = new Label("Name:");

TextField nameField = new TextField();

Label emailLabel = new Label("Email:");

TextField emailField = new TextField();

Button submitButton = new Button("Submit");

Button cancelButton = new Button("Cancel");

dialog.add(nameLabel);

dialog.add(nameField);

dialog.add(emailLabel);

dialog.add(emailField);

dialog.add(submitButton);

dialog.add(cancelButton);

submitButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

System.out.println("Submitted: " + nameField.getText() + ", " +

emailField.getText()); dialog.dispose();

});

cancelButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

dialog.dispose();

});
dialog.setSize(300, 150);

dialog.setVisible(true);

}
public static void main(String[] args) {

new CustomDialogExample();

b] Output:

7. Design a Feedback Form: Create a feedback form using Java AWT that includes text areas for
comments and ratings. Use AWT components to ensure the layout is clean and easy to navigate.

Ans:

a] Code:

import java.awt.*;

import java.awt.event.*;

public class FeedbackForm extends Frame implements ActionListener {

private TextArea commentsArea;

private TextField ratingField;

private Button submitButton;

public FeedbackForm() {

setTitle("Feedback Form");

setLayout(new GridLayout(5, 1, 10, 10));


Label commentsLabel = new Label("Comments:");

commentsArea = new TextArea(5, 30);

Label ratingLabel = new Label("Rating (1-5):");

ratingField = new TextField(5);

submitButton = new Button("Submit");

submitButton.addActionListener(this);

add(commentsLabel);

add(commentsArea);

add(ratingLabel);

add(ratingField);

add(submitButton);

setSize(300, 300);

setVisible(true);
addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent

we) { System.exit(0);

});

public void actionPerformed(ActionEvent e)

{ String comments =

commentsArea.getText(); String rating =

ratingField.getText();

if (comments.isEmpty() || rating.isEmpty())

{ System.out.println("Please fill out all fields.");

} else {

System.out.println("Feedback submitted!");

System.out.println("Comments: " +
comments); System.out.println("Rating: " +

rating);

commentsArea.setText("");

ratingField.setText("");

public static void main(String[] args) {

new FeedbackForm();

}
b] Output:

8. Create a Dashboard Interface: Design a simple dashboard interface using Java AWT. The
dashboard should contain multiple panels, each representing different sections like user profile,
settings, and notifications.

Ans:

a] Code:

import java.awt.*;

import java.awt.event.*;
public class Dashboard extends Frame {

public Dashboard() {

setTitle("Simple Dashboard");

setLayout(new GridLayout(1, 3, 10, 10));

Panel userProfilePanel = new Panel();

Panel settingsPanel = new Panel();

Panel notificationsPanel = new Panel();

userProfilePanel.setLayout(new BorderLayout());

settingsPanel.setLayout(new BorderLayout());

notificationsPanel.setLayout(new BorderLayout());

userProfilePanel.add(new Label("User Profile", Label.CENTER), BorderLayout.NORTH);

settingsPanel.add(new Label("Settings", Label.CENTER), BorderLayout.NORTH);

notificationsPanel.add(new Label("Notifications", Label.CENTER),

BorderLayout.NORTH); userProfilePanel.add(new Label("Name: John Doe"),

BorderLayout.CENTER); settingsPanel.add(new Button("Change Settings"),

BorderLayout.CENTER);

notificationsPanel.add(new TextArea("No new notifications"),

BorderLayout.CENTER) add(userProfilePanel);

add(settingsPanel);

add(notificationsPanel);

setSize(600, 200);
setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

});

}
public static void main(String[] args) {

new Dashboard();

b] Output:

9. Design a Menu Bar and Toolbar: Create a window with a menu bar and toolbar using Java AWT.
Include multiple menus and buttons with icons in the toolbar. Focus on the alignment and overall
design consistency.

Ans:

a] Code:

import java.awt.*;

import java.awt.event.*;

public class SimpleMenuToolbar extends Frame {

public SimpleMenuToolbar() {

// Set the frame title

setTitle("Simple Menu and Toolbar");


// Set layout manager

setLayout(new BorderLayout());

// Create the menu bar

MenuBar menuBar = new MenuBar();

// Create menus

Menu fileMenu = new Menu("File");

Menu editMenu = new Menu("Edit");

Menu helpMenu = new Menu("Help");

// Add menu items to the File menu

fileMenu.add(new MenuItem("New"));

fileMenu.add(new MenuItem("Open"));
fileMenu.add(new MenuItem("Exit"));

// Add menu items to the Edit menu

editMenu.add(new MenuItem("Cut"));

editMenu.add(new MenuItem("Copy"));

editMenu.add(new MenuItem("Paste"));

// Add menu items to the Help menu

helpMenu.add(new MenuItem("About"));

// Add menus to the menu bar

menuBar.add(fileMenu);

menuBar.add(editMenu);
menuBar.add(helpMenu);

// Set the menu bar for the frame

setMenuBar(menuBar);

// Create a toolbar using a panel

Panel toolbar = new Panel();

toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

// Add buttons to the toolbar

toolbar.add(new Button("New"));

toolbar.add(new Button("Open"));

toolbar.add(new Button("Save"));

toolbar.add(new Button("Cut"));
toolbar.add(new Button("Copy"));

toolbar.add(new Button("Paste"));

// Add toolbar panel to the frame

add(toolbar, BorderLayout.NORTH);

// Set frame size and make it visible

setSize(400, 300);

setVisible(true);

// Add window closing event

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent


we) { System.exit(0);

});

public static void main(String[] args) {

new SimpleMenuToolbar();

}
b] Output:

10. Create a Digital Clock Interface: Design a digital clock interface using Java AWT. The clock
should display the current time in a large, readable font. Experiment with different color schemes
and layouts to make the clock visually appealing.
Ans:
a] Code:
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;

public class DigitalClock extends Frame {


Label timeLabel;

public DigitalClock() {
// Set the frame title
setTitle("Digital Clock");

// Set layout manager for the frame


setLayout(new FlowLayout());

// Create a label to display the time


timeLabel = new Label();
timeLabel.setFont(new Font("Arial", Font.BOLD, 48));
add(timeLabel);

// Set frame size and make it visible


setSize(400, 150);
setVisible(true);

// Update the time every second


new Timer().start();
// Add window closing event
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}

// Timer class to update the clock


class Timer extends Thread {
public void run() {
while (true) {
// Get the current time
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

// Format the time as HH:MM:SS


String time = String.format("%02d:%02d:%02d", hour, minute, second);

// Update the time label


timeLabel.setText(time);

// Sleep for 1 second


try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {


new DigitalClock();
}
}

b] Output:

You might also like