Java-ohjelmointiympäristön asentaminen
Asenna Java-ohjelmointiympäristö. Ensin Java development Kit, sitten Java api documentation ja lopuksi JCreator tai jokin muu IDE.
Käyttöliittymä
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
class Ala {
private int x,y,z;
public void setAla(String x, String y){
this.x = Integer.valueOf(x);
this.y = Integer.valueOf(y);
z = this.x * this.y;
}
public String getAla(){
return String.valueOf(z);
}
}
class Suorakulmio {
private int x,y;
void piirra(String x, String y, Graphics g) {
this.x=Integer.valueOf(x);
this.y=Integer.valueOf(y);
g.drawRect(200,50,this.x,this.y);
}
}
public class Kayttoliittyma extends Applet implements ActionListener {
TextField tx, ty;
Label tz;
Button laske;
Ala ala = new Ala();
Suorakulmio ruutu = new Suorakulmio();
public void init () {
add(tx = new TextField());
add(ty = new TextField());
add(tz = new Label(""));
add(laske = new Button("Ala"));
laske.addActionListener(this);
}
public void paint(Graphics g) {
tx.reshape(20, 20, 80, 20);
ty.reshape(20, 40, 80, 20);
tz.reshape(20, 60, 80, 20);
laske.reshape(20, 80, 60, 20);
ruutu.piirra(tx.getText(),ty.getText(),g);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Ala")){
ala.setAla(tx.getText(),ty.getText());
tz.setText(ala.getAla());
repaint();
}
}
}
Runko
/**
* @(#)Merkkijonot.java
*
* Merkkijonot Applet application
*
* @author
* @version 1.00 2009/10/29
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Merkkijonot extends Applet implements ActionListener {
TextField tx;
Label lx;
Button ok;
String x,y;
public void init () {
add(tx = new TextField());
add(lx = new Label(""));
add(ok = new Button("Ok"));
ok.addActionListener(this);
}
public void paint(Graphics g) {
tx.reshape(40, 20, 100, 20);
lx.reshape(40, 40, 180, 20);
ok.reshape(40,60,40,20);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Ok")){
x = tx.getText();
lx.setText(x);
tx.setText("");
tx.requestFocus();
}
repaint();
}
}