LUMBINI ACADEMIC COLLEGE
(Affilated to Tribhuvan University)
LAB REPORT OF:
Advance Java Programming [CACS 354]
SUBMITTED BY SUBMITTED TO
Name: _________________ _________________
Santosh Pokhrel
Faculty: BCA 6th Semester
Java swing code to print hello world:
import [Link].*;
public class Swing {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
JPanel panel = new JPanel();
JLabel label = new JLabel("Hello, Swing!");
[Link](label);
JButton button = new JButton("Click Me");
[Link](button);
[Link](panel);
[Link](true);
}
}
Output:
Java Code to Create a Register Page:
import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
public class RegistrationForm extends JFrame {
private JTextField firstNameField, lastNameField, emailField, addressField;
private JFormattedTextField dobField, phoneField;
private ButtonGroup genderGroup;
public RegistrationForm() {
// Frame properties
setTitle("Registration Form");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel;
JLabel firstNameLabel, lastNameLabel, dobLabel, genderLabel, phoneLabel,
emailLabel, addressLabel;
JRadioButton maleRadioButton, femaleRadioButton;
JButton registerButton;
int yoffset = 20;
int labelHeight = 30;
int fieldHeight = 30;
int buttonHeight = 30;
int gapHeight = 10;
// Panel to hold the components
panel = new JPanel();
[Link](null);
// Labels and text fields
firstNameLabel = new JLabel("First Name:");
[Link](20, yoffset, 80, labelHeight);
firstNameField = new JTextField();
[Link](100, yoffset, 150, fieldHeight);
addPlaceholder(firstNameField, "Enter First Name");
yoffset = yoffset + gapHeight + labelHeight;
lastNameLabel = new JLabel("Last Name:");
[Link](20, yoffset, 80, labelHeight);
lastNameField = new JTextField();
[Link](100, yoffset, 150, fieldHeight);
addPlaceholder(lastNameField, "Enter Last Name");
yoffset = yoffset + gapHeight + labelHeight;
dobLabel = new JLabel("DOB:");
[Link](20, yoffset, 80, labelHeight);
dobField = new JFormattedTextField(new DateFormatter());
[Link](100, yoffset, 150, fieldHeight);
addPlaceholder(dobField, "yyyy-MM-dd");
yoffset = yoffset + gapHeight + labelHeight;
// Gender radio buttons
genderLabel = new JLabel("Gender:");
[Link](20, yoffset, 80, labelHeight);
maleRadioButton = new JRadioButton("Male");
[Link](100, yoffset, 60, buttonHeight);
femaleRadioButton = new JRadioButton("Female");
[Link](160, yoffset, 70, buttonHeight);
genderGroup = new ButtonGroup();
[Link](maleRadioButton);
[Link](femaleRadioButton);
yoffset = yoffset + gapHeight + labelHeight;
phoneLabel = new JLabel("Phone:");
[Link](20, yoffset, 80, labelHeight);
phoneField = new JFormattedTextField(createFormatter("##########"));
[Link](100, yoffset, 150, fieldHeight);
addPlaceholder(phoneField, "Enter Phone Number");
yoffset = yoffset + gapHeight + labelHeight;
emailLabel = new JLabel("Email:");
[Link](20, yoffset, 80, labelHeight);
emailField = new JTextField();
[Link](100, yoffset, 150, fieldHeight);
addPlaceholder(emailField, "Enter Email");
yoffset = yoffset + gapHeight + labelHeight;
addressLabel = new JLabel("Address:");
[Link](20, yoffset, 80, labelHeight);
addressField = new JTextField();
[Link](100, yoffset, 150, fieldHeight);
addPlaceholder(addressField, "Enter Address");
yoffset = yoffset + gapHeight + labelHeight;
// Register button
registerButton = new JButton("Register");
[Link](100, yoffset, 150, buttonHeight);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (validateFields()) {
[Link](null, "Registration Successful!");
} else {
[Link](null, "Please fill out all fields
correctly.");
}
}
});
// Adding components to the panel
[Link](firstNameLabel);
[Link](firstNameField);
[Link](lastNameLabel);
[Link](lastNameField);
[Link](dobLabel);
[Link](dobField);
[Link](genderLabel);
[Link](maleRadioButton);
[Link](femaleRadioButton);
[Link](phoneLabel);
[Link](phoneField);
[Link](emailLabel);
[Link](emailField);
[Link](addressLabel);
[Link](addressField);
[Link](registerButton);
// Adding panel to the frame
add(panel);
// Make the frame visible
setVisible(true);
}
private void addPlaceholder(JTextComponent textComponent, String placeholder)
{
[Link](placeholder);
[Link]([Link]);
[Link](new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if ([Link]().equals(placeholder)) {
[Link]("");
[Link]([Link]);
}
}
@Override
public void focusLost(FocusEvent e) {
if ([Link]().isEmpty()) {
[Link]([Link]);
[Link](placeholder);
}
}
});
}
private boolean validateFields() {
if ([Link]().isEmpty() || [Link]().equals("Enter
First Name") ||
[Link]().isEmpty() || [Link]().equals("Enter
Last Name") ||
[Link]().isEmpty() || [Link]().equals("yyyy-MM-dd") ||
[Link]().isEmpty() || [Link]().equals("Enter Phone
Number") ||
[Link]().isEmpty() || [Link]().equals("Enter Email") ||
[Link]().isEmpty() || [Link]().equals("Enter
Address") ||
[Link]() == null) {
return false;
}
String phoneText = [Link]();
if () {
[Link](this, "Phone number must be exactly 10
digits.");
return false;
}
String emailText = [Link]();
String emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com$";
if () {
[Link](this, "Please enter a valid email address
ending with '.com'.");
return false;
}
return true;
}
private MaskFormatter createFormatter(String s) {
MaskFormatter formatter = null;
try {
formatter = new MaskFormatter(s);
[Link](false);
} catch (ParseException e) {
[Link]();
}
return formatter;
}
private static class DateFormatter extends [Link]
{
private final String datePattern = "yyyy-MM-dd";
private final SimpleDateFormat dateFormatter = new
SimpleDateFormat(datePattern);
@Override
public Object stringToValue(String text) throws ParseException {
return [Link](text);
}
@Override
public String valueToString(Object value) throws ParseException {
if (value != null) {
Date date = (Date) value;
return [Link](date);
}
return "";
}
}
public static void main(String[] args) {
// Run the form
[Link](new Runnable() {
@Override
public void run() {
new RegistrationForm();
}
});
}
}
Output:
Write a program to draw line, rectangle, circle and ellipse using graphics in
swing.
import [Link].*;
import [Link];
class Main extends Canvas{ public void paint(Graphics g) {
setBackground([Link]); setForeground([Link]);
[Link](20,20,80,80);
[Link](100,100,100,100);
[Link](200, 200, 100, 100);
}
public static void main(String[] args) {
Main m=new Main();
JFrame f=new JFrame("Shapes in swing");
[Link](m);
[Link](400,400);
[Link](true);
}
}
OUTPUT :
Write a swing program to read two input from user using input dialog and
sum
those values and display it using message dialog.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
class Addition extends JFrame implements ActionListener{
JLabel label1, label2, label3;
JTextField jt1, jt2;
JButton submit;
Addition(){
setTitle("User input");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
[Link](null);
label1 = new JLabel("Enter 1st num:");
[Link](20,50,100,20);
[Link](label1);
jt1=new JTextField();
[Link](130,50,100,20);
[Link](jt1);
label2 = new JLabel("Enter 2nd num:");
[Link](20,100,100,20);
[Link](label2);
jt2=new JTextField();
[Link](130,100,100,20);
[Link](jt2);
label3 = new JLabel("hello");
submit = new JButton("Calculate");
[Link](130,200,80,20);
[Link](submit);
[Link](this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
int a = [Link]([Link]());
int b = [Link]([Link]());
int c = 0;
if ([Link]().equals(submit)) {
c = a + b;
[Link]([Link](c));
[Link](new JFrame(), label3);
}
}
}
class Main {
public static void main(String[] args) {
Addition a = new Addition();
}
}
Output:
Write a swing program to get two number input using two text field and
multiply it by clicking button and display it in third text field.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
class Mult extends JFrame implements ActionListener{
JLabel label1, label2, label3;
JTextField jt1, jt2;
JButton submit;
Mult(){
setTitle("User input");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
[Link](null);
label1 = new JLabel("Enter First number:");
[Link](20,50,100,20);
[Link](label1);
jt1=new JTextField();
[Link](130,50,100,20);
[Link](jt1);
label2 = new JLabel("Enter Second number:");
[Link](20,100,100,20);
[Link](label2);
jt2=new JTextField();
[Link](130,100,100,20);
[Link](jt2);
label3 = new JLabel("Total is ");
[Link](20, 150, 100 ,20);
[Link](label3);
submit = new JButton("Calculate");
[Link](150,200,90,30);
[Link](submit);
[Link](this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if([Link]().isEmpty()){
[Link](new JFrame(), "please enter first number");
return;
}
if([Link]().isEmpty()){
[Link](new JFrame(), "please enter second number");
return;
}
int a = [Link]([Link]());
int b = [Link]([Link]());
int c = 0;
if ([Link]().equals(submit)) {
c = a * b;
[Link]("Total is: " + c);
}
}
}
class Main {
public static void main(String[] args) {
Mult m = new Mult();
}
}
OUTPUT :
Write a swing program to implement multiple mouse event.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
class Main {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public Main(){
prepareGUI();
}
public static void main(String[] args){
Main swingListenerDemo = new Main();
[Link]();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
[Link](400,400);
[Link](new GridLayout(3, 1));
headerLabel = new JLabel("",[Link] );
statusLabel = new JLabel("",[Link]);
[Link](350,100);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
[Link](0);
}
});
controlPanel = new JPanel();
[Link](new FlowLayout());
[Link](headerLabel);
[Link](controlPanel);
[Link](statusLabel);
[Link](true);
}
private void showMouseListenerDemo(){
[Link]("Listener in action: MouseListener");
JPanel panel = new JPanel();
[Link]([Link]);
[Link](new FlowLayout());
[Link](new CustomMouseListener());
JLabel msglabel =
new JLabel("Welcome to Mouse Events .",[Link]);
[Link](msglabel);
[Link](new CustomMouseListener());
[Link](msglabel);
[Link](panel);
[Link](true);
}
class CustomMouseListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked: ("+[Link]()+", "+[Link]() +")");
}
public void mousePressed(MouseEvent e) {
[Link]("Mouse pressed: ("+[Link]()+", "+[Link]() +")");
}
public void mouseReleased(MouseEvent e) {
[Link]("Mouse released: ("+[Link]()+", "+[Link]() +")");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse entered: ("+[Link]()+", "+[Link]() +")");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse exit: ("+[Link]()+", "+[Link]() +")");
}
}
}
OUTPUT:
Write a swing program to implement layout manager.
Border layout
import [Link].*;
import [Link].*;
class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
[Link](b1, [Link]); // b1 will be placed in the North Direction
[Link](b2, [Link]); // b2 will be placed in the South Direction
[Link](b3, [Link]); // b2 will be placed in the East Direction
[Link](b4, [Link]); // b2 will be placed in the West Direction
[Link](b5, [Link]); // b2 will be placed in the Center
[Link](300, 300);
[Link](true);
}
public static void main(String[] args) {
new Border();
}
}
OUTPUT :
Write a swing program to check login validation by using database.
[Link] file
package [Link];
import [Link].*;
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
class Myframe extends JFrame implements ActionListener{
JLabel labelName, labelEmail,labelFaculty,labelPassword;
JTextField t1,t2,t3,t4;
JButton submit , Login;
private String dbURL = "jdbc:mysql://localhost:3306/validation";
private String username = "root";
private String dbpassword = "";
private Connection conn;
String tabelName = "STUDENTS";
Myframe(){
setTitle("Register");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
[Link](null);
labelName = new JLabel("name");
[Link](20,50,100,20);
[Link](labelName);
t1=new JTextField();
[Link](130,50,100,20);
[Link](t1);
labelEmail = new JLabel("email");
[Link](20,100,100,20);
[Link](labelEmail);
t2=new JTextField();
[Link](130,100,100,20);
[Link](t2);
labelFaculty = new JLabel("faculty");
[Link](20,150,100,20);
[Link](labelFaculty);
t3=new JTextField();
[Link](130,150,100,20);
[Link](t3);
labelPassword = new JLabel("password");
[Link](20,200,100,20);
[Link](labelPassword);
t4=new JTextField();
[Link](130,200,100,20);
[Link](t4);
submit = new JButton("Submit");
[Link](150,350,80,20);
[Link](submit);
Login = new JButton("Login");
[Link](250,350,80,20);
[Link](Login);
[Link](this);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SignIn lg = new SignIn();
}
});
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = [Link]();
String email = [Link]();
String faculty = [Link]();
String password = [Link]();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
boolean check_email = [Link](emailPattern);
if([Link]()|| [Link]()) {
[Link](new JFrame(), "Please Enter Your Name");
return;
}
if([Link]() || [Link]()){
[Link](new JFrame(), "Please Enter Your email");
return;
}else if(!check_email){
[Link](new JFrame(), "Please Enter valid email
format");
return;
}
if([Link]() || [Link]()){
[Link](new JFrame(), "Please Enter your faculty");
return;
}
if([Link]() || [Link]()){
[Link](new JFrame(), "Please Enter your
password");
return;
}
try{
[Link]("[Link]");
conn = [Link](dbURL,username,dbpassword);
if(conn !=null){
[Link]("successfully connected");
}else [Link](" connection Failed");
String InsertQ ="INSERT INTO " + tabelName +
"(name,email,faculty,password)
VALUES('"+name+"','"+email+"','"+faculty+"','"+password+"')";
Statement insertTable = [Link]();
[Link](InsertQ);
[Link]("Inserted Successfully");
}catch(ClassNotFoundException ex){
[Link]();
}catch (SQLException ex){
[Link]();
}
}
}
public class Main {
public static void main(String[] args){
Myframe myframe = new Myframe();
}
}
[Link] file
package [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link].*;
public class SignIn extends JFrame {
JLabel labelEmail, labelPassword;
JTextField t1,t2;
JButton submit;
private String dbURL = "jdbc:mysql://localhost:3306/validation";
private String username = "root";
private String dbpassword = "";
private Connection conn;
String tabelName = "STUDENTS";
public SignIn(){
setTitle("Login");
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container c = getContentPane();
[Link](null);
labelEmail = new JLabel("email");
[Link](20,50,100,20);
[Link](labelEmail);
t1=new JTextField();
[Link](130,50,100,20);
[Link](t1);
labelPassword = new JLabel("password");
[Link](20,100,100,20);
[Link](labelPassword);
t2=new JTextField();
[Link](130,100,100,20);
[Link](t2);
submit = new JButton("Login");
[Link](150,200,80,20);
[Link](submit);
setVisible(true);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String email = [Link]();
String password = [Link]();
if([Link]() || [Link]()){
[Link](new JFrame(), "Please Enter Your email");
return;
}
if([Link]() || [Link]()){
[Link](new JFrame(), "Please Enter your password");
return;
}
try{
[Link]("[Link]");
conn = [Link](dbURL,username,dbpassword);
if(conn !=null){
[Link]("successfully connected");
}else [Link](" connection Failed");
Statement st = [Link]();
String select = "select password from students where email = '"+[Link]()+"'"; ResultSet rs =
[Link](select);
String get_password = "";
while ([Link]()){
get_password = [Link](1);
}
if(get_password.equals([Link]())){
[Link](new JFrame(), "Login Success");
}else{
[Link](new JFrame(), "email or password is incorrect");
}
}catch(ClassNotFoundException ex){
[Link]();
}catch (SQLException ex){
[Link]();
}
}
});
}
}
OUTPUT :