الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيفية تشغيل المتصنت في كلاس ليس ميدلت؟

مغلق
بدأه loloo_min في 2 يناير 2008 · 8 رد · 1,181 مشاهدة · في JavaME
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

الأخوة الأفاضل

أشكركم جميعا على ا لجهود المبذولة في هذا المنتدى وللقائمين عليه

بس ياريت أجد رد سريع لمشكلتي لأن أحيانا الردود تطول بالأسابيع وأنا في حاجة ماسة لحل لهذه المشكلة

أنا بابرمج لعبة عالجوال واستخدمت فيها الفورم

هي عبارة عن أزارير وبجانب كل ازرار فيلد لادخال الرقم

بحيث ان اللاعب بضغط عشان يشوف الصورة وبعدبن يعطي كل صورتين مترابطتين الرقم نفسه الذي سيتم وضعه في الفيلد

هادي الفكرة باختصار

الآن أنا عالقة في نقطة وأتمنى أي أحد يساعدني فيها لأنها مرة مهمة وعلى أساسها تعتمد اللعبه ككل

وأتمنى من الجميع المساعدة بقدر الإمكان

المشكلة هي

أنا عندي تقريبا اكثر من 14 كلاس في اللعبه وواحد منهم فقط هو الميدلت

واللعبة تحتاج أن اللاعب ينتقل من فورم للآخر عن طريق اختيار امر الانتقال

"OK"

بس المشكلة الآن

لمن أشغل المتصنت في كلاس غير الميدلت يعطيني خطأ

Building "Hygienical Habits"

C:\Documents and Settings\Alla\j2mewtk\2.5.2\apps\Hygienical Habits\src\Button.java:48: setItemCommandListener(javax.microedition.lcdui.ItemCommandListener) in javax.microedition.lcdui.Item cannot be applied to (Button)

item3.setItemCommandListener(this);

^

1 error

com.sun.kvem.ktools.ExecutionException

Build failed

هل في طريقة تخليني أقدر أشغل المتصنت في كلاس غير الميدلت؟

سواء أكان المتنصت لأمر او لأزرار

رجاءا أفيدوني بحلول سريعة لأن الوقت محدود

مع الشكر مقدما...

#2

الأخوة الأفاضل حاولت أطبق

ItemCommandListener

واللي مش ميدلتButton في الكلاس

وبطل يطلعلي رسالة الخطأ بس المشكلة إن الازرار مابيعطي استجابة

هادي الأكواد اللي مستخدمة في هذه المشكلة

ده الكلاس هو الكلاس الرئيسي واللي اضطريت اني اطبق كل الوامر فيه عشان مش راضية تطبق في الكلاس غير الميدلت

خصوصا

لمن اكتب

form.setCurrent(this)

في كلاس مش ميدلت يعطيني غلط ويقول نفس الرسالة اللي في الفقرة السابقة

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class GameMidlet extends MIDlet implements 
CommandListener , ItemCommandListener
{

private Instruction inst;
//private Submit submit;
protected static Display display;

public GameMidlet()
{
display = Display.getDisplay(this);
inst = new Instruction();


}

public void startApp()
{
inst.displayInfo(inst.form);
inst.form.setCommandListener(this);
inst.playerName.form.setCommandListener(this);
//inst.playerName.start.button.item1.setItemComman

dListener(this);
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){}


public void commandAction(Command c, Displayable 

s)
{
if(s==inst.form && c == inst.ExitCm)
{
destroyApp(false);
notifyDestroyed();
}
else{
if(s==inst.form && c == inst.startCm)
{

inst.playerName.displayInfo( 

inst.playerName.form);

}
}

if (c == inst.playerName.deleteCm && s == 

inst.playerName.form)
{
inst.playerName.fldName.delete

(inst.playerName.fldName.size()-1, 1);
}


if (c == inst.playerName.backCm && s == 

inst.playerName.form)
{
display.setCurrent(inst.form);
inst.form.setCommandListener(this);
}

if (c == inst.playerName.okCm && s == 

inst.playerName.form)
{
inst.playerName.string = inst.playerName.getName

();
display.setCurrent(inst.playerName.start.form);
inst.playerName.start.form.setCommandListener

(this);
}

if(s==inst.playerName.form && c == 

inst.playerName.ExitCm)
{
destroyApp(false);
notifyDestroyed();
}

if(s==inst.playerName.start.form && c == 

inst.playerName.start.ExitCm)
{
destroyApp(false);
notifyDestroyed();
}
/**
if (c == inst.playerName.start.backCm && s == 

inst.playerName.start.form)
{
display.setCurrent(inst.playerName.form);
inst.playerName.form.setCommandListener(this);
}
**/
}

public void commandAction(Command c, Item i)
{

try{
if(inst.playerName.start.button.item3.getLabel

().equals("Level 1"))
{
display.setCurrent(inst.playerName.form);
inst.playerName.form.setCommandListener(this);

//inst.playerName.start.button.level1();
}}
catch(Exception e){
String gripe = "Error:" + e;
Alert a = new Alert

("Error",gripe,null,AlertType.ERROR);
display.setCurrent(a);
}


try{
if(inst.playerName.start.button.item3.getLabel

().equals("Level 2"))
{
inst.playerName.start.displayInfo

(inst.playerName.start.form);
}}
catch(Exception e){
String gripe = "Error:" + e;
Alert a = new Alert

("Error",gripe,null,AlertType.ERROR);
display.setCurrent(a);
}
}
}

ده الكلاس هو الكلاس اللا فيه المشكلة لان الازارير اللا فيه ماتستجيب للامر

import javax.microedition.lcdui.*;

public class Start extends GamesScreens  
{
//protected Levels levels;
protected Button button;

public Start()
{
super("Start");
button = new Button();
button.addButtonToCenter("Level 1",false, form);
button.addButtonToCenter("Level 2",false, form);
}

}

ده الكلاس هو اللا يحتوي على الخطوة

addButtonToCenter

وهو اللا شغلت فيه المتصنت على

StringItem

بس ما أعطى نتيجة

import javax.microedition.lcdui.*;


public class Button implements ItemCommandListener 

{
protected String bttnName;
protected StringItem item3;

public Button(){

StringItem item3 = new StringItem("", null, 

Item.BUTTON);
}



public void addButtonToLeft(String name,Form form)
{
Command cmmd = new Command(name, Command.ITEM, 1);

StringItem item1 = new StringItem(name, null, 

Item.BUTTON);
//item1.setLabel(name);
item1.setDefaultCommand(cmmd);
//item1.setItemCommandListener(this);
int flags =  

Item.LAYOUT_LEFT|Item.LAYOUT_VCENTER|Item.LAYOUT_2 

;
item1.setLayout(item1.getLayout()|flags);
form.append(item1);
}

public void addButtonToRight(String name, Form 

form)
{
Command cmd = new Command(name, Command.ITEM, 1);
StringItem item2 = new StringItem(name, null, 

Item.BUTTON);
//item2.setLabel(name);
item2.setDefaultCommand(cmd);
//item2.setItemCommandListener(this);
int flag =  
Item.LAYOUT_RIGHT|Item.LAYOUT_VCENTER|
Item.LAYOUT_2;
item2.setLayout(item2.getLayout()|flag);
form.append(item2);
}

 void addButtonToCenter(String name,boolean ln, 

Form form) {
Command cmmd = new Command(name, Command.ITEM, 1);
StringItem item3 = new StringItem(name, null, 

Item.BUTTON);
//item3.setLabel(name);
item3.setDefaultCommand(cmmd);
item3.setItemCommandListener(this);
int flags = Item.LAYOUT_2 | Item.LAYOUT_CENTER | 

Item.LAYOUT_EXPAND;
if (ln) flags |= Item.LAYOUT_NEWLINE_AFTER;
item3.setLayout(item3.getLayout() | flags);
form.append(item3);
}

public void commandAction(Command c, Item i){}
}

هادا الكلاس الام اللي بيورث خصائصه للكلاس

Start

import javax.microedition.lcdui.*;

public class GamesScreens {

protected final Form form;
protected String string;
protected Command ExitCm;

public GamesScreens(String formName)
{
form = new Form(formName);
ExitCm = new Command("Exit", Command.EXIT, 0);
form.addCommand(ExitCm);
}

public void displayInfo(Form form)
{
GameMidlet.display.setCurrent(form);
}
}

أحتاج أيضا طريقة أشغل فيها

form.setCurrent(this)

في كلاس مايكون ميدلت

الشكر مقدما لكل من يهتم في الموضوع ويدلي برأيه الذي ربما يكون سر الحل لمشكلتي

في انتظار الردود

تم تعديل هذه المشاركة بواسطة loloo_min في 3 يناير 2008 في 01:09

#3

هذا مثال يوضح ما تحتاجينه. عذرا على عدم الترتيب بسبب العجله. اذا وجد شيئ لم تفهميه ارجو ان تسالي عنه.

/*
 * Main.java
 *
 * Created on 03 كانون الثاني, 2008, 10:11 ص
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author MRDTech-PC
 */

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Main extends MIDlet{
	private Display display;
	Form1 f1;

	public void startApp(){
		display = Display.getDisplay(this);
		f1 = new Form1( this );

		display.setCurrent( f1 );

	}

	public void setCurrnt( Form f) {
		display.setCurrent( f );

	}

	protected void pauseApp() {

	}

	protected void destroyApp(boolean b) throws MIDletStateChangeException {

	}

	public void exit() throws MIDletStateChangeException {
		destroyApp(false);
		notifyDestroyed();

	}
}

/*
 * Form1.java
 *
 * Created on 03 كانون الثاني, 2008, 10:13 ص
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author MRDTech-PC
 */
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Form1 extends Form implements CommandListener{

	private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
	private static final Command CMD_SHOW = new Command("Show Next Form", Command.SCREEN, 1);

	private Main parent;

	/** Creates a new instance of Form1 */
	public Form1(Main parent ) {
		super("");

		this.parent = parent;

		StringItem item =
			new StringItem("Form1", "This is form1");
		append( item );	 
		addCommand(CMD_EXIT);
		addCommand(CMD_SHOW);

		setCommandListener( this );


	}

	public void commandAction(Command command, Displayable displayable) {
		if (command == CMD_EXIT) {
			try {
				System.out.println("in");
				parent.exit();
			} catch (MIDletStateChangeException ex) {
				ex.printStackTrace();
			}

		} else if(command == CMD_SHOW)  {
			parent.setCurrnt( new Form2( parent ) );

		}

	}

}

/*
 * Form2.java
 *
 * Created on 03 كانون الثاني, 2008, 10:28 ص
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
 *
 * @author MRDTech-PC
 */
public class Form2 extends Form implements CommandListener{

	 private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
	private static final Command CMD_BACK = new Command("Back", Command.BACK, 1);

	private Main parent;

	/** Creates a new instance of Form1 */
	public Form2(Main parent ) {
		super("");

		this.parent = parent;

		StringItem item =
			new StringItem("Form2", "This is form2");
		append( item );	 
		addCommand(CMD_EXIT);
		addCommand(CMD_BACK);

		setCommandListener( this );


	}

	public void commandAction(Command command, Displayable displayable) {
		if (command == CMD_EXIT) {
			try {
				System.out.println("in");
				parent.exit();
			} catch (MIDletStateChangeException ex) {
				ex.printStackTrace();
			}

		} else if(command ==CMD_BACK) {
			parent.setCurrnt( new Form1(parent) );

		}

	}

}
#4

أشكرك أخ muqawem على الاكواد

بس هل هادا معناه ان الحل في ان اعر ف الكلاس الاساسي في كل كلاس في اللعبة؟

هل لازم ان الكلاسات تورث من الفورم لان اصلا عندي الكلاسات بتورث من كلاس

GamesScreens

وماينفع الكلاس يورث اكتر من مرة مثل ماانت عارف؟

باختصار

هل ممكن توضيح اكتر لسبب المشكلة لان احس اني تهت؟

شكرا

في انتظار الردود والمشاركات من الجميع

#5

Muqawem الأخ

حاولت أطبق الاكواد اللي ارسلتها على اللعبة بس بتطلعلي المسج التالية

startApp threw an Exception
java.lang.SecurityException: Application not authorized to access the restricted API
 at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(+40)
 at com.sun.midp.security.SecurityToken.checkIfPermissionAllowed(+7)
 at com.sun.midp.midletsuite.MIDletSuiteImpl.checkIfPermissionAllowed(+8)
 at com.sun.midp.midlet.MIDletState.<init>(+83)
 at javax.microedition.midlet.MIDletProxy.<init>(+5)
 at javax.microedition.midlet.MIDlet.<init>(+13)
 at GameMidlet.<init>(+4)
 at GamesScreens.<init>(+29)
 at Instruction.<init>(+7)
 at GameMidlet.startApp(+17)
 at javax.microedition.midlet.MIDletProxy.startApp(+7)
 at com.sun.midp.midlet.Scheduler.schedule(+270)
 at com.sun.midp.main.Main.runLocalClass(+28)
 at com.sun.midp.main.Main.main(+80)
[code]


مع العلم اني نسخت الاكواد حقتك وطبقتها في البرنامج باسخدام 
API JTWI CLCD 1.1
واشتغلت كويس

هادي الاكواد اللي طبقتها بس ياريت أعرف هل تطبيقي لها صحيح؟
وفين المشكلة؟

[code]/*
* GameMidlet.java
*
* Created on 03-01-2008
*
*  */

/**
*
* author Alaa Menshawi@
*/

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GameMidletEx extends MIDlet{

	private Display display;
	InstructionEx inst;

	public void startApp(){
		display = Display.getDisplay(this);
		inst = new InstructionEx(this);
		display.setCurrent(inst );

	}

	public void setCurrnt( Form f) {
		display.setCurrent( f );

	}

	protected void pauseApp() {

	}

	protected void destroyApp(boolean b) throws MIDletStateChangeException {

	}

	public void exit() throws MIDletStateChangeException {
		destroyApp(false);
		notifyDestroyed();

	}
}

/*
* GamesScreens.java
*
* Created on 03 -01-2008
*
*
*/

/**
*
* author@ Alaa Menshawi
*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class GamesScreensEx extends Form implements CommandListener{

	protected String string;
	protected Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);

	protected GameMidletEx parent;

	/** Creates a new instance of GamesScreensEx */
	public GamesScreensEx(GameMidletEx parent, String formName)
 {
		super(formName);

		this.parent = parent;

		addCommand(CMD_EXIT);


		setCommandListener( this );


	}

	public void commandAction(Command c, Displayable d) {
		if (c == CMD_EXIT) {
			try {
				System.out.println("in");
				parent.exit();
			} 
		 catch (MIDletStateChangeException ex)
				{
ex.printStackTrace();
			}


}
}
}

/*
* Instruction.java
*
* Created on 03 -02-2008
*/

/**
*
* author Alaa Menshawi@
*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class InstructionEx extends GamesScreensEx implements CommandListener{

	protected GameMidletEx parent;

	protected Command CMD_START = new Command("Start", Command.SCREEN, 1);
	protected Command CMD_SHOW = new Command("Show Previous Scores", Command.SCREEN, 1);
protected PlayerNameEx playerNameEx = new PlayerNameEx(parent);

	/** Creates a new instance of InstructionEx */
	public InstructionEx(GameMidletEx parent) {
		super(parent,"Instruction");
		addCommand(CMD_START);
		addCommand(CMD_SHOW);

string ="Welcome to the Hygienical Habits Game \n \n This Game contains 6 different pictures, \n beside each picture, there is a small box \n where you can enter one number only to match between the pictures. \n \n There is a relationship between each 2 pictures in the game, \n which represents one important healthy habit in our daily life. \n \n  Your task is to find out the related picture, \n and give each two of them the same number. \n You need to enter the number in a box beside the pictures. \n \n For Example: \n Picture a and Picture b are related, \n So, I have to give the same number for [a] and  let say number (5). \n \n  Note that you have to hit the buttons in order to see the pictures. \n \n Once you finished, you have to submit your answers by choosing the submit command in the Menu";
append(string);

	}


	public void commandAction(Command c, Displayable d) {

		if (c == CMD_START) {

				parent.setCurrnt(playerNameEx);

			}  }
}

/*
* PlayerNameEx.java
*
* Created on 03-04-2008
*/

/**
*
* author Alaa Menshawi@
*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class PlayerNameEx extends GamesScreensEx implements CommandListener{

	protected GameMidletEx parent;

	private static final Command CMD_OK = new Command("OK", Command.OK, 1);
	private static final Command CMD_DEL = new Command("Delete", Command.SCREEN, 1);

	private static final Command CMD_BACK = new Command("Back", Command.SCREEN, 1);

protected TextField 
fldName = new TextField("Player Name", null, 14, TextField.ANY);

//protected Start start = new Start();

	 public PlayerNameEx(GameMidletEx parent)  
	 {
	 super(parent,"Player Name");

	 this.parent = parent;
	 append(fldName);
	 addCommand(CMD_OK);
	 addCommand(CMD_DEL);
	 addCommand(CMD_BACK);

	 setCommandListener( this );

   } 

	 public String getName(){

	 string = fldName.getString();
	 return string;
							}


	public void commandAction(Command c,	 Displayable d) {
		if (c == CMD_BACK) {

		 parent.setCurrnt(parent.inst);
	}

	   if(c == CMD_DEL){
		 fldName.delete(fldName.size()-1, 1);
}

}
}

أتمنى من الجميع المشاركة وأنا لازلت في انتظار الحلول لأني متورطة ومش عارفة ايش الحل؟

#6

أختي هناك خطأ في InstructionEx class حيث أعدتي تعريف ال parent

protected GameMidletEx parent;

ولذلك تكون قيمته null لذلك احذفي هذا السطر وسيعمل البرنامج.

وشيء أخر أنا أعتبر هذا السطر غير جيد برمجيا

protected PlayerNameEx playerNameEx = new PlayerNameEx(parent);

حيث أن ال parent يمكن أن يكون null لذلك أفضل استخدام التالي

playerNameEx = new PlayerNameEx(parent);

في commandAction قبل

parent.setCurrnt(playerNameEx);

أخر ملاحظه هو أنني ضعيف في high level UI لذلك ربما الطريقه السابقه غير مرتبه ويمكنك تعديل GameMidletEx عن طريق اضافه التالي

private static GameMidletEx instance;

	public void startApp(){
		display = Display.getDisplay(this);
		inst = new InstructionEx(this);
		display.setCurrent(inst );
		instance = this;

	}

وبالتي سوف تستطيعين استخدام ال Midlet الرئيسيه في اي وقت عن طريق

GameMidletEx.getInstance().setCurrnt( playerNameEx );

مثلا.

#7

Muqawem أخ

شكرا جزيلا على الردود المفيدة والقيمة

وأشكرك بشكل خاص كونك الوحيد الذي أعطاني إجابة لتساؤلاتي

ولكن للأسف المشرف لم يؤيد الطريقة التي اقترحتها في ان انشيء في كل كلاس مثال من الميدلت كلاس

فهو يقول أن هذه الطريقة غير مسموح بها

لذا سأضطر إلى أن أطبق كل الاوامر في الميدلت كلاس

أتمنى أن أرى مشاركاتك في مواضيع قادمة

وشكرا جزيلا للجميع

#8

السلام عليكم

أولا لا داعي للشكر. وثانيا في أخر مشاركه تراجعت عن الحل السابق و أخبرتك أن توجدي instance من ال Midlet يكون static لتتمكني من الرجوع اليه من جميع الكلاسات الأخرى. فاذا كانت هذه الطريقه غير مجديه أيضا أرجو ايضاح الطريقه المثلى منك أو من المشرف.

وجزى الله الجميع خيرا...

#9

ماأتناقشنا بشكل عميق في الحل الأمثل لأنه غير مسموح له بابداء الحلول لي

ولكن عندما أخبرته بأنني طبقت كل الأوامر في الكلاس ميدلت

قال لابأس بها كمشروع لطلبة البكالوريوس من الناحية الأكاديمية

ولكن إذا كنت مهتمة بالناحية التجارية للتطبيق فهناك حلول أخرى ولكني لم أهتم للسؤال عنها في ذلك الوقت

على العموم قد يكون اقتراحك الأخير مجدي ولكني لم أعرضه على المشرف ولم أطبقه في مشروعي حيث أني اكتفيت بالحل الذي ذكرته

وجزاك الله والجميع خيرا

هذا الموضوع مغلق.

مواضيع مشابهة