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

Error:class or interface expected

مغلق
بدأه microsmart1 في 14 نوفمبر 2006 · 3 رد · 773 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;














   class MyMatching extends JFrame
{


	 final JTextField txt1=new JTextField("Name1",10);



 	JLabel lbl3=new JLabel("Love");





	 final  JTextField txt2=new JTextField("Name2",10);



	JButton btn=new JButton("Result");
	JTextField txt3=new JTextField("%",5);



	JTextArea ta=new JTextArea(10,40);






	JPanel pnl1=new JPanel();
	JPanel pnl2=new JPanel();




	MyInner inner;

	MyMatching()
	{
		setupGUI();
	}

	private void setupGUI()
	{


		setSize(500,300);
		setTitle("Love Matching v1");
		Container c=getContentPane();





		c.setLayout(new BorderLayout());





		pnl1.setLayout(new FlowLayout());


		 pnl1.add(txt1);

		 pnl1.add(lbl3);

		 pnl1.add(txt2);

		 pnl1.add(btn);
		 pnl1.add(txt3);

		 pnl2.add(ta);

		 c.add("North",pnl1);
		 c.add("Center",pnl2);



		  setVisible(true);

		  inner=new MyInner ();	

		  btn.addActionListener( inner);

		  }

		  }

					 class MyInner implements ActionListener	  
   {


	 public void actionPerformed(ActionEvent e)
	 {



		  if(!txt1.getText().equals("Name1") && !txt2.getText().equals("Name2"))
		  {




			 	if (e.getActionCommand().equals("Result"))
				{


//First process
int random= ((int)(50*Math.random()))+50;
 txt3.setText(random+"%");

 //second process

String  msg[ ]={"Love Can Touch Us One Time And Last For The Life Time\nYou Are A True Lovers.","Your Feelings Are Different \nYou Should Forget it."
,"Love Is A Passion\nYou Must Be More Romantic.","You Are An Angel\n But Your Lover Is A Liar."
,"You Must Think Deeply Before That Choice\nLove Is Not An Entertainment.","Do Not Lie To Your Hart\nYou Still Love Your First Lover."
,"You Cannot Love Again\nYour First Lover is So Lovely And Cannot Be Forotten."}; 






		ta.setText(  msg[((int)( msg.length*Math.random()))]	);
		/* breaking up the code
		double random = Math.random();
		int index = (int) (msg.length*random);
	   String message = msg[index];
	   ta.setText(message);*/

		ta.setForeground(Color.RED);

			   }






		 else
		  {

		   MyFrame2.this.c2.setSelected(true); 


		  }

		  }

		  }

		  }


	class MyFrame2 extends JFrame

 {
 	ImageIcon img=new ImageIcon("D:\\Mastering Java\\Java Projects\\Icons\\alert_lrg.gif ");

	JLabel lbl=new 	JLabel(" You Must Enter A Name!",img,JLabel.LEFT);

	 JPanel pnl3=new JPanel();

	 MyFrame2()
	 {
	 	setupgui();	 	
	 }

	 private void setupgui()

	 {
	 	setSize(220,130);
	 	setTitle("Error");


	 	Container c2=getContentPane();;


	 	pnl3.setLayout(new BorderLayout());


		lbl.setIcon(img);

	   	pnl3.add("Center",lbl);

	   	c2.add(pnl3);

	   	setVisible(true);


	 }   

	 }	 



			public	   static void main(String[]args)
	{
		MyMatching mtch=new MyMatching();
		 MyFrame2 frm=new  MyFrame2();
	}

so sir here how can i organize my classes

here i have 4 classes

1-class MyMatching //containing the first frame

2-class MyFrame2//containing the second frame

3-class MyInner //containing listeners for the two prevoius classes

4-the main class

****in the inner class i have to use objects from the first two classe***

what would be the approbriate organization for the classes?

who contains other?

thank you all

#2

هلا بك اخوي

في اكثر من طريقه لحل المشكله

- انت مستخدم Inner Class وما يقدر يوصل للمتغيرات txt1 , txt2 داخل ال Frame

- بامكانك أنشاء كلاس خاص بال Listener وتمرير ال Frame للكلاس ومن خلاله توصل للمتغيرات

او بامكانك استخدام Anonmuse Class كلاس بدون اسم داخل ال Method Parameter والمثال بيوضحلك

Frame1.addActionListener (
new ActionListener()   {

public void actionPaerformed(ActionEvent e)
{

	  // Now U can get To The Class Variables 


}

}


);

وبكذا تقدر توصل للمتغيرات الي داخل الكلاس لاحظ انو الاكشن الحين مخصص لل frame فقط

اذا فكرت انك تفصل اكشن عن بعض بامكانك استخدام اكثر من Annoumous Class او عن طريق

تم تعديل هذه المشاركة بواسطة _Developer_ في 14 نوفمبر 2006 في 21:24

#3

جزاك الله خيرا اخى على الاهتمام

شوف انا جربت طريقة اخرى وفصلت كل كلاس فى ملف منفصل

الكلاس الأول

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;


	 public	  class MyMatching extends JFrame
{


	 final JTextField txt1=new JTextField("Name1",10);



 	JLabel lbl3=new JLabel("Love");





	 final  JTextField txt2=new JTextField("Name2",10);



	JButton btn=new JButton("Result");
	JTextField txt3=new JTextField("%",5);



	JTextArea ta=new JTextArea(10,40);






	JPanel pnl1=new JPanel();
	JPanel pnl2=new JPanel();




	MyInner inner;

	MyMatching()
	{
		setupGUI();
	}

	private void setupGUI()
	{


		setSize(500,300);
		setTitle("Love Matching v1");
		Container c=getContentPane();





		c.setLayout(new BorderLayout());





		pnl1.setLayout(new FlowLayout());


		 pnl1.add(txt1);

		 pnl1.add(lbl3);

		 pnl1.add(txt2);

		 pnl1.add(btn);
		 pnl1.add(txt3);

		 pnl2.add(ta);

		 c.add("North",pnl1);
		 c.add("Center",pnl2);



		  setVisible(true);

		  inner=new MyInner ();	

		  btn.addActionListener( inner);



		  }   


	 }

الكلاس الثانى

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;





   public	  class MyFrame2 extends JFrame

 {
 	ImageIcon img=new ImageIcon("D:\\Mastering Java\\Java Projects\\Icons\\alert_lrg.gif ");

	JLabel lbl=new 	JLabel(" You Must Enter A Name!",img,JLabel.LEFT);

	 JPanel pnl3=new JPanel();

	 MyFrame2()
	 {
	 	setupgui();	 	
	 }

	 private void setupgui()

	 {
	 	setSize(220,130);
	 	setTitle("Error");


	 	Container c2=getContentPane();;


	 	pnl3.setLayout(new BorderLayout());


		lbl.setIcon(img);

	   	pnl3.add("Center",lbl);

	   	c2.add(pnl3);

	   	setVisible(true);


	 } 





	 }

الكلاس الثالث

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;





			public		 class MyInner implements ActionListener	  
   {


   	   	   public  void 	setMyMatching(MyMatching match)
	 	 {

		  this.match =match;

		 }



	 public void actionPerformed(ActionEvent e)
	 {




















		  if(!txt1.getText().equals("Name1") && !txt2.getText().equals("Name2"))
		  {




			 	if (e.getActionCommand().equals("Result"))
				{


//First process
int random= ((int)(50*Math.random()))+50;
 txt3.setText(random+"%");

 //second process

String  msg[ ]={"Love Can Touch Us One Time And Last For The Life Time\nYou Are A True Lovers.","Your Feelings Are Different \nYou Should Forget it."
,"Love Is A Passion\nYou Must Be More Romantic.","You Are An Angel\n But Your Lover Is A Liar."
,"You Must Think Deeply Before That Choice\nLove Is Not An Entertainment.","Do Not Lie To Your Hart\nYou Still Love Your First Lover."
,"You Cannot Love Again\nYour First Lover is So Lovely And Cannot Be Forotten."}; 






		ta.setText(  msg[((int)( msg.length*Math.random()))]	);
		/* breaking up the code
		double random = Math.random();
		int index = (int) (msg.length*random);
	   String message = msg[index];
	   ta.setText(message);*/

		ta.setForeground(Color.RED);

			   }






		 else
		  {

		   MyFrame2.this.c2.setSelected(true); 


		  }

		  }

		  }

		  }

طبعا فى الكلاس الثالث inner اريد استخدام متغيرات من الكلاس الاول فقمت بالربط بين الكلاسين عن طريق

	   public  void 	setMyMatching(MyMatching match)
	 	 {

		  this.match =match;

		 }

ولكن الخطأ يكون

cannot trsolve simple variable match,txt1,txt2,txt3,ta

اى انه يتجاهل الميثود ولا يربط بين الكلاسين لماذا؟

وكيف أصحح الخطأ؟

ثانيا اريد استخدام متغيرات من الكلاس الثانى MyFrame2 فى الكلاس الثالث MyInner

فى هذا الكود

MyFrame2.this.c2.setSelected(true);

ولكن يظهر لى خطأ

not an enclosing class MyFrame2

مع اننى لم اقم بعمل nested classes

فما العمل مع هذان المشكلتان؟

جزيل الشكر مقدما

#4

مازلت بانتظار ارائكم اخوانى

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

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