Document directory
- Basic textbox
- Textbox with Timer
- Textbox with command
Niang's, I bought a bad book, and a simple demo started with a problem. It was wrong for me to compile it.
The Code is as follows:
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Public class dipplaytest extends MIDlet {
Private display;
Private textbox T;
Public dipplaytest (){
// Super ();
Display = display. getdisplay (this );
T = new Textbox ("display text", "Display object test", 0, 0 );
}
Public void Startapp (){
Display. setcurrent (t );
}
Public void pauseapp (){
}
Public void destroyapp (Boolean unconditional ){
}
}
The compilation error tells me that it is a parameter problem. After troubleshooting for half a day, we found that the textbox constructor can be compiled successfully after being removed. Obviously, the parameter of this function is faulty.
Java. Lang. illegalargumentexception
At com. Sun. MIDP. lcdui. dynamiccharacterarray. <init> (+ 15)
At com. Sun. MIDP. lcdui. dynamiccharacterarray. <init> (+ 6)
At javax. microedition. lcdui. textfield. <init> (+ 53)
At javax. microedition. lcdui. textbox. <init> (+ 74)
At dipplaytest. <init> (+ 26)
At java. Lang. Class. runcustomcode (+ 0)
At com. Sun. MIDP. MIDlet. midletstate. createmidlet (+ 19)
At com. Sun. MIDP. MIDlet. selector. Run (+ 22)
View textbox instructions
First, its definition: Public Textbox (String title, string text, int maxsize, int constraints)
Parameters:
Title-The title text to be shown with the display
Text-The initial contents of the text editing area, null may be used to indicate no initial content
Maxsize-The maximum capacity in characters. The specified may limit boundary maximum capacity and the actually assigned capacity may me specify than requested. A defensive application will test the actually specified capacity with getmaxsize ().
Constraints-See input constraints
There is also a description
Creates a new textbox object with the given title string, initial contents, maximum size in characters, and constraints. if the text parameter is null, the textbox is created empty. the maxsize parameter must be greater than zero. an illegalargumentexception is thrown if the length of the initial contents string exceeds maxsize. however, the implementation may assign a maximum size smaller than the application had requested. if this occurs, and if the length of the contents exceeds the newly assigned maximum size, the contents are truncated from the end in order to fit, and no exception is thrown.
After a long time, I entered a problem with maxsize. If I passed a value of 0, it would be a problem. Because, even if the textbox is empty, maxsize must be greater than 0.
I changed the parameter. It's really good. It's amazing that there are errors in the book. Let you debug and learn more ~~~~
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Public class dipplaytest extends MIDlet {
Private display;
Private textbox T;
Public dipplaytest (){
// Super ();
Display = display. getdisplay (this );
T = new Textbox ("display text", "Display object test", 20, 0 );
}
Public void Startapp (){
Display. setcurrent (t );
}
Public void pauseapp (){
}
Public void destroyapp (Boolean unconditional ){
}
}
Finally, let's look at the textbox example.
Http://www.eli.sdsu.edu/courses/fall04/cs683/notes/midletui/midletui.html#a_Toc496260825
Hi-level UI Components
CS 683 fall 04 |
Doc 19, MIDlet UI slide #8 |
Textbox
Textbox receivemtation
Http://www.eli.sdsu.edu/courses/fall04/cs683/j2me/docs/api/midp/javax/microedition/lcdui/TextBox.html
Input constraints
Http://www.eli.sdsu.edu/courses/fall04/cs683/j2me/docs/api/midp/javax/microedition/lcdui/TextField.html
CS 683 fall 04 |
Doc 19, MIDlet UI slide #9 |
Basic textbox
Based on example, pages 89-90, j2in a nutshell, Kim topley, O 'Reilly
import java.io.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet; public class TextBoxMIDlet extends MIDlet { private static final int MAX_TEXT_SIZE = 80; protected TextBox textBox; protected Display display; public TextBoxMIDlet() { textBox = new TextBox("TextBox Example", readFile("/Text.txt"), MAX_TEXT_SIZE, TextField.ANY); Ticker ticker = new Ticker("This is a ticker..."); textBox.setTicker(ticker); display = Display.getDisplay(this); display.setCurrent(textBox); } public void startApp() { } public void pauseApp() { } public void destroyApp( boolean unconditional ) { }
CS 683 fall 04 |
Doc 19, MIDlet UI slide #10 |
Basic textbox continued
private String readFile(String filename) { try { InputStream byteInput = getClass().getResourceAsStream(filename); InputStreamReader characterInput = new InputStreamReader(byteInput); char[] buffer = new char[32]; StringBuffer stringBuffer = new StringBuffer(); int count; while ((count = characterInput.read(buffer, 0, buffer.length)) > -1) { stringBuffer.append(buffer, 0, count); } return stringBuffer.toString(); } catch (Exception ioProblem) { return "Could not read file"; } }}
CS 683 fall 04 |
Doc 19, MIDlet UI slide #11 |
Textbox with Timer
Based on examples, pages 73-74, 89-90, j2in a nutshell
import java.io.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet;import java.util.*; public class ThreadTextBoxMIDlet extends MIDlet { private static final int MAX_TEXT_SIZE = 80; protected TextBox textBox; private Timer timer; private int count = 0; public ThreadTextBoxMIDlet() { textBox = new TextBox("Timer Example", "Hello for now", MAX_TEXT_SIZE, TextField.ANY); Display.getDisplay(this).setCurrent(textBox); } public void startApp() { if (timer == null) startTimer(); else count = 0; }
CS 683 fall 04 |
Doc 19, MIDlet UI slide #12 |
Textbox with timer continued
public void pauseApp() { textBox.setString("Here we go again"); } public void destroyApp( boolean unconditional ) { stopTimer(); } private void startTimer() { TimerTask addCount = new TimerTask() { public void run() { textBox.setString( "" + count++); if (count == 10) notifyPaused(); else if (count > 15) resumeRequest(); } }; timer = new Timer(); timer.schedule(addCount, 2000, 1000); } private void stopTimer() { if (timer != null) timer.cancel(); }}
CS 683 Fall 04 |
Doc 19, MIDlet UI Slide # 13 |
Textbox with command
Based on example, Pages 99-100, j2_in a nutshell
import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Displayable; public class CommandTextBoxMIDlet extends TextBoxMIDlet implements CommandListener { private static final Command EXIT_COMMAND = new Command("Exit", Command.EXIT, 0); private static final Command OK_COMMAND = new Command("OK", Command.OK, 0); private static final Command CLEAR_COMMAND = new Command("Clear", Command.SCREEN, 1); private static final Command REVERSE_COMMAND = new Command("Reverse", Command.SCREEN, 1); public CommandTextBoxMIDlet() { super(); textBox.addCommand(OK_COMMAND); textBox.addCommand(EXIT_COMMAND); textBox.addCommand(CLEAR_COMMAND); textBox.addCommand(REVERSE_COMMAND); textBox.setCommandListener(this); }
CS 683 fall 04 |
Doc 19, MIDlet UI slide #14 |
Textbox with command continued
public void commandAction(Command c, Displayable d) { if (c == EXIT_COMMAND) { destroyApp(true); notifyDestroyed(); } else if (c == OK_COMMAND) { textBox.setString("OK pressed"); } else if (c == CLEAR_COMMAND) { textBox.setString(null); } else if (c == REVERSE_COMMAND) { String str = textBox.getString(); if (str != null) { StringBuffer sb = new StringBuffer(str); textBox.setString(sb.reverse().toString()); } } } }