You are on page 1of 15

Samarpan Academy

Dhumbarahi, Kathmandu

Tribhuvan University
Faculty of Humanities and Social Science

Lab 2

Course Code: CACS354


Semester: VI

Submitted To:
Department of Computer Application
Samarpan Academy
In partial fulfillment of the requirement for the Bachelor in Computer Application

Submitted By:
Sandesh Tramu Thapa
Reg. No.: 6-2-1200-6-2018
1. Write a swing program to read two input from user using input dialog and sum those values
and display it using message dialog.

package guisumapp;

import java.awt.FlowLayout;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class GUISumApp {

public static void main(String[] args) {

TextField f = new TextField();

String a1 = JOptionPane.showInputDialog(null,"Enter first number");

String b1=JOptionPane.showInputDialog(null,"Enter second number");

int a = Integer.parseInt(a1);

int b = Integer.parseInt(b1);

int sum= a+b;

JOptionPane.showMessageDialog(null,"The total sum is "+ sum);

Output:
2. Write a swing program to create Menus.
package menubar;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
public class MenuBar {
public static void main(String[] args) {
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
}
Output:
3.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.
package multiply_textfield;
import javax.swing.*;
import java.awt.event.*;
public class Multiply_textField {
// Initialization
JFrame f;
JTextField t1,t2,t3;
JButton btn;
Multiply_textField() //Creating Constructor
{
f = new JFrame("Multipication");
t1 = new JTextField();
t1.setBounds(30,50,120,30); //border size
t2 = new JTextField();
t2.setBounds(30,90,120,30);
t3 = new JTextField();
t3.setBounds(30,130,120,30);
btn = new JButton("Multiply");
btn.setBounds(30,160,80,40);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String a = t1.getText();
String b = t2.getText();
int c = Integer.parseInt(a); //To change strings into Integers
int d = Integer.parseInt(b);
int result = c*d;
t3.setText(String.valueOf(result)); // as result is integer value we cane't set directly
}
});
f.add(t1); f.add(t2); f.add(t3); f.add(btn);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new Multiply_textField(); //calling Constructor
}
}
Output:
4. Write a swing program to connect database and display student details in JTable.
import javax.swing.*;    
public class TableExample {    
   JFrame f;    
    TableExample(){    
   f=new JFrame();    
    String data[][]={ {"101","Amit","670000"},    
                          {"102","Jai","780000"},    
                         {"101","Sachin","700000"}};    
   String column[]={"ID","NAME","SALARY"};         
    JTable jt=new JTable(data,column);    
    jt.setBounds(30,40,200,300);          
    JScrollPane sp=new JScrollPane(jt);    
   f.add(sp);          
  f.setSize(300,400);    
    f.setVisible(true);    
}     
public static void main(String[] args) {    
    new TableExample();    
}    
}  
Output:
5. Write a database program to implementing RowSet.
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.Statement;  
import javax.sql.RowSetEvent;  
import javax.sql.RowSetListener;  
import javax.sql.rowset.JdbcRowSet;  
import javax.sql.rowset.RowSetProvider;  
public class RowSetExample {  
        public static void main(String[] args) throws Exception {  
                Class.forName("oracle.jdbc.driver.OracleDriver");  
    //Creating and Executing RowSet  
        JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();  
        rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");  
        rowSet.setUsername("system");  
        rowSet.setPassword("oracle");  
        rowSet.setCommand("select * from emp400");  
        rowSet.execute();       
    while (rowSet.next()) {  
                        // Generating cursor Moved event  
                        System.out.println("Id: " + rowSet.getString(1));  
                        System.out.println("Name: " + rowSet.getString(2));  
                        System.out.println("Salary: " + rowSet.getString(3));  
              }  
   }  
}  
Output:
6. Write a database program to execute query and get results from table using prepare statement.

package examplepreparedst;
import java.sql.*;
public class ExamplePreparedSt {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/students","root","");
PreparedStatement st = con.prepareStatement("insert into Emp values(?,?)");
st.setInt(1, 125);
st.setString(2, "Bishal");
int i = st.executeUpdate();
System.out.println(i + " records inserted");
con.close();
} catch (Exception e) {
System.out.println(e);
} }
Output:

id Name
101 Mahesh
125 Bishal
225 Ravi
320 Shiva
7. Write a swing program to implement multiple mouse event.

package mouseevent;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class MouseEventExample {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,200);
// frame.dispose();
frame.setTitle("Calculation");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
//Jtextfield
JTextField t1,t2,t3;
t1=new JTextField(20);
t2=new JTextField(20);
t3=new JTextField(20);
frame.add(t1);
frame.add(t2);
frame.add(t3);

t3.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int c = a+b;
t3.setText(String.valueOf(c));
}
public void mouseReleased(MouseEvent e){
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int c = a-b;
t3.setText(String.valueOf(c));
}
});
}
}
Output:
When button is pressed:

When button is released:


8. Write a swing program to implement layout manager.

package layoutexample;
import java.awt.*;
import javax.swing.*;
public class LayoutExample {

public static void main(String[] args) {


JFrame f = new JFrame("Layout Display");
f.setSize(750, 750);
JButton btn1 = new JButton("NORTH");
JButton btn2 = new JButton("SOUTH");
JButton btn3 = new JButton("EAST");
JButton btn4 = new JButton("WEST");
JButton btn5 = new JButton("CENTER");
f.add(btn1);
f.add(btn2);
f.add(btn3);
f.add(btn4);
f.add(btn5);

//FlowLayout
f.setLayout(new FlowLayout());

//Grid Layout
f.setLayout(new GridLayout(5,5));
//Border Layout
f.setLayout(new BorderLayout(30, 20));
f.add(btn1, BorderLayout.NORTH);
f.add(btn2, BorderLayout.SOUTH);
f.add(btn3, BorderLayout.EAST);
f.add(btn4, BorderLayout.WEST);
f.add(btn5, BorderLayout.CENTER);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Output:
Border Layout Grid Layout

Flow Layout

You might also like