java

java

Saturday 10 December 2011

frame





the Given Program that accepts a login name and password from the user. The application contains two buttons. The 'Log In 'button when clicked, checks the user name and password entered in the text box, and the ‘cancel’ button will terminate the application. The user is excepted to enter both username and password before clicking the Log In button.
If the correct username and password is entered by the user, then a new frame is opened where product rate and quantity are displayed. The product is a combo box which has 5 options :

Product 1
Product 2
Product 3
Product 4
Product 5

On selecting product 1 the rate displayed on the text field will be 150, similarly product 2, product 3, product 4 and product5 will display rate as 250, 350, 450 and 550 respectively. The rate text field will be Editable false i.e. the contents of rate box cannot be edited. The select the respective Quantity for the product. On clicking the enter button again a new frame will be opened where all the components are editable false and a new text field is displayed where the total amount is displayed. In addition to this a menu bar called ‘file’ is also displayed on the container which has two menu items new and exit, on clicking new the log in page is opened and on clicking exit the application is terminated.

The username and password in the given program
username: akhtar
password: blank
*/

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author akhtar
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author akhtar
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Admin extends JFrame implements ActionListener
{
JTextField jft1,jft2;
JButton b1,b2;
Container cont;

Admin()
{
cont = getContentPane();
cont.setLayout(null);

JLabel jl1=new JLabel("Username:");
cont.add(jl1);
jl1.setBounds(20,30,100,20);

jft1=new JTextField();
cont.add(jft1);
jft1.setBounds(100,30,200,30);

JLabel jl2=new JLabel("Password:");
cont.add(jl2);
jl2.setBounds(20,80,100,20);

jft2=new JTextField();
cont.add(jft2);
jft2.setBounds(100,80,200,30);

b1=new JButton("Sign In");
cont.add(b1);
b1.setBounds(75,150,100,30);
b1.addActionListener(this);

b2=new JButton("Cancel");
cont.add(b2);
b2.setBounds(200,150,100,30);
b2.addActionListener(this);
}

public static void main(String args[])
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == b2)
System.exit(0);

String user=jft1.getText();
String password=jft2.getText();

if(user.equals("akhtar")&&password.equals("blank"))
{
Admin2 jf=new Admin2();
jf.setSize(400,300);
jf.setVisible(true);
}
else
JOptionPane.showMessageDialog(cont,"Invalid Details","Lionel's Message",

JOptionPane.ERROR_MESSAGE);
}
}

class Admin2 extends JFrame implements ActionListener,ItemListener
{
JComboBox jc;
JTextField ft1,ft2,ft3;
JButton B1;
String z,t;
Admin2()
{
Container cont=getContentPane();
cont.setLayout(null);

JLabel l1=new JLabel("Product:");
cont.add(l1);
l1.setBounds(20,30,100,20);

jc=new JComboBox();
jc.addItem("product1");
jc.addItem("product2");
jc.addItem("product3");
jc.addItem("product4");
jc.addItem("product5");
cont.add(jc);
jc.setBounds(80,30,200,20);
jc.addItemListener(this);

JLabel l2=new JLabel("Rate:");
cont.add(l2);
l2.setBounds(20,60,50,20);

ft1=new JTextField();
cont.add(ft1);
ft1.setEditable(false);
ft1.setBounds(80,60,50,20);

JLabel l3=new JLabel("Quantity:");
cont.add(l3);
l3.setBounds(20,90,100,20);

ft2=new JTextField();
cont.add(ft2);
ft2.setBounds(80,90,50,20);

B1=new JButton("Enter");
cont.add(B1);
B1.setBounds(150,150,100,30);
B1.addActionListener(this);
}

public void actionPerformed(ActionEvent ne)
{
String m=ft1.getText();
int a=Integer.parseInt(m);
String n=ft2.getText();
int b=Integer.parseInt(n);
z=jc.getSelectedItem().toString();

int tot=a*b;
t = String.valueOf(tot);

Admin3 admn3=new Admin3(z,m,n,t);
admn3.setSize(400,400);
admn3.setVisible(true);
}

public void itemStateChanged(ItemEvent me)
{
z=jc.getSelectedItem().toString();
if(z.equals("product1"))
ft1.setText("150");
else if(z.equals("product2"))
ft1.setText("250");
else if(z.equals("product3"))
ft1.setText("350");
else if(z.equals("product4"))
ft1.setText("450");
else if(z.equals("product5"))
ft1.setText("550");
}
}

class Admin3 extends JFrame implements ActionListener
{
JTextField ftf1,ftf2,ftf3,ftf0;
String x,y,z,q;
int l,m,n,tot1;
JMenuBar bar= new JMenuBar();
JMenuItem it1,it2;

Admin3(String l,String m,String n,String t)
{
Container cont=getContentPane();
cont.setLayout(null);

JMenu file = new JMenu ("File");
it1 = new JMenuItem("New");
it2 = new JMenuItem("Exit");
file.add(it1);
file.add(it2);

it1.addActionListener(this);
it2.addActionListener(this);

bar.add(file);
setJMenuBar(bar);

JLabel lb1=new JLabel("Product:");
cont.add(lb1);
lb1.setBounds(20,30,100,20);

ftf0=new JTextField();
ftf0.setEditable(false);
ftf0.setBounds(80,30,200,20);
cont.add(ftf0);
ftf0.setText(l);

JLabel lb2=new JLabel("Rate:");
cont.add(lb2);
lb2.setBounds(20,60,50,20);

ftf1=new JTextField();
cont.add(ftf1);
ftf1.setEditable(false);
ftf1.setBounds(80,60,50,20);
ftf1.setText(m);

JLabel lb3=new JLabel("Quantity:");
cont.add(lb3);
lb3.setBounds(20,90,100,20);

ftf2=new JTextField();
cont.add(ftf2);
ftf2.setEditable(false);
ftf2.setBounds(80,90,50,20);
ftf2.setText(n);

JLabel lb4=new JLabel("Total Amt");
cont.add(lb4);
lb4.setBounds(20,120,100,20);

ftf3=new JTextField();
cont.add(ftf3);
ftf3.setEditable(false);
ftf3.setBounds(80,120,100,20);
ftf3.setText(t);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==it1)
{
Admin admn=new Admin();
admn.setSize(400,400);
admn.setVisible(true);
}

if(ae.getSource()==it2)
System.exit(0);javascript:void(0)
}
}


/* Kindly leave your useful comments or suggesstin if any. */

java program

Class JProgressBar
java.lang.Object
extended byjava.awt.Component
extended byjava.awt.Container
extended byjavax.swing.JComponent
extended byjavax.swing.JProgressBar


public class JProgressBar extends JComponent implements SwingConstants, Accessible

A component that, by default, displays an integer value within a bounded interval. A progress bar typically communicates the progress of some work by displaying its percentage of completion and possibly a textual display of this percentage.

To indicate that a task of unknown length is executing, you can put a progress bar into indeterminate mode. While the bar is in indeterminate mode, it animates constantly to show that work is occurring. As soon as you can determine the task's length and amount of progress, you should update the progress bar's value and switch it back to determinate mode.

Here is an example of creating a progress bar, where task is an object that returns information about the progress of some work:

progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);

Here is an example of updating the value of the progress bar:

progressBar.setValue(task.getCurrent());

Here is an example of putting a progress bar into indeterminate mode, and then switching back to determinate mode once the length of the task is known:

progressBar = new JProgressBar();
...//when the task of (initially) unknown length begins:
progressBar.setIndeterminate(true);
...//do some work; get length of task...
progressBar.setMaximum(newLength);
progressBar.setValue(newValue);
progressBar.setIndeterminate(false);





/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author akhtar
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ProgressBarr extends JFrame implements ActionListener
{
JButton jb;
JProgressBar jpb1;
int value =0;
ProgressBarr()
{
Container contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());

jb = new JButton("Click");
contentPane.add(jb);
jb.addActionListener(this);

jpb1 = new JProgressBar(0,1000);
contentPane.add(jpb1);
jpb1.setStringPainted(true);
}

public static void main(String args[])
{
ProgressBarr pb1 = new ProgressBarr();
pb1.setSize(500,500);
pb1.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
while(value<=1000)
{
value = value+10;
jpb1.setValue(value);
}
}
}

Wednesday 7 December 2011

What Is a Class?

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.
The following Bicycle class is one possible implementation of a bicycle:
class Bicycle {

       int cadence = 0;
       int speed = 0;
       int gear = 1;

       void changeCadence(int newValue) {
            cadence = newValue;
       }

       void changeGear(int newValue) {
            gear = newValue;
       }

       void speedUp(int increment) {
            speed = speed + increment;   
       }

       void applyBrakes(int decrement) {
            speed = speed - decrement;
       }

       void printStates() {
            System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);
       }
}
 

The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of bicycle objects. The fields cadence, speed, and gear represent the object's state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.
You may have noticed that the Bicycle class does not contain a main method. That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.
Here's a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:
class BicycleDemo {
     public static void main(String[] args) {

          // Create two different Bicycle objects
          Bicycle bike1 = new Bicycle();
          Bicycle bike2 = new Bicycle();

          // Invoke methods on those objects
          bike1.changeCadence(50);
          bike1.speedUp(10);
          bike1.changeGear(2);
          bike1.printStates();

          bike2.changeCadence(50);
          bike2.speedUp(10);
          bike2.changeGear(2);
          bike2.changeCadence(40);
          bike2.speedUp(10);
          bike2.changeGear(3);
          bike2.printStates();
     }
}
The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3

How does Java allocate stack and heap memory?


Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class).