andaohiresakaobjetfahaxiii [wiki ifg]
Writing /home/infogasy/domains/wiki.infogasy.net/public_html/data/cache/2/2af15d9ed36ff5aaa7958b54cbbe3eda.xhtml failed

Daty : 24/03/2005 Mpanoratra : Tambanivohitra.com

Andao Hiresaka Objet Faha XIII

Ohatra fampiharana amin’ny fanorenana manao andalan-tohatra :

Jery todika :

Raha atao jery todika ary ireo vainga kilasy natsangantsika hatreto ka hifantenana ireo izay ho ilaintsika handrafetana ohatra fampiharana iray dia ireto avy:

  • Kilasy ZavatraAzoKajiana.java : ho entina manao asa fampitambarana (adisanina raha isa, fanatovanana raha soratra)
  • Kilasy TahirySoratraASCII.java : ho entina mitahiry maharitra ireo zavatra kirakiraina karazana soratra, dia tsy inona fa ny tambatra ireo lahasoratra rehetra nampidirina hatrizay.
  • Kilasy SehoLahasoratra.java : ho entina maneho ety ivelany ny fifandraisana amin’ilay praograma fampiharana dia tsy inona eto amin’ity ohatra ity fa:

?fandefasana lahasoratra avy amin’ny toerana fandraisana izany (zava-madinika TextArea ato ambany), mba hivondrona amin’ilay zava-madinika TextArea ao ambony?

Ireo kilasy telo ireo moa dia efa nasiana fanazavana tamin’ireo resaka tany aloha, fa ny hifotorana eto dia hoe ahoana ary izany ny fomba hampiarahana azireo mba ho afaka miara-miasa, ka hanatrarana ilay tanjona voafaritra io ambony io.

Rafitra kisarisary fanazavana

Azo aseho toa izao angamba ny rafitra mampifandray azireo, sy ny fifanakalozana zavatra kirakiraina isan?andalana rafitra :

Zavatra vitsy tsara marihina eto mba hanajana ny fanorenana miandalan-tohatra dia ireto:

  • Rehefa voafaritra fa ireo ny fifandraisany dia tsy tokony ho atao ny mandingana andalana rehefa mampita zavatra kirakiraina isika amin’ireo rafitra ireo (ohatra: tsy tokony hatao ny tonga dia mandefa anaty tahiry maharitra izay vaoray tanaty zava-madinika ao amin’ny kilasy fanehoana na GUI)
  • amin’ny alalan?ireo getters sy setters n?ireo kilasy tsirairay avy no tokony hifampitana ireo zavatra mifamezivezy amin’ireo andalana ireo. Raha fotorina lalina izay fomba izay dia azo atao ny mandraikitra maharitra fa : ?ireo setters avy amin’ny andalana iray dia tokony ho afaka manaiky tsara ho arguments izay avoakan’ny getters amin’ny andalana manakaiky azy?

Rafitra kisarisary manampy amin'ny fanoratana ny fiaraha-miasan?ireo kilasy

Dia ahoana indray ary ny tokony hatao rehefa soratana anaty kilasy iray ny fiaraha-miasan?ireo kilasy manorina ilay praogramantsika ireo ?

Ilay etsy ambony mantsy dia natao hanazavana fotsiny hoe: manao ahoana ireo andalany, fa tsy nisy hirika hanampy antsika tao hoe: toa izao no tokony handravonana ka hanoratana mihitsy anaty kilasy iray ilay faraha-miasa tadiavina.

Ho fitadidiana aloha dia tsara ny mamerina fa misy 3 ny mety ho fiarahan’ny kilasy iray miaraka miasa amina kilasy hafa :

  • Fandovana (heritage) avy amin’ny kilasy hafa zay ampifandraisina amin’ny extends na implements (raha interface ilay kilasy hafa)
  • Fifanampiana, fiarahana (composition, association na aggregation) amin’ilay kilasy hafa; io moa dia fampidirana fotsiny ho anisan’ny attribut membre- ny kilasintsika ireo kilasy hafa.
  • Fampidirana anaty (classe interne) ilay kilasy hafa mba ho voafaritra ao anatin’ny kilasintsika.

Ity ary misy sary mety hanampy antsika amin’ny fanoratana no sady tsy dia miala lavitra ilay sary tetsy ambony.

Ilay sary sokera mainty dia manambara fa composition no iaraha-miasan?ilay kilasy ialan’ny tsipika amin’ilay kilasy iantefan?ilay sokera mainty. Kisarisary toa io moa no ifotoran’ny fomba fanehoana ny fampiasana ny UML na (Langage de Modelisation Unifiee) entina manatomotra fanorenana tetikasa informatika.

Raha tsorina izany dia ao anaty kilasy SehoLahasoratra ihany no hataontsika misy ilay hetsika main(String arg[]) mahazatra ka io kilasy io no fidirana raha vao mihodina ilay praograma.

Rehefa nasiana fanovana kely ary ilay fanehoana tary aloha dia toa izao no fisehoany:

Niampy bokotra iray fotsiny ery ambony, mba ahafahana mampiditra amin’ilay toerana fijerena ao ambony ireo lahasoratra efa voarakitra taloha. Miampy Component (Button) iray araka izay izany ilay LayoutManager namboarina ho an?ity fampiasana ity: LahasoratraLayout (tandremana tsara ny filaharany rehefa ampidirina anaty Container)

Dia toa izao ary izany ny tokony ho ao anaty kilasy SehoLahasoratra.java miaraka miasa amin’ny kilasy ZavatraAzoKajiana.java sy TahirySoratraASCII.java

 
package resakaObjet;
import java.awt.*;
import java.awt.event.* ;
import javax.swing.*;
import resakaObjet.TahirySoratraASCII; // fampiasana ny fonosana ilaina @ fitehirizana
import resakaObjet.ZavatraAzoKajiana; // fampiasana ny fonosana ilaina @ asa fanao
 
public class SehoLahasoratra extends Frame
{
	Label lohateny1, lohateny2;
	Button bokotra1, bokotra2, bokotra3;
	TextArea tahirySoratra;
	TextArea lahasoratra;
	
	TahirySoratraASCII tahiryMaharitra; //  Atao attribut-n?ity kilasy ity ny Objet tahiryMaharitra
	ZavatraAzoKajiana zavatra; // Atao attribut-n?ity kilasy ity ny Objet zavatra
	
	public SehoLahasoratra()
	{
		super("Fanoratana lahasoratra");
		lohateny1 = new Label("Lahasoratra taloha :");
		tahirySoratra = new TextArea(20,100);
		lohateny2 = new Label("Lahasoratra vaovao :");
		
		lahasoratra = new TextArea(20,100);
		bokotra1 = new Button("Alefaso");
		bokotra2 = new Button("Fafao");
		bokotra3 = new Button("Vakio"); // Ilay bokotra fanampiny: fijerena ireo lahasoratra taloha
		/* Famoronana ireo Objet tokony hiara-miasa amin'ity kilasy ity. ?tahiry.dat? no anaran?ilay fitehirizana lahasoratra anaty milina */
		tahiryMaharitra = new TahirySoratraASCII(".","tahiry.dat","");
		zavatra = new ZavatraAzoKajiana("");
		
		setLayout(new lahasoratraLayout());
		
		/* Tsy maintsy hajaina ny filaharan'ny fampidirana ireo zava-madinika rehetra ampiasaina anaty fanehoana */
		add(lohateny1); add(bokotra3); add(tahirySoratra);
		add(lohateny2); add(lahasoratra);
		add(bokotra1); add(bokotra2);
		
		pack();
		addWindowListener(this.new RindraToerana());
	}
	
	public TextArea getTahirySoratra()
	{
		return tahirySoratra;
	}
	
	public void setHainoTranga()
	{
		bokotra1.addActionListener(this.new RindraTranga());
		bokotra1.setActionCommand("1");
		bokotra2.addActionListener(this.new RindraTranga());
		bokotra2.setActionCommand("2");
		bokotra3.addActionListener(this.new RindraTranga());
		bokotra3.setActionCommand("3");
	}
	
	private class RindraTranga implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String action = e.getActionCommand();
			int choix = Integer.parseInt(action);
			switch(choix)
			{
			case 1:
				try {
					StringBuffer buf = (StringBuffer)tahiryMaharitra
					.mamakyTahiry();
					zavatra.setSanda(buf.toString());
					zavatra.setSanda(zavatra
							.kajyAdisanina("\n==========\n"));
					zavatra.setSanda(zavatra
							.kajyAdisanina(lahasoratra.getText()));
					
					tahiryMaharitra.manoratraTahiry((String)zavatra.getSanda());
					tahirySoratra.setText((String)zavatra.getSanda());
					lahasoratra.setText("");
				}
				catch (Exception ex)
				{
					break;
				}
				break;
			case 2:
				lahasoratra.setText("");
				break;
			case 3:
				tahirySoratra.setText(tahiryMaharitra
						.mamakyTahiry()
						.toString());
				break;
			}
		}
	}
	
	private class lahasoratraLayout implements LayoutManager
	{
		
		public lahasoratraLayout()
		{
		}
		
		public void addLayoutComponent(String name, Component comp)
		{
		}
		
		public void removeLayoutComponent(Component comp)
		{
		}
	}
	
	
	public Dimension preferredLayoutSize(Container parent)
	{
		Dimension dim = new Dimension(0, 0);
		
		Insets insets = parent.getInsets();
		dim.width = 494 + insets.left + insets.right;
		dim.height = 437 + insets.top + insets.bottom;
		
		return dim;
	}
	
	public Dimension minimumLayoutSize(Container parent)
	{
		Dimension dim = new Dimension(0, 0);
		return dim;
	}
	
	public void layoutContainer(Container parent)
	{
		Insets insets = parent.getInsets();
		
		Component c;
		c = parent.getComponent(0);
		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,176,24);}
		c = parent.getComponent(1);
		if (c.isVisible()) {c.setBounds(insets.left+185,insets.top+8,72,24);}
		c = parent.getComponent(2);
		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,472,152);}
		c = parent.getComponent(3);
		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,184,24);}
		c = parent.getComponent(4);
		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+232,480,160);}
		c = parent.getComponent(5);
		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+400,72,24);}
		c = parent.getComponent(6);
		if (c.isVisible()) {c.setBounds(insets.left+104,insets.top+400,72,24);}
	}
}
 
private class RindraToerana extends WindowAdapter
{
	public void windowClosing(WindowEvent ev)
	{
		System.exit(0);
	}
}
 
static public void main(String arg[])
{
	SehoLahasoratra test = new SehoLahasoratra();
	test.setHainoTranga();
	test.getTahirySoratra().setEditable(false);
	test.setVisible(true);
}
 

Fanazavana:

Raha asiana fanazavana kely ny zavatra niova amin’ity kilasy SehoLahasoratra ity dia ny fidiran’ny Objet roa tahiryMaharitra sy zavatra naorina avy amin’ireo kilasy TahirySoratraASCII sy ZavatraAzoKajiana

Ny tsara hojerena akaiky koa angamba dia ilay zavatra atao rehefa tsindriana iray amin’ireo bokotra telo hitantsika ireo:

      case 1: // tsindry bokotra Alefaso
         try {
             StringBuffer buf = (StringBuffer)tahiryMaharitra.mamakyTahiry();
             zavatra.setSanda(buf.toString());
             zavatra.setSanda(zavatra.kajyAdisanina("\n==========\n"));
             zavatra.setSanda(zavatra.kajyAdisanina(lahasoratra.getText()));
 
             tahiryMaharitra.manoratraTahiry((String)zavatra.getSanda());
             tahirySoratra.setText((String)zavatra.getSanda());
             lahasoratra.setText("");
         }
         catch (Exception ex)
         {
             break;
         }
         break;
  1. Vakiana avy any anaty tahiry fichier ?tahiry.dat? anaty milina ireo lahasoratra voatahiry taloha dia apetraka anaty fitehirizana mandalo StringBuffer: buf aloha
  2. Ampidirina anaty Objet zavatra ireo lahasoratra taloha ireo. Asiana tsipika “=========” manasaraka ireo lahasoratra taloha sy izay mbola hampidirina
  3. Atao ny adisanina soratra ary mbola ao anaty Objet zavatra ihany no hitehirizina ny vokatra; izany hoe : lahasoratra taloha miampy ny vaovao: zavatra.setSanda(zavatra.kajyAdisanina(lahasoratra.getText()));
  4. Soratana anaty tahiry maharitra ireo lahasoratra manontolo ao anaty Objet zavatra; dia tsy hadino ny maneho koa io lahasoratra manontolo io anaty zava-madinika TexteArea ambony, dia fafana ilay ao ambany rehefa vita izay rehetra izay:
       tahiryMaharitra.manoratraTahiry((String)zavatra.getSanda());
       tahirySoratra.setText((String)zavatra.getSanda());
       lahasoratra.setText("");

Satria moa misy E/S eto miasa, sy koa ilay hestika kajyAdisanina(Objet) mety hisy kiana, ka noho izany dia tsy maintsy asiana fifehezana kiana izany ao: try { ? } catch { ? }

   case 2: // bokotra Fafao
               lahasoratra.setText("");
               break;

Famafana izay ao anaty zava-madinika TextArea ambany

     case 3: // bokotra Vakio
                 tahirySoratra.setText(tahiryMaharitra.mamakyTahiry().toString());
                 break;

Famakiana ireo lahasoratra taloha ho ao anaty zava-madinika TextArea Ambony

Dia toa izao ary ny fiasan?ilay praogramantsika amin’ny andrana natao eto :


Lahatsoratra Java

 

andaohiresakaobjetfahaxiii.txt · Novaina farany:: 2008/09/11 12:43