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

عندي مشكله ب applet الله خليكم ساعدوني

مغلق
بدأه CS25 في 8 يناير 2007 · 8 رد · 937 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام

لوسمحتم انا عندي هوموورك وعندي مشكله بالحل وهذا الكود بjava applet

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class Hw5 extends Applet   {

	Color   textColor=Color.black;
	Color   backGroundColor;
	MyCanvas  myCanvas = new MyCanvas();  
	Label   label		= new Label("new Phrase:");
	TextField text	  = new TextField(20);
	Choice choice	= new Choice();
	List	  list		   = new List();
	Button black	   = new Button("Black");
	Button blue		 = new Button("Blue");
	Button cyan		= new Button("Cyan");
	Button dark		 = new Button("Dark Gray");
	Button gray		 = new Button("Gray");
	Button green	   = new Button("Green");
	Button light		  = new Button("Light Gray");
	Button magenta  = new Button("Magenta");
	Button orange	 = new Button("Orange");
	Button red		   = new Button("Red");
	Button white		= new Button("White");
	Button yellow	   = new Button("Yellow");

		public void init() {	


		 list.addItem("JAVA is Fun!");
		 list.addItem("Foo + Bar = FooBar");
		 list.addItem("Hello, World!");
		 list.addItem("Goodbye Cruel World");


		 choice.addItem("Black");
		 choice.addItem("Blue");
		 choice.addItem("Cyan");
		 choice.addItem("Dark Gray");
		 choice.addItem("Gray");
		 choice.addItem("Green");
		 choice.addItem("Light");
		 choice.addItem("Magenta");
		 choice.addItem("Orange");
		 choice.addItem("Red");
		 choice.addItem("White");
		 choice.addItem("Yellow");

		 Panel p = new Panel();
		 p.add(choice);
		 p.add(black);
		 p.add(blue);
		 p.add(cyan);
		 p.add(dark);
		 p.add(gray);
		 p.add(green);
		 p.add(light);
		 p.add(magenta);
		 p.add(orange);
		 p.add(red);
		 p.add(white);
		 p.add(yellow);	

		 black.addActionListener(myCanvas);
		 blue.addActionListener(myCanvas);
		 cyan.addActionListener(myCanvas);
		 dark.addActionListener(myCanvas);
		 gray.addActionListener(myCanvas);
		 green.addActionListener(myCanvas);
		 light.addActionListener(myCanvas);
		 magenta.addActionListener(myCanvas);
		 orange.addActionListener(myCanvas);
		 red.addActionListener(myCanvas);
		 white.addActionListener(myCanvas);
		 yellow.addActionListener(myCanvas);		 

		 p.setLayout(new GridLayout(0,1,3,3));	   		 


		 Panel q = new Panel();
		 q.add(label);
		 q.add(text);		 					 						

		 Panel r = new Panel();			 
		 r.add(new MyCanvas());
		 r.add(list);			   
		 r.setLayout(new GridLayout(0,1,3,3));					  

		 setLayout(new BorderLayout(0,10));	 
		 add(BorderLayout.WEST,p);	  
		 add(BorderLayout.SOUTH,q);				
		 add(BorderLayout.CENTER,r);   

	}

class MyCanvas extends Canvas implements ActionListener  {  //.. note: inner class

   public void paint(Graphics g) {

	 int i,k=0; 	
 	 for (i=0;i<getSize().height;i++) { 	 
 		g.setColor(Color.gray);	
 		g.drawLine(0,k,k,0);
 		k=k+13;
 	 }

 	 k=0; 	
 	 int x=getSize().height;
 	 for (i=0;i<getSize().height;i++) { 	 
 		g.setColor(Color.gray);	
 		g.drawLine(0,x,k,getSize().height);
 		k=k+13;
 		x=x-13;
 	 }

 	 g.setColor(textColor); 
 	 g.drawString("Hello World!", getSize().width/2,getSize().height/2);			 	 

   } //end of paint method

	 public void actionPerformed(ActionEvent e){

		if (e.getSource() instanceof Button) {
			Button x = (Button) (e.getSource());

			if (x==black) {
				textColor=Color.black;			
			}

			if (x==blue) {
				textColor=Color.blue;				
			}

			if (x==cyan) {
				textColor=Color.cyan;				
			}

			if (x==dark) {
				textColor=Color.darkGray;				
			}

			if (x==gray) {
				textColor=Color.gray;	
			}

			if (x==green) {
				textColor=Color.green;				
			}

			if (x==light) {
				textColor=Color.lightGray;			
			}

			if (x==magenta) {
				textColor=Color.magenta;
			}

			if (x==orange) {
				textColor=Color.orange;
			}

			if (x==red) {
				textColor=Color.red;			
			}

			if (x==white) {
				textColor=Color.white;				
			}

			if (x==yellow) {
				textColor=Color.yellow;		
			}  

		repaint();

		} //end of actionPerformed
	}

 }		

}

عباره عن مجموعه من components عندي مشكله بالbuttons الي على يسار شاشه

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

اهوه شغال بس يشتغل اللون كتابه الي اختاره لمن اغير حجم applet

بس لمن يكون applet ثابت لون كتابه مايتغير

اتمنى احد يرد بسرعه تسليمه اليوم:)

#2
package hw5;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class Hw5 extends Applet
{
	private Hw5 temp;
	private Color textColor = Color.black;
	Color backGroundColor;
	MyCanvas myCanvas = new MyCanvas();
	Label label = new Label("new Phrase:");
	TextField text = new TextField(20);
	Choice choice = new Choice();
	List list = new List();
	Button black = new Button("Black");
	Button blue = new Button("Blue");
	Button cyan = new Button("Cyan");
	Button dark = new Button("Dark Gray");
	Button gray = new Button("Gray");
	Button green = new Button("Green");
	Button light = new Button("Light Gray");
	Button magenta = new Button("Magenta");
	Button orange = new Button("Orange");
	Button red = new Button("Red");
	Button white = new Button("White");
	Button yellow = new Button("Yellow");

	public void init()
	{
		temp = this;

		list.addItem("JAVA is Fun!");
		list.addItem("Foo + Bar = FooBar");
		list.addItem("Hello, World!");
		list.addItem("Goodbye Cruel World");

		choice.addItem("Black");
		choice.addItem("Blue");
		choice.addItem("Cyan");
		choice.addItem("Dark Gray");
		choice.addItem("Gray");
		choice.addItem("Green");
		choice.addItem("Light");
		choice.addItem("Magenta");
		choice.addItem("Orange");
		choice.addItem("Red");
		choice.addItem("White");
		choice.addItem("Yellow");

		Panel p = new Panel();
		p.add(choice);
		p.add(black);
		p.add(blue);
		p.add(cyan);
		p.add(dark);
		p.add(gray);
		p.add(green);
		p.add(light);
		p.add(magenta);
		p.add(orange);
		p.add(red);
		p.add(white);
		p.add(yellow);

		black.addActionListener(myCanvas);
		blue.addActionListener(myCanvas);
		cyan.addActionListener(myCanvas);
		dark.addActionListener(myCanvas);
		gray.addActionListener(myCanvas);
		green.addActionListener(myCanvas);
		light.addActionListener(myCanvas);
		magenta.addActionListener(myCanvas);
		orange.addActionListener(myCanvas);
		red.addActionListener(myCanvas);
		white.addActionListener(myCanvas);
		yellow.addActionListener(myCanvas);

		p.setLayout(new GridLayout(0, 1, 3, 3));

		Panel q = new Panel();
		q.add(label);
		q.add(text);

		Panel r = new Panel();
		r.add(new MyCanvas());
		r.add(list);
		r.setLayout(new GridLayout(0, 1, 3, 3));

		setLayout(new BorderLayout(0, 10));
		add(BorderLayout.WEST, p);
		add(BorderLayout.SOUTH, q);
		add(BorderLayout.CENTER, r);

	}

	public void paint(Graphics g)
	{
		super.paint(g);
		/*int i, k = 0;
	  for (i = 0; i < getSize().height; i++) {
		g.setColor(Color.gray);
		g.drawLine(0, k, k, 0);
			k = k + 13;
		  }

		  k = 0;
		  int x = getSize().height;
		  for (i = 0; i < getSize().height; i++) {
			g.setColor(Color.gray);
			g.drawLine(0, x, k, getSize().height);
			k = k + 13;
			x = x - 13;
		  }*/

				g.setColor(textColor);
				g.drawString("Hello World!", (getSize().width / 2),
				 (getSize().height / 2) - 100);

	} //end of paint method

	class MyCanvas extends Canvas implements ActionListener
	{ //.. note: inner class
		public void actionPerformed(ActionEvent e)
		{

			if (e.getSource() instanceof Button)
			{
				String x = e.getActionCommand();

				if (x.equals("Black"))
				{
					textColor = Color.black;
				}

				else if (x.equals("Blue"))
				{
					textColor = Color.blue;
				}

				else if (x.equals("Cyan"))
				{
					textColor = Color.cyan;
				}
				else if (x.equals("Dark Gray"))
				{
					textColor = Color.darkGray;
				}

				else if (x.equals("Gray"))
				{
					textColor = Color.gray;
				}

				else if (x.equals("Green"))
				{
					textColor = Color.green;
				}

				else if (x.equals("Green"))
				{
					textColor = Color.lightGray;
				}

				else if (x.equals("Magenta"))
				{
					textColor = Color.magenta;
				}

				else if (x.equals("Orange"))
				{
					textColor = Color.orange;
				}

				else if (x.equals("Red"))
				{
					textColor = Color.red;
				}

				else if (x.equals("White"))
				{
					textColor = Color.white;
				}

				else if (x.equals("Yellow"))
				{
					textColor = Color.yellow;
				}
				temp.repaint();
			} //end of actionPerformed
		}
	}
}

و كن فتى في ذرى العلياء همّته

يسمو بغاياته حتى على زحل

موقع خاص بي

My Website

قمت بتصميم لعبة بسيطة بإستخدام اللغة الجميلة الجافا

Plane Fighter Game

برنامج جميل بلغة الجافا يساعد على تنظيم المشاكل

The TS Organizer

#3

مشكور اخي الكريم عالمحاوله ولكن البرنامج لايعمل بشكل صحيح بعد التعديل الي سويته

#4

قلت بأن Buttons لا تقوم بتغيير الألوان

جربته عندي فلم يعملوا أيضا فقمت بتعديل ذلك فقط، فالكود شغال حاول إزالة سطر package hw5

و كن فتى في ذرى العلياء همّته

يسمو بغاياته حتى على زحل

موقع خاص بي

My Website

قمت بتصميم لعبة بسيطة بإستخدام اللغة الجميلة الجافا

Plane Fighter Game

برنامج جميل بلغة الجافا يساعد على تنظيم المشاكل

The TS Organizer

#5

ازلته والوضع نفس ما هو عليه

#6

لا أعلم ما المشكلة و لكن أعدت كتابة برنامجك بطريقة أخرى، جربها

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HW5 extends Applet {

	private Label label;
	private TextField text;
	private Choice choice;
	private List list = new List();
	private Color color[];
	private String[] colorName;
	private Button[] button;
	private Panel leftPanel;
	private Panel centerPanel;
	private Panel bottomPanel;
	private Label coloredTextLabel;

	public void init() {
		this.setLayout(new BorderLayout());

		color = new Color[] {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
		Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA,
		Color.ORANGE, Color.RED, Color.WHITE, Color.YELLOW};
		colorName = new String[] {"Black", "Blue", "Cyan", "Dark Gray", "Gray",
		"Green", "Light Gray", "Magneta", "Orange", "Red", "White",
		"Yellow"};
		button = new Button[12];
		choice = new Choice();

		leftPanel = new Panel();
		leftPanel.setLayout(new GridLayout(13, 1));
		leftPanel.add(choice);

		for (byte index = 0; index < button.length; index++) {
			button[index] = new Button(colorName[index]);
			button[index].addActionListener(new ButtonEvent(this));
			leftPanel.add(button[index]);

			choice.add(colorName[index]);
		}
		this.add(leftPanel, BorderLayout.WEST);

		list.addItem("JAVA is Fun!");
		list.addItem("Foo + Bar = FooBar");
		list.addItem("Hello, World!");
		list.addItem("Goodbye Cruel World");

		bottomPanel = new Panel();
		text = new TextField(20);
		label = new Label("new Phrase:");
		bottomPanel.add(label);
		bottomPanel.add(text);
		this.add(bottomPanel, BorderLayout.SOUTH);

		centerPanel = new Panel(new GridLayout(2, 1));
		coloredTextLabel = new Label("Hello World");
		coloredTextLabel.setAlignment(Label.CENTER);
		centerPanel.add(coloredTextLabel);
		centerPanel.add(list);
		this.add(centerPanel, BorderLayout.CENTER);
	}

	public String[] getAllColorName() {
		return colorName;
	}

	public String getColorName(byte index) {
		return colorName[index];
	}

	public Color[] getAllColor() {
		return color;
	}

	public Color getColor(byte index) {
		return color[index];
	}

	public void setColoredTextLabel(Color color) {
		coloredTextLabel.setForeground(color);
	}

	private final class ButtonEvent implements ActionListener {
		private HW5 temp;
		public ButtonEvent(HW5 hw5) {
			temp = hw5;
		}
		public void actionPerformed(ActionEvent ee) {
			for (byte index = 0; index < temp.getAllColorName().length; index++) {
				if (temp.getColorName(index).equals(ee.getActionCommand())) {
					temp.setColoredTextLabel(temp.getColor(index));
				}
			}
		}
	}
}

و كن فتى في ذرى العلياء همّته

يسمو بغاياته حتى على زحل

موقع خاص بي

My Website

قمت بتصميم لعبة بسيطة بإستخدام اللغة الجميلة الجافا

Plane Fighter Game

برنامج جميل بلغة الجافا يساعد على تنظيم المشاكل

The TS Organizer

#7

شكرا اشتغل وياي

مشكور اخي كريم

بس عندي سؤال ويريت تشرح حقي هالكود

	private HW5 temp;
		public ButtonEvent(HW5 hw5) {
			temp = hw5;
		}

لمن عرفت عنصر temp من نوع HW5 شنو معناة هالحركه

تم تعديل هذه المشاركة بواسطة CS25 في 11 يناير 2007 في 05:44

#8

أنظر إلى هذا الكود داخل init لقد قمت بإرسال this أي reference للكلاس HW5 كي أستطيع إستخدام methods التي بداخله

button[index].addActionListener(new ButtonEvent(this));

و لكنك تستطيع إستخامها من دون الحركة التي قمت بها، لأنك تستطيع إستخدام public للكلاس الخارجي في الكلاس الداخلي

و لكن إعتقدت بأنها أسهل للفهم، و هذا الكود من دونها مع تعديل أنني لا أحتاج إلى Object جديد في كل مرة أضيف فيها Listener إلى Button

private ButtonEvent buttonEvent;

buttonEvent = new ButtonEvent();
for (byte index = 0; index < button.length; index++) {
	button[index] = new Button(colorName[index]);
	button[index].addActionListener(buttonEvent);
	leftPanel.add(button[index]);

	choice.add(colorName[index]);
}

private final class ButtonEvent implements ActionListener {
		public ButtonEvent() { }
		public void actionPerformed(ActionEvent ee) {
			for (byte index = 0; index < getAllColorName().length; index++) {
				if (getColorName(index).equals(ee.getActionCommand())) {
					setColoredTextLabel(getColor(index));
				}
			}
		}
}

تم تعديل هذه المشاركة بواسطة Final Heaven في 11 يناير 2007 في 09:48

و كن فتى في ذرى العلياء همّته

يسمو بغاياته حتى على زحل

موقع خاص بي

My Website

قمت بتصميم لعبة بسيطة بإستخدام اللغة الجميلة الجافا

Plane Fighter Game

برنامج جميل بلغة الجافا يساعد على تنظيم المشاكل

The TS Organizer

#9

شكرا

لك

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

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