โปรแกรมแรก ภาษาจาวา
Home  Contents KMArticlesMembersSponsorsAbout us

ปรับปรุง : 2556-06-20 (ปรับ template)
ขอบซ้ายขอบบน
โปรแกรมแรก ภาษาจาวา
    โปรแกรม 6 แบบด้วยภาษาจาวา
    1. Command Line
    คือ โปรแกรมที่ประมวลผลผ่าน System Command อาจสั่งประมวลผลใน editplus ได้
    2. Application
    คือ แสดงผลการทำงานในเฟรม (Frame) แบบกราฟฟิก (Graphic Mode) คล้าย Visual Basic
    3. Applet
    คือ ผลการประมวลผล ถูกนำไปใช้ใน Webpage ผ่าน Applet Tag อาจใช้ appletviewer แทน explorer
    4. JSP
    คือ การพัฒนาเว็บเพจแบบ Server-Side Script คล้าย php, asp หรือ perl ผ่าน Tomcat Server
    5. Servlet
    คือ การพัฒนาเว็บเพจ ที่โครงสร้างภาษายังเป็นแบบ Java ผ่าน Tomcat Server
    6. J2ME
    คือ การพัฒนาโปรแกรมสำหรับอุปกรณ์เคลื่อนที่ ต้องติดตั้ง J2SDK และ J2ME ทดสอบใน KToolbar
เว็บเพจหน้านี้ รวมโปรแกรมแรก แต่ละแบบที่พัฒนาด้วย JAVA
1. syscmd.java
/*
Title : Test of Command Line
DOS> javac syscmd.java
DOS> java syscmd
*/
class syscmd {
public static void main(String args[]) {
System.out.println("hello");
} }
/* 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 {
static int haveit;
public static void main(String a[]) {
System.out.println(haveit);
} } class car {
static int haveit = 1;
public static void main(String a[]) {
System.out.println(haveit);
} }

2. app1.java
/*
Title : Test of Application with Full Button
DOS> javac app1.java
DOS> java app1
*/
import java.awt.*;
public class app { 
static Frame fs = new Frame("THAIABC");
static Button b = new Button("hello");
public static void main(String args[]) {
fs.setSize(400,150); // (กว้าง,ยาว)
fs.add(b);
fs.show();
} }
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() { }
}
"Imagination is more important than knowledge" - Albert Einstein
Home
Thaiabc.com
Thainame.net
Lampang.net
Nation university
PHP
MySQL
Visual basic.NET
TabletPC
Linux
Online quiz
Download
Search engine
Web ranking
Add website
Blog : Education
Blog : ACLA
Blog : Lampang
Facebook.com
Twitter.com
About us
My dream
Site map
Sponsor
http://goo.gl/72BPC