Thursday, 6 September 2012

Java Button


Introduction of Java Button


Button is used to perform some task.We can use following method for this purpose.
  1. Constructors
    
    Button(String matter);
    
    
  2. Foreground color method
    
    void setForeground(Color);
    Color getForeground();
    
    
  3. Background color method
    
    void setBackground(Color);
    Color getBackground();
    
    
  4. Location method
    
    void setLocation(int x, int y);
    void setLocation(Point);
    Point getLocation();
    
    
  5. Labe Size method
    
    void setSize(int width, int height);
    void setSize(Dimension);
    Dimension getSize();
    
Programm

Save MyFrameListener.java

import java.awt.event.*;
class MyFrameListener extends WindowAdapter
{
	public void windowClosing(WindowEvent we)
	{
		System.exit(0);
	}
}


Save MyFrame.java 
 
import java.awt.*;
class MyFrame extends Frame implements ActionListener, TextListener, FocusListener
{
	Label l1, l2, l3;
	TextField t1, t2, t3;
	Button btnAdd, btnSubtract, btnMultiply, btnDivide;
	MyFrame()
	{
		super("Simple Math Calculator");
		setSize(350,200);
		addWindowListener(new MyWindowAdapter());
		setResizable(false);
		addControls();
		setVisible(true);
		enableDisableButtons(false);
	}
	
	private void addControls()
	{
		setLayout(null);
		
		int x, y, hgap, vgap, lblWidth, txtWidth, temp, btnWidth;
		
		y = 35;
		hgap = 3;
		vgap = 3;
		lblWidth = 120;
		txtWidth = 150;
		btnWidth = 80;
		
		temp = (getSize().width - (lblWidth + hgap + txtWidth)) / 2;
		x = temp;
		
		l1 = new Label("Enter first number");
		l1.setSize(lblWidth, 18);
		l1.setLocation(x,y);
		add(l1);
		
		x += lblWidth + hgap;
		
		t1 = new TextField();
		t1.setSize(txtWidth, 20);
		t1.setLocation(x,y);
		add(t1);
		
		x = temp;
		y += 20 + vgap;
		
		l2 = new Label("Enter second number");
		l2.setSize(lblWidth, 18);
		l2.setLocation(x,y);
		add(l2);
		
		x += lblWidth + hgap;
		
		t2 = new TextField();
		t2.setSize(txtWidth, 20);
		t2.setLocation(x,y);
		add(t2);
		
		
		y += 20 + vgap + 20;
		x = (getSize().width - (4*btnWidth + 3*hgap)) / 2;
		
		btnAdd = new Button("Add");
		btnAdd.setSize(btnWidth, 24);
		btnAdd.setLocation(x,y);
		add(btnAdd);
		
		x += btnWidth + hgap;
		
		btnSubtract = new Button("Subtract");
		btnSubtract.setSize(btnWidth, 24);
		btnSubtract.setLocation(x,y);
		add(btnSubtract);
		
		x += btnWidth + hgap;
		
		btnMultiply = new Button("Multiply");
		btnMultiply.setSize(btnWidth, 24);
		btnMultiply.setLocation(x,y);
		add(btnMultiply);
		
		x += btnWidth + hgap;
		
		btnDivide = new Button("Divide");
		btnDivide.setSize(btnWidth, 24);
		btnDivide.setLocation(x,y);
		add(btnDivide);
		
		x = temp;
		y += 24 + vgap + 20;
		
		l3 = new Label("Result");
		l3.setSize(lblWidth, 18);
		l3.setLocation(x,y);
		add(l3);
		
		x += lblWidth + hgap;
		
		t3 = new TextField();
		t3.setSize(txtWidth, 20);
		t3.setLocation(x,y);
		t3.setEditable(false);
		add(t3);
		
		btnAdd.addActionListener(this);
		btnSubtract.addActionListener(this);
		btnMultiply.addActionListener(this);
		btnDivide.addActionListener(this);
		
		t1.addTextListener(this);
		t2.addTextListener(this);
		t1.addFocusListener(this);
		t2.addFocusListener(this);
	}
	
	private void enableDisableButtons(boolean flag)
	{
		btnAdd.setEnabled(flag);
		btnSubtract.setEnabled(flag);
		btnMultiply.setEnabled(flag);
		btnDivide.setEnabled(flag);
	}
	
	public void focusGained(FocusEvent fe)
	{
		TextField tf = (TextField)fe.getSource();
		tf.setSelectionStart(0);
		tf.setBackground(Color.YELLOW);
	}
	public void focusLost(FocusEvent fe)
	{
		TextField tf = (TextField)fe.getSource();
		tf.setBackground(Color.WHITE);
	}
	
	public void textValueChanged(TextEvent te)
	{
		float n1, n2;
		boolean flag = false;
		try
		{
			n1 = Float.parseFloat(t1.getText());
			n2 = Float.parseFloat(t2.getText());
			flag = true;
		}
		catch (NumberFormatException exp)
		{
			flag = false;
		}
		finally
		{
			enableDisableButtons(flag);
		}
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		float n1, n2, n3;
		n1 = Float.parseFloat(t1.getText());
		n2 = Float.parseFloat(t2.getText());
		n3 = 0;
		
		if (ae.getSource() == btnAdd)
			n3 = n1 + n2;
		else if (ae.getSource() == btnSubtract)
			n3 = n1 - n2;
		else if (ae.getSource() == btnMultiply)
			n3 = n1 * n2;
		else if (ae.getSource() == btnDivide)
			n3 = n1 / n2;
			
		t3.setText(Float.toString(n3));
	}
}


Save GUI1.java

public class GUI1
{
	public static void main(String args[])
	{
		MyFrame mf = new MyFrame();
	}
}

No comments:

Post a Comment