HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Getting Started with Java on PDAs"
Vol. 7, Issue 2, p. 88

	


Listing 1

package basicconsole;

public class counter extends Thread {
 private int begin;
 private int end;
 
public counter(int begin, int end) {
 this.begin = begin;
 this.end = end;
}

public void run() {
System.out.println(this.getName() + " has begun executing ");
for (int i = begin; i <= end; i++) {
System.out.print(i + " ");
}
System.out.println("\n" + this.getName() + " has completed executing
");
}

}


Listing 2

package basicconsole;

public class app {

public app() {
}

public static void main(String[] args) {
counter threadOne = new counter(1, 100);
counter threadTwo = new counter(101, 200);
threadOne.setName("ThreadOne");
threadTwo.setName("ThreadTwo");
threadOne.start();
threadTwo.start();
}
}


Listing 3

package basicawt;

import java.awt.*;

public class app {
boolean packFrame = false;

/**Construct the application*/
public app() {
frame1 frame = new frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from
their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}

//Center the window
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}

/**Main method*/
public static void main(String[] args) {
new app();
}
}

Listing 4

package basicawt;

import java.awt.*;
import java.awt.event.*;

public class frame1 extends Frame implements Runnable {
Button button1 = new Button();
TextField textField1 = new TextField();
Thread t;
int Count;
Button button2 = new Button();
TextField textField2 = new TextField();
int textBox;
/**Construct the frame*/
public frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

/**Component initialization*/
private void jbInit() throws Exception {
button1.setLabel("Start Thread One");
button1.setBounds(new Rectangle(14, 51, 202, 27));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.setSize(new Dimension(235, 290));
this.setTitle("Basic AWT");
this.setLayout(null);
button2.setLabel("Start Thread Two");
button2.setBounds(new Rectangle(14, 121, 202, 27));
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
textField2.setBounds(new Rectangle(14, 158, 202, 27));
textField1.setBounds(new Rectangle(14, 85, 202, 27));
this.add(textField2, null);
this.add(button1, null);
this.add(textField1, null);
this.add(button2, null);
}

/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void button1_actionPerformed(ActionEvent e) {
Count = 0;
textBox = 1;
t = new Thread(this);
t.setName("ThreadOne");
t.start();
}

public void run() {
if (textBox == 1) {
for (int i = 0; i <= 200; i++) {
textField1.setText(i + " ");
}
} else {
for (int i = 0; i <= 200; i++) {
textField2.setText(i + " ");
}
}
}

void button2_actionPerformed(ActionEvent e) {
Count = 0;
textBox = 2;
t = new Thread(this);
t.setName("ThreadTwo");
t.start();
}
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.