Really Basic Java Template08/09/06 PermalinkTemplates for an easy way to make a quick Java application.
//Basic Java application template
import java.io.*;
import java.util.*;
import java.lang.*;
public class Template{
Template(){
go();
}
public void go(){
try{
//do stuff
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args){
new Template();
}
}
//Basic servlet
import java.io.*;
import java.util.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletTemplate extends HttpServlet
{
public void init(ServletConfig servletconfig) throws ServletException
{
super.init(servletconfig);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("ok");
}
}
//Basic applet
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class AppletTemplate extends Applet{
String text = null;
public void init() {
text = "AppletTemplate";
setSize(100,30);
}
public void start() {
}
public void stop() {
}
public void destroy() {
}
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString(text, 20, 20);
}
}
//Basic Midlet
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MidletTemplate extends MIDlet implements CommandListener
{
public Command exitCommand;
public Form f;
public MidletTemplate()
{
exitCommand = new Command("Exit", Command.EXIT, 1);
f = new Form("MidletTemplate");
}
protected void startApp()
{
f.addCommand(exitCommand);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
}
protected void pauseApp() {}
protected void destroyApp(boolean bool) {}
public void commandAction(Command cmd, Displayable disp)
{
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
---------------
Or with a menu..
//Basic Midlet with a menu
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MidletTemplate extends MIDlet implements CommandListener
{
public Command exitCommand,selectCommand=null;
public List list=null;
public MidletTemplate()
{
exitCommand = new Command("Exit", Command.EXIT, 1);
selectCommand = new Command("Select", Command.BACK, 0);
list = new List("Midlet with Menu", List.IMPLICIT, new String[]{"Option 0","Option 1","About"}, null);
}
protected void startApp()
{
list.addCommand(selectCommand);
list.addCommand(exitCommand);
list.setCommandListener(this);
Display.getDisplay(this).setCurrent(list);
}
protected void pauseApp() {}
protected void destroyApp(boolean bool) {}
public void commandAction(Command cmd, Displayable disp)
{
if (cmd == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if(cmd == selectCommand) {
if(list.getSelectedIndex()==0){
//Option 0
}
else if(list.getSelectedIndex()==1){
//Option 1
}
else if(list.getSelectedIndex()==2){
//About
}
}
}
}
//Basic Xlet
import javax.microedition.xlet.*;
public class XletTemplate implements Xlet {
private XletContext context;
public XletTemplate(){
}
public void destroyXlet( boolean unconditional )
throws XletStateChangeException {
exitXlet();
}
public void exitXlet(){
context.notifyDestroyed(); // destroys Xlet
}
public XletContext getContext(){ // for convenience
return context;
}
public void initXlet( XletContext context )
throws XletStateChangeException {
this.context = context;
}
public void pauseXlet(){
}
public void startXlet() throws XletStateChangeException {
}
}
//Basic Android App
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidTemplate extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Test");
setContentView(tv);
}
}
|
|