AJP Assignment (22203A0041)
AJP Assignment (22203A0041)
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.*;
GridBagLayoutExample()
txtPass.setEchoChar('*');
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
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.*;
GridBagLayoutExample()
txtPass.setEchoChar('*');
txtConfirmPass.setEchoChar('*');
setLayout(new GridBagLayout());
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
{
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.*;
Panel colorPreview;
public ColorPicker() {
colorPreview.setBackground(Color.WHITE);
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() {
System.exit(0);
}
});
button.setBackground(color);
button.addActionListener(this);
add(button);
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 SimpleGridLayout() {
add(button);
setSize(400, 400);
setVisible(true);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
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 Calculator() {
setTitle("Calculator");
setLayout(new BorderLayout());
display.setEditable(false);
add(display, BorderLayout.NORTH);
// Create panel for buttons and set its
String[] buttonLabels = {
};
button.addActionListener(this);
buttonPanel.add(button);
add(buttonPanel, BorderLayout.CENTER);
setSize(300, 400);
setVisible(true);
// Add window closing event
addWindowListener(new WindowAdapter() {
System.exit(0);
}
});
@Override
currentInput += command;
display.setText(currentInput);
// Clear everything
currentInput = "";
operator = "";
firstOperand = 0;
isOperatorSet = false;
display.setText("");
// Calculate result
try {
double secondOperand = Double.parseDouble(currentInput);
operator); display.setText(String.valueOf(result));
currentInput = String.valueOf(result);
operator = "";
isOperatorSet = false;
} catch (Exception ex) {
display.setText("Error");
} else {
// Handle operators
if (isOperatorSet) {
operator = command;
currentInput); } else {
firstOperand = Double.parseDouble(currentInput);
operator = command;
currentInput = "";
isOperatorSet = true;
case "+":
case "-":
case "*":
return num1 * num2;
case "/":
if (num2 != 0) {
} else {
zero"); }
default:
return 0;
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 CustomDialogExample() {
setLayout(new FlowLayout());
openDialogButton.addActionListener(this);
add(openDialogButton);
setSize(300, 200);
setVisible(true);
addWindowListener(new WindowAdapter() {
System.exit(0);
});
if (e.getSource() == openDialogButton) {
createCustomDialog();
}
}
dialog.add(nameLabel);
dialog.add(nameField);
dialog.add(emailLabel);
dialog.add(emailField);
dialog.add(submitButton);
dialog.add(cancelButton);
submitButton.addActionListener(new ActionListener() {
emailField.getText()); dialog.dispose();
});
cancelButton.addActionListener(new ActionListener() {
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 FeedbackForm() {
setTitle("Feedback Form");
submitButton.addActionListener(this);
add(commentsLabel);
add(commentsArea);
add(ratingLabel);
add(ratingField);
add(submitButton);
setSize(300, 300);
setVisible(true);
addWindowListener(new WindowAdapter()
we) { System.exit(0);
});
{ String comments =
ratingField.getText();
if (comments.isEmpty() || rating.isEmpty())
} else {
System.out.println("Feedback submitted!");
System.out.println("Comments: " +
comments); System.out.println("Rating: " +
rating);
commentsArea.setText("");
ratingField.setText("");
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");
userProfilePanel.setLayout(new BorderLayout());
settingsPanel.setLayout(new BorderLayout());
notificationsPanel.setLayout(new BorderLayout());
BorderLayout.CENTER);
BorderLayout.CENTER) add(userProfilePanel);
add(settingsPanel);
add(notificationsPanel);
setSize(600, 200);
setVisible(true);
addWindowListener(new WindowAdapter() {
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 SimpleMenuToolbar() {
setLayout(new BorderLayout());
// Create menus
fileMenu.add(new MenuItem("New"));
fileMenu.add(new MenuItem("Open"));
fileMenu.add(new MenuItem("Exit"));
editMenu.add(new MenuItem("Cut"));
editMenu.add(new MenuItem("Copy"));
editMenu.add(new MenuItem("Paste"));
helpMenu.add(new MenuItem("About"));
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
setMenuBar(menuBar);
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
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, BorderLayout.NORTH);
setSize(400, 300);
setVisible(true);
addWindowListener(new WindowAdapter()
});
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 DigitalClock() {
// Set the frame title
setTitle("Digital Clock");
b] Output: