โปรแกรมแรก ด้วยภาษา JAVA

ปรับปรุง : 2549-11-06 (class)
เว็บเพจหน้านี้ รวมโปรแกรมแรก แต่ละแบบที่พัฒนาด้วย 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() { }
}

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223