0% found this document useful (0 votes)
54 views

Adv Java File

The document contains a list of 13 programming assignments related to Java concepts like constructor overloading, multithreading, database connectivity, remote method invocation, Swing components like JTree and JTable. Sample code is provided for the first 6 assignments on constructor overloading, multithreading, database connectivity using JDBC and MySQL, remote method invocation and creating a tree using JTree. The outputs of the sample programs are also displayed.

Uploaded by

praveenverma1990
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Adv Java File

The document contains a list of 13 programming assignments related to Java concepts like constructor overloading, multithreading, database connectivity, remote method invocation, Swing components like JTree and JTable. Sample code is provided for the first 6 assignments on constructor overloading, multithreading, database connectivity using JDBC and MySQL, remote method invocation and creating a tree using JTree. The outputs of the sample programs are also displayed.

Uploaded by

praveenverma1990
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

14418

INDEX
S.No

Program

1.

Write a program for Constructor Overloading

2.

Write a program for multithreading in java.

3.

Write a program of Java Database Connectivity using MySQL.

4.

Write a program to show Remote Method Invocation

5.

Write a program to create tree using JTree class of swings

6.

Write a program to draw table using JTable class of swings

7.

Write a program to create a List using JList class of swings

8.

WAP to draw graphics using applet.

9.

Write a servlet program using Netbeans

10.

Write a program to make a calculator

11.

Write a program to draw Styled text components using swings.

12.

Write a program to show functionalities using AWT

13.

Write a program to make a bean component using java bean.

Date

Signature

14418

Practical :1
Write a program in java for constructor overloading
class Maths
{
Maths(double a, double b)
{
double sum;
sum=a+b;
System.out.println(Addition=+sum);
}
Maths(int a, int b)
{
int sum;
sum=a+b;
System.out.println(Addition=+sum);
}
}
class M
{
public static void main(String args[])
{
Maths m=new Maths(4.0,5.8);
Maths n=new Maths(7.6,3.4);
}
}

14418

OUTPUT:-

14418

Practical :2
Write a program in java to show multithreading.
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
if(i==1)
yield();
System.out.println(\t from thread A:i=+i);
}
System.out.println(\t exit from A);
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println(\t from thread B:j=+j);
if(j==3)
stop();
System.out.println(\t from thread B:j=+j);
}
4

14418

System.out.println(\t exit from B);}


}
class C extends Thread
{
Public void run()
{
for(int k=1;k<=5;k++)
{System.out.println(\t from thread C:k=+k);}
System.out.println(\t exit from C);
}
}
class ThreadMethods
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
System.out.println(\t start thread A);
threadA.start();
System.out.println(\t start thread B);
threadB.start();
System.out.println(\t start thread C);
threadC.start();
System.out.println(\t END OF MAIN OF THREAD);
}
}
5

14418

OUTPUT:-

14418

Practical :3
Write a program of Java Database Connectivity using MySQL.
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class connect extends javax.swing.JFrame {
public connect() {
initComponents();
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
t1 = new javax.swing.JTable();
b1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
t1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"id", "username", "password"
}
jScrollPane1.setViewportView(t1);
b1.setText("fetch data");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

14418

b1ActionPerformed(evt); }});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
DING)

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA

.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452,
javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup(
.addGap(216, 216, 216)
.addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(932, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109,
javax.swing.GroupLayout.PREFERRED_SIZE
.addGap(35, 35, 35)
.addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 67,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(415, Short.MAX_VALUE))
);
pack();
8

14418

}// </editor-fold>
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel)t1.getModel();
try{
Class.forName("java.sql.Driver");
Connection conn =
(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/userlogin?
zeroDateTimeBehavior=convertToNull","root","abc");
Statement st = conn.createStatement();
String query;
query = "select * from user;";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
String d1 = rs.getString("id");
String d2 = rs.getString("username");
String d3 = rs.getString("password");
model.addRow(new Object[]{d1,d2
rs.close();
st.close();
conn.close();
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, "error in connectivity");
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
try {
9

14418

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels())
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}}} catch (ClassNotFoundException ex)
{
java.util.logging.Logger.getLogger(connect.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(connect.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(connect.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex)
{ java.util.logging.Logger.getLogger(connect.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} //</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new connect().setVisible(true)
}
});}
private javax.swing.JButton b1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable t1;
}

10

14418

Output:

11

14418

Practical :4
Wap to show Remote Method Invocation
INTERFACE
package interface2;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Interface2 extends Remote {
public int add(int n1,int n2) throws RemoteException;
public int sub(int n1,int n2) throws RemoteException;
}
IMPLEMENTATION
package implementation2;
import interface2.Interface2;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Implementation2 extends UnicastRemoteObject implements Interface2 {
public Implementation2() throws RemoteException{
}
@Override
public int add(int n1, int n2) throws RemoteException{
return n1+n2;
}
@Override
public int sub(int n1, int n2) throws RemoteException {
12

14418

return n1-n2;
}
}
SERVER
package server2;

import implementation2.Implementation2;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server2 {
public static void main(String[] args) throws RemoteException {
try{
Registry reg = LocateRegistry.createRegistry(1099);
Implementation2 c = new Implementation2();
reg.rebind("myObj", c);
System.out.println("Server is ready");
}catch(Exception ex){
ex.printStackTrace();
}
}}
CLIENT
package client2;
import interface2.Interface2;
import java.rmi.registry.Registry;
13

14418

import java.rmi.registry.LocateRegistry;
public class Client2 {
public static void main(String[] args) {
try{
Registry reg1 = LocateRegistry.getRegistry("127.0.0.1",1099);
Interface2 obj1 = (Interface2)reg1.lookup("myObj");
System.out.println("1+1="+ obj1.add(1,1));
System.out.println("10-1="+ obj1.sub(10,1));
}catch(Exception e){
e.printStackTrace();
}
}
}

14

14418

OUTPUT:

15

14418

Practical :5
Write a program to create tree using JTree class of swings
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class SimpleTree1
{
public static void main(String[] args)
{
Frame frame = new SimpleTreeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class SimpleTreeFrame extends JFrame
{
public SimpleTreeFrame()
{
setTitle("SimpleTree");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state= new DefaultMutableTreeNode("California");
16

14418

country.add(state);
DefaultMutableTreeNode city= new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city);
JTree tree = new JTree(root);
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(tree));
}
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
}

17

14418

Output:

18

14418

Practical :6
Write a program to draw table using JTable class of swings
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class PlanetTable
{
public static void main(String[] args)
{
JFrame frame = new PlanetTableFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class PlanetTableFrame extends JFrame
{
public PlanetTableFrame()
{
setTitle("PlanetTable")
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
final JTable table = new JTable(cells, columnNames);
add(new JScrollPane(table), BorderLayout.CENTER);
JButton printButton = new JButton("Print");
19

14418

printButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
table.print();
}
catch (java.awt.print.PrinterException e)
{
e.printStackTrace();
}
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(printButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private Object[][] cells =
{
{ "Mercury", 2440.0, 0, false, Color.yellow },
{ "Venus", 6052.0, 0, false, Color.yellow },
{ "Earth", 6378.0, 1, false, Color.blue },
{ "Mars", 3397.0, 2, false, Color.red },
20

14418

{ "Jupiter", 71492.0, 16, true, Color.orange },


{ "Saturn", 60268.0, 18, true, Color.orange },
{ "Uranus", 25559.0, 17, true, Color.blue },
{ "Neptune", 24766.0, 8, true, Color.blue },
{ "Pluto", 1137.0, 1, false, Color.black }
};
private String[] columnNames = { "Planet", "Radius", "Moons", "Gaseous", "Color" };
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 200;
}

21

14418

Output:

22

14418

Practical: 7
Write a program to create a List using JList class of swings
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class List


{
public static void main(String[] args)
{
JFrame frame = new ListFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ListFrame extends JFrame
{
public ListFrame()
{
setTitle("ListTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

String[] words =
{
23

14418

"quick","brown","hungry","wild","silent",
"huge","private","abstract","static","final"
};

wordList = new JList(words);


wordList.setVisibleRowCount(4);
JScrollPane scrollPane = new JScrollPane(wordList);

listPanel = new JPanel();


listPanel.add(scrollPane);
wordList.addListSelectionListener(new
ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
Object[] values = wordList.getSelectedValues();

StringBuilder text = new StringBuilder(prefix);


for (int i = 0; i < values.length; i++)
{
String word = (String) values[i];
text.append(word);
text.append(" ");
}
text.append(suffix);
24

14418

label.setText(text.toString());
}
});

buttonPanel = new JPanel();


group = new ButtonGroup();
makeButton("Vertical", JList.VERTICAL);
makeButton("Vertical Wrap", JList.VERTICAL_WRAP);
makeButton("Horizontal Wrap", JList.HORIZONTAL_WRAP);

add(listPanel, BorderLayout.NORTH);
label = new JLabel(prefix + suffix);
add(label, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}

private void makeButton(String label, final int orientation)


{
JRadioButton button = new JRadioButton(label);
buttonPanel.add(button);
if (group.getButtonCount() == 0) button.setSelected(true);
group.add(button);
button.addActionListener(new
ActionListener()
25

14418

{
public void actionPerformed(ActionEvent event)
{
wordList.setLayoutOrientation(orientation);
listPanel.revalidate();
}
});
}

private static final int DEFAULT_WIDTH = 400;


private static final int DEFAULT_HEIGHT = 300;
private JPanel listPanel;
private JList wordList;
private JLabel label;
private JPanel buttonPanel;
private ButtonGroup group;
private String prefix = "The ";
private String suffix = "fox jumps over the lazy dog.";
}

26

14418

OUTPUT:

27

14418

Practical: 8
WAP to draw graphics using applet.
Hello world.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;

public class HelloWorldApplet extends Applet


{
private Button go;
private TextField name;
private Label hello;
private ActionListener Listener;

@Override
public void init()
{

go = new Button("go");
name = new TextField();
hello = new Label("hello");

this.setLayout(new BorderLayout());
this.add(name, BorderLayout.NORTH);

28

14418

Panel center = new Panel();

center.add(go);

this.add(center,BorderLayout.CENTER);

this.add(hello,BorderLayout.SOUTH);

PrintHello listener = new PrintHello(hello, name);

go.addActionListener(Listener);

}
Print hello
import java.awt.*;
import java.awt.event.*;

class PrintHello implements ActionListener

{
private Label label;
private TextField textfield;
29

14418

public PrintHello(Label l, TextField t)


{
label = l;
textfield = t;
}
@Override
public void actionPerformed(ActionEvent ae)
{
String name = textfield.getText();
if(name!=null && !(name.equals("")));
{
label.setText("Welcome" +name);
}}}

30

14418

OUTPUT:

31

14418

Practical :9
Write a servlet program using Netbeans

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class jar2 extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("application/pdf");
ServletContext ctx = getServletContext();
InputStream isr = ctx.getResourceAsStream("/syllabus.pdf");
int read=0;
byte[] bytes = new byte[10000];
OutputStream os = response.getOutputStream();
while((read = isr.read(bytes)) !=-1)
{
os.write(bytes,0,read);
}
os.flush();
os.close();
}
}
32

14418

Compilation Code:
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\HelloServlet\WEBINF\classes>javac jar2.java
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\HelloServlet\WEBINF\classes>

33

14418

Output:

34

14418

Practical:10
Write a program to make a calculator
package calculator1;
/**
*
* @author Student
*/
public class calculator1 extends javax.swing.JFrame {
//variables
double plusminus;
double firstDouble;
double secondDouble;
double totalDouble;
//to check for button clicks
int plusClick;
int minusClick;
int multiplyClick;
int divideClick;
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
display = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
plus = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
minus = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
multiply = new javax.swing.JButton();
decimal = new javax.swing.JButton();
clear = new javax.swing.JButton();
posneg = new javax.swing.JButton();
equals = new javax.swing.JButton();
divide = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
35

14418

display.setEditable(false);
display.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
display.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
displayActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton4.setText("4");
plus.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
plus.setText("+");
jButton6.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton6.setText("5");
minus.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
minus.setText("-");
jButton8.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton8.setText("7");
jButton9.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton9.setText("6");
36

14418
jButton10.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton10.setText("9");
jButton11.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton11.setText("8");
jButton12.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
jButton12.setText("0");
multiply.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
multiply.setText("X");
decimal.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
decimal.setText(".");
clear.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
clear.setText("C");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
posneg.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
posneg.setText("+/-");
equals.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
equals.setText("=");
divide.setFont(new java.awt.Font("Arial", 1, 18)); // NOI18N
divide.setText("/");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE))
37

14418
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(21, 21, 21)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(posneg, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(display, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
38

14418
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(display, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(plus, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(minus, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(multiply, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(posneg, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE)
39

14418
.addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE, 62,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(38, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void displayActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void clearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
display.setText("");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton1.getText());
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton2.getText()); // TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
display.setText(display.getText()+jButton3.getText());
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
40

14418
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(calculator1.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(calculator1.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(calculator1.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(calculator1.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new calculator1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton clear;
private javax.swing.JButton decimal;
private javax.swing.JTextField display;
private javax.swing.JButton divide;
private javax.swing.JButton equals;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton minus;
private javax.swing.JButton multiply;
private javax.swing.JButton plus;
private javax.swing.JButton posneg;
// End of variables declaration
}
41

14418

OUTPUT:

42

14418

Practical:11
Write a program to draw Styled text components using swings package
editorpane

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
This program demonstrates how to display HTML documents
in an editor pane.
*/
public class EditorPaneTest
{
public static void main(String[] args)
{
JFrame frame = new EditorPaneFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
43

14418

/**
This frame contains an editor pane, a text field and button
25.

to enter a URL and load a document, and a Back button to

26.

return to a previously loaded document.

27. */
class EditorPaneFrame extends JFrame
{
public EditorPaneFrame()
{
setTitle("EditorPaneTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

final Stack<String> urlStack = new Stack<String>();


final JEditorPane editorPane = new JEditorPane();
final JTextField url = new JTextField(30);

// set up hyperlink listener

editorPane.setEditable(false);
editorPane.addHyperlinkListener(new
HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent event)
{
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
44

14418

{
try
{
// remember URL for back button
urlStack.push(event.getURL().toString());
// show URL in text field
url.setText(event.getURL().toString());
editorPane.setPage(event.getURL());
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
}
}
});

// set up checkbox for toggling edit mode

final JCheckBox editable = new JCheckBox();


editable.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
45

14418

editorPane.setEditable(editable.isSelected());
}
});

// set up load button for loading URL

ActionListener listener = new


ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
// remember URL for back button
urlStack.push(url.getText());
editorPane.setPage(url.getText());
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
}
};

JButton loadButton = new JButton("Load");


46

14418

loadButton.addActionListener(listener);
url.addActionListener(listener);

// set up back button and button action

JButton backButton = new JButton("Back");


backButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (urlStack.size() <= 1) return;
try
{
// get URL from back button
urlStack.pop();
// show URL in text field
String urlString = urlStack.peek();
url.setText(urlString);
editorPane.setPage(urlString);
}
catch (IOException e)
{
editorPane.setText("Exception: " + e);
}
47

14418

}
});
add(new JScrollPane(editorPane), BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.add(new JLabel("URL"));
panel.add(url);
panel.add(loadButton);
panel.add(backButton);
panel.add(new JLabel("Editable"));
panel.add(editable);
add(panel, BorderLayout.SOUTH);
}
private static final int DEFAULT_WIDTH = 600;
private static final int DEFAULT_HEIGHT = 400;
}

48

14418

OUTPUT:

49

14418

Practical: 12
Write a program to show functionalities using AWT
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

/**
. This program demonstrates the use of a clip shape.
*/
public class ClipTest
{
public static void main(String[] args)
{
JFrame frame = new ClipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

/**
This frame contains a checkbox to turn a clip off
.

and on, and a panel to draw a set of lines with or without

50

14418

clipping.
*/
class ClipTestFrame extends JFrame
{
public ClipTestFrame()
{
setTitle("ClipTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

final JCheckBox checkBox = new JCheckBox("Clip");


checkBox.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
panel.repaint();
}
});
add(checkBox, BorderLayout.NORTH);

panel = new
JPanel()
{
public void paintComponent(Graphics g)
{
51

14418

super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;

if (clipShape == null) clipShape = makeClipShape(g2);

g2.draw(clipShape);

if (checkBox.isSelected()) g2.setClip(clipShape);

// draw line pattern


final int NLINES = 50;
Point2D p = new Point2D.Double(0, 0);
for (int i = 0; i < NLINES; i++)
{
double x = (2 * getWidth() * i) / NLINES;
double y = (2 * getHeight() * (NLINES - 1 - i)) / NLINES;
Point2D q = new Point2D.Double(x, y);
g2.draw(new Line2D.Double(p, q));
}}
};
add(panel, BorderLayout.CENTER);
}
Shape makeClipShape(Graphics2D g2)
{
FontRenderContext context = g2.getFontRenderContext();
52

14418

Font f = new Font("Serif", Font.PLAIN, 100);


GeneralPath clipShape = new GeneralPath();
TextLayout layout = new TextLayout("Hello", f, context);
AffineTransform transform = AffineTransform.getTranslateInstance(0, 100);
Shape outline = layout.getOutline(transform);
clipShape.append(outline, false);
layout = new TextLayout("World", f, context);
transform = AffineTransform.getTranslateInstance(0, 200);
outline = layout.getOutline(transform);
clipShape.append(outline, false);
return clipShape;
}
private JPanel panel;
private Shape clipShape;
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
}

53

14418

OUTPUT:

54

14418

Practical:13
Write a program to make a bean component using java bean
package com.horstmann.corejava;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
A bean for viewing an image.
public class ImageViewerBean extends JLabel
public ImageViewerBean()
{
setBorder(BorderFactory.createEtchedBorder());
}
public void setFileName(String fileName)
{
try
{
file = new File(fileName);
setIcon(new ImageIcon(ImageIO.read(file)));
}
catch (IOException e)
{
file = null;
setIcon(null);
}
}
public String getFileName()
{
if (file == null) return null;
else return file.getPath();
}
public Dimension getPreferredSize()
{
return new Dimension(XPREFSIZE, YPREFSIZE);
}
private File file = null;
private static final int XPREFSIZE = 200;
private static final int YPREFSIZE = 200;
}
55

14418

OUTPUT:

56

You might also like