![]() | โปรแกรมแรก ด้วยภาษา JAVA | ![]() |
1. syscmd.java
/*
Title : Test of Command Line
DOS> javac syscmd.java
DOS> java syscmd
*/
class syscmd {
/* Title : Test of Command Line (ใช้อธิบายเรื่อง static, instant, encapsulation, class และ sourcecode) DOS> javac asset.java DOS> java home DOS> java car Output> home=0 and car=1 */ class home { |
2. app1.java
/*
Title : Test of Application with Full Button
DOS> javac app1.java
DOS> java app1
*/
import java.awt.*;
public class app {
Title : Test of Application with Exit Button DOS> javac app2.java DOS> java app2 */ import java.awt.*; import java.awt.event.*; public class app2 implements ActionListener { Frame s = new Frame("This is a boy"); Button bclose = new Button("Exit"); public static void main(String[] args) { new app2().init(); } public void init( ) { s.setSize(200,200); s.setLayout(null); // ทำให้ setBounds ใช้ได้ bclose.setBounds(10,40,70,20); s.add(bclose); bclose.addActionListener(this); s.show(); } public void actionPerformed(ActionEvent a) { if(a.getSource()==bclose) { System.exit(0); } } } |
3. helloapp.java และ x.htm
/*
DOS> javac helloapp.java
DOS> edit x.htm
<applet code="helloapp.class" width=120 height=120></applet>
DOS> explorer x.htm
*/
import java.applet.*;
import java.awt.Graphics;
public class helloapp extends Applet {
public void paint(Graphics g) {
g.drawString("hello",10,20);
}
}
|
4. hello.jsp
<%--
Require : J2SDK + Tomcat Apache
--%>
<%
out.println("hello");
%>
|
5. servhello.java
// Require : J2SDK + Tomcat Apache
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servhello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "hello";
out.println("<body>" + title);
}
}
|
6. hellomobile.java
/*
Step:
1. Install J2SDK + J2ME
2. New Project "burin1" in KToolbar and "burin1.hellomobile" in MIDIet Class Name
3. Type Source code in C:\WTK21\apps\burin1\src\hellomobile.java
4. build, run .. for testing in KToolbar
5. Next Step : learn to create JAR and JAD for mobile phone
Details : http://www.yonok.ac.th/burin/class/indexr.htm
*/
package burin1;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class hellomobile extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private boolean firstTime;
private Form mainForm;
public hellomobile() {
firstTime = true;
mainForm = new Form("rujjanapan");
}
protected void startApp() {
if(firstTime) {
mainForm.append("This demo display hello" + "and wait your text.");
mainForm.append(new TextField("Hello", "", 5, TextField.NUMERIC));
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
firstTime = false;
}
Display.getDisplay(this).setCurrent(mainForm);
}
public void commandAction(Command c, Displayable s) {
if(c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
protected void destroyApp(boolean unconditional) { }
protected void pauseApp() { }
}
|