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, Buttons and Text field
Swing, Buttons and textfield


// First example: -------------
/**
Here a simple example using the first element of swing 
and actionListener. J maches javax.
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;//For J-Components


public class NumbersBoxes extends JApplet implements ActionListener
{

private final static long serialVersionUID = 42L;// For the compiler

//Declarations:
//---------------
JTextField  text;
String   Text;
// 2 Panels
JPanel ThisPanel = new JPanel();
JPanel TextPanel = new JPanel();
//4 bottons
JButton Fibona = new JButton("Fib");
JButton Factoria  = new JButton("Fac");
JButton Somma   = new JButton("Sum");
JButton Closa   = new JButton("Quit");

//Initialisation:
//---------------
public void init()
{
// Once the frame is fired
this.setBackground(Color.white); 
this.setLayout(new BorderLayout(0,0));	

// Make the buttons listen
Fibona.addActionListener(this);
Factoria.addActionListener(this);
Somma.addActionListener(this);
Closa.addActionListener(this);

// Add the bottons in the panele ThisPanel	
add(ThisPanel);
ThisPanel.add(Fibona);
ThisPanel.add(Factoria);
ThisPanel.add(Somma);
ThisPanel.add(Closa);
ThisPanel.add(TextPanel);
//Where to have the output ( textfield)

text = new JTextField("");
text.setForeground(Color.red);
text.setEditable(false);
TextPanel.add(text);
text.setText("              ");
}


//Performing:
//-----------
public void actionPerformed(ActionEvent evt )
{

JButton keyClicked = (JButton)(evt.getSource()); 
/* String ThisLabel = keyClicked.getLabel();  
getLabel(). Deprecated. - Replaced by getText */

String ThisLabel = keyClicked.getText();

if (("Quit".equals(ThisLabel))){
System.exit(0); 

}
else if (("Fib".equals(ThisLabel)) | ("Fac".equals(ThisLabel)) 
| ("Sum".equals(ThisLabel)))
{ 			
text.setText(" ");// empty first
Text = ThisLabel;
text.setText(Text);


Object obj = evt.getSource();//Just for background colors
if (obj == Somma)
TextPanel.setBackground(Color.red);
if (obj == Factoria)
TextPanel.setBackground(Color.green);
}

}//End ActionPerformed

}


// Second example: -------------

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Date;
import java.util.*;

public class TheDays extends Applet implements ActionListener
{
//1. Declare variables:
//-----------------------

TextField TheResult ;

Color lightGray = new Color(200,200,200); 
Color lightRed = new Color(248,200,200); 

Font TheFont1 = new Font("Arial", Font.BOLD, 13); 
Font TheFont2 = new Font("Courier New", Font.BOLD, 18);


Button [] TheKeys = {new Button ("0"), new Button ("1"), 
new Button ("2"), new Button ("3"), 
new Button ("4"),new Button ("5"),new Button ("6"),
new Button ("Today"),new Button ("OFF")};


//2. Initialisation:
//--------------------
public void init() 
{ 
this.setBackground(lightRed ); 
this.setLayout(new BorderLayout());

setLayout(null);
setSize(600, 400);
setVisible(true);

TheResult = new TextField();
TheResult.setLocation(100, 100);
TheResult.setSize(400,35);
add (TheResult);
TheResult.setFont(TheFont2);
TheResult.setForeground(Color.red);


	for(int i=0; i< 9 ; i++) 
	{ 
	TheKeys[i].setBackground(lightGray);
	TheKeys[i].setForeground(Color.blue);
	TheKeys[i].setLocation(100 + 60*(i - 1), 200);
	TheKeys[i].setSize(50, 30);
	TheKeys[i].setFont(TheFont1);
	add(TheKeys[i]);
	TheKeys[i].addActionListener(this); 
	}

}


public void actionPerformed(ActionEvent evt) 
{ 
Button keyClicked = (Button)(evt.getSource()); 
String name = keyClicked.getLabel(); 
runCalculations(name);
} 

public void runCalculations (String name){
if(name =="0"){ TheResult.setText("Sunday");}
else if(name =="1"){ TheResult.setText("Monday");}
else if(name =="2"){ TheResult.setText("Tuesday");}
else if(name =="3"){ TheResult.setText("Wednesday");}
else if(name =="4"){ TheResult.setText("Thirsday");}
else if(name =="5"){ TheResult.setText("Friday");}
else if(name =="6"){ TheResult.setText("Saturday");}
else if(name =="Today"){ PerformThis (name);}
else {PerformThis();}
}


public void PerformThis(String TheString)
{
Date ThisDate = new Date();
TheResult.setText(TheString + " : " + ThisDate);  
}


public void PerformThis()
{
System.exit(0); 	 
}

}
		
	
		
	

  
Google
Web
ScientificSentence
 




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


© Scientificsentence 2009. All rights reserved.