Javascript  
 
  ScriptJava  
 
  Perl  
 
  PHP  
 
  ROR  
 
  ask us  
 

 

Applications
  1. Getting started
  2. Definitions
  3. First program
  4. Characters and Strings
  5. Object and methods
  6. Arrays and Circles
  7. Exceptions
  8. The main method
  9. Reading
  10. Writing file
  11. Vectors
  12. Stacks
  13. Map The Dictionary
  14. Lists
  15. Linked lists
  16. Collection
  17. Interfaces
  18. Scanner
  19. StringTokenizer
  20. Generics
  21. JDBC
  22. DataBase Queries
  23. JSP, The main step

Graphics

Applets
  1. Regards
  2. One Picture
  3. Calculator
  4. Random pictures
  5. Bouncing picture

Swings
  1. Buttons listeners
  2. TextFields
  3. Swing Example

JavaBeans
  1. The first step
  2. Example

Search a word:
   



© The scientific sentence. 2010

Swing, The first step
Swing, Buttons listeners


/**
This example shows how to build buttons inside a Layout . There are three 
buttons to click to have their corresponding rresults in a text field.
The steps:
1. import applet for applets and awt for graphics.
2. build a class ThisAction inside the class NumbersStrings. The first 
inherits (extends) from Applet class. The second  inside implements fron 
ActionListener class which contains then the actionPerformed method.
3. Declare the components: Buttons and textfield
4. Initialization of :
	- The Layout (size, etc ..)
	- The components (buttons and textfield): 
		-- Instantion,
		-- location, size, color, etc ...
		-- add (NAME);
5. Make the buttons to listen: NAME.addActionlistener(OneAction);
*/
	import java.awt.*;
	import java.awt.event.*;
	import java.applet.*;
	/*
	For JPanel
	import javax.swing.JPanel; 
	import javax.swing.*;
	And replace 
	Applet, Button, Frame, Panel by: JApplet, JButton, JFrame, JPanel 
	*/

	public class NumbersStrings extends Applet 
	//The Buttons class inherite fron the Applet Class.
	{
	//private final static long serialVersionUID = 42L;// For the compiler
	//Declare components	
	final int TheNumber = 10;
	Button	Fibonacci, Factorial, Sum, QuitButton;
	//Button is predefined
	TextField TheResult;		//TextField is predefined	
	String TheString;
	int int1;
	
		//Let's declare some light colors( not predefined):
		Color lightBlue = new Color(185,185,255); 
		Color lightGray = new Color(200,200,200); 
		Color lightRed = new Color(248,200,200); 
		
		//And some fonts:
		Font f1 = new Font("Arial", Font.BOLD, 13); 
		Font f2 = new Font("Courier New", Font.BOLD, 15); 
	
	/*
	Apply colors and fonts on an object: 
	- TheObject.setFont(f1); and
	- TheObject.setBackground(lightBlue); or
	- TheObject.setForeground(Color.red); //Predefined
	*/
					
	public void init(){
	setLayout(null);
	setSize(500, 300);
	setVisible(true);	

	// Add components		

	TheResult = new TextField();
	TheResult.setEditable(false);
	TheResult.setLocation(70, 50);
	TheResult.setSize(120,35);
	add (TheResult);
	TheResult.setForeground(Color.red);
	TheResult.setFont(f1);

	
	Fibonacci = new Button("Fibonacci of " + TheNumber);
	Fibonacci.setLocation(80, 100);
	Fibonacci.setSize( 100,40);
	Fibonacci.setForeground(Color.green);
	add (Fibonacci);	

	
	Factorial  = new Button("Factorial of " + TheNumber);
	Factorial.setLocation(80, 150);
	Factorial.setSize( 100,40);
	Factorial.setForeground(Color.green);
	add (Factorial);
	Factorial.setForeground(Color.blue);//Predefined
	Factorial.setBackground(lightBlue);		
	
	
	Sum  = new Button("Sum of " + TheNumber);
	Sum.setLocation(80, 200);
	Sum.setSize( 100,40);
	Sum.setForeground(Color.blue);
	add (Sum);
	Sum.setFont(f2);
	Sum.setBackground(lightGray);

	QuitButton  = new Button("Quit");
	QuitButton.setLocation(80, 250);
	QuitButton.setSize(100,40);
	QuitButton.setForeground(Color.green);
	add (QuitButton);
	QuitButton.setBackground(lightRed);

	
	// Make Listener
	ThisAction OneAction = new ThisAction();
	Fibonacci.addActionListener(OneAction);
	Factorial.addActionListener(OneAction);
	Sum.addActionListener(OneAction);
	QuitButton.addActionListener(OneAction);	
	}	


	
	//Definition of the Actionlistner's class ThisAction	
	public class ThisAction implements ActionListener {
	public void actionPerformed(ActionEvent evt ){			
	Object obj = evt.getSource();

	if (obj == Fibonacci)
	{
	int1 =  Calculation(Fibonacci);
	String TheString = new String(int1 + ""); 
	TheResult.setText(TheString);
	}	
		
	else
	if (obj == Factorial)
	{
	int1 =  Calculation(Factorial);
	String TheString = new String(int1 + ""); 
	TheResult.setText(TheString);
	}
				
	else
	if ( obj == Sum)
	{
	int1 =  Calculation(Sum);
	String TheString = new String(int1 + ""); 
	TheResult.setText(TheString);
	}

	else 
	if ( obj == QuitButton){
	Fibonacci.setEnabled(false);
	Factorial.setEnabled(false);
	Sum.setEnabled(false);
	QuitButton.setEnabled(false);
	}
	}
	}

	private int Calculation(Object arg)
	{
	if (Fibonacci.equals(arg)) int1 = Fibonacci(TheNumber);
	else if (Factorial.equals(arg)) int1 = Factorial(TheNumber);
	else if (Sum.equals(arg)) int1 = Sum(TheNumber);
	else if (QuitButton.equals(arg)) {
	System.exit(0);// Equivalent to Runtime.getRuntime().exit(0);
	}
	return int1;
	}


	private int Fibonacci(int m)
	{
	if (m <= 2) return m;
	else 
	return	Fibonacci(m-1) + Fibonacci(m-2);  
	}


	private int Factorial(int m)
	{
	if (m <= 1) return m;
	else 
	return	m * Factorial(m - 1);

	}

	private int Sum(int m)
	{
	return	m * (m+1) /2;

	}

	public static void main (String[] args)	{}
}


	

  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.