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

سؤال في الgui ,والfile

بدأه سمو الروحـ في 14 مايو 2012 · 2 رد · 407 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته ..

أتمنى مساعدتي فلدي برنامج يتكون من إنشاء نافذة لجعل المستخدم يدخل حجم المصفوفة التي سأستفيد منها في حفظ الأوبجيكت المتكون في الtexeArea

بشكل عام البرنامج يتكون من 4 كلاسات الأول للأسطوانة والثاني للشكل حيث كلاس الشكل يحوي مصفوفة من نوع الأسطوانة , الثالث كلاس لتكوين نافذة عند حدوث خطأ في الإدخال و الرابع كلاس المين وهو الذي سيكون به النوافذ وأزرار النقر سنكون به frame يحوي صورة و اثنين textField و textArea بحيث أن المستخدم يدخل الارتفاع ونصف القطر للاسطوانه وأمامه زرين للاختيار الحجم والمساحة .. بحسب ضغطه على ايهم سيظهر الناتج في textArea ومايتكون فيها يجب حفظه في المصفوفة ... لكن مشكلتي هنا : عند إغلاق النافذة من قبل المستخدم يتطلب مني نقل البيانات المخزنة في المصفوفة للأوبجيت من نوع أسطوانة في ملف من نوع txtfile ...

يسير البرنامج بشكل صحيح لكنه لا يكتب البيانات في الملف من نوع txt هذه الخطوة لم تضبط معي, لا أعلم إن كان هناك من خطوة تدع الأمر ينفذ بعد إغلاق النافذة من قبل المستخدم !

وهذا هو كود المين :

   import java.util.*;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.io.*;
   public class Project extends JFrame implements ActionListener{
      private static final int FRAME_WIDTH    = 250;
      private static final int FRAME_HEIGHT   = 500;
      private static final int FRAME_X_ORIGIN = 500;
      private static final int FRAME_Y_ORIGIN = 150;
      private JButton valumeButton;
      private JButton surfaceAreaButton;
      private JTextField height;
      private JTextField radius;
      private JTextArea  textArea;
      private JScrollPane scrollText;
      static Scanner input=new Scanner (System.in);
      static int Asize;
      static Shapes sh;

      public static void main (String[]args)throws FileNotFoundException,IOException{
         Project frame = new Project();
         frame.setVisible(true);

         PrintWriter b=new PrintWriter(new FileOutputStream(new File("Array's conents.txt")));
         for(int i=0;i<sh.getnumShapes();i++)
            b.println(sh.getCylinder().toString());//end loop
         b.close();
      }//end main.

      public Project(){
         String message = JOptionPane.showInputDialog( null,"Enter size of the array to store the lines of output in file" );
         Asize= Integer.parseInt(message);// to get the size of the array.
         sh = new Shapes(Asize);
         Container contentPane;
         setSize(FRAME_WIDTH, FRAME_HEIGHT);
         setResizable (false);
         setTitle("Cylinder");
         setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

         contentPane = getContentPane( );
         contentPane.setLayout(new FlowLayout());
         JLabel image = new JLabel(new ImageIcon("Cylinder.gif"));
         contentPane.add(image);

         JLabel hLabel = new JLabel();
         hLabel.setText("Height:");
         hLabel.setSize(25,25);
         contentPane.add(hLabel);

         height= new JTextField();
         height.setColumns(15);
         contentPane.add(height);
         height.addActionListener(this);

         JLabel rLabel = new JLabel();
         rLabel.setText("Radius");
         rLabel.setSize(25,25);
         contentPane.add(rLabel);

         radius= new JTextField();
         radius.setColumns(15);
         contentPane.add(radius);
         radius.addActionListener(this);

      //create and place two buttons on the frame
         valumeButton = new JButton("valume");
         contentPane.add(valumeButton);
         valumeButton.addActionListener(this);

         surfaceAreaButton = new JButton("surfaceArea");
         contentPane.add(surfaceAreaButton);
         surfaceAreaButton.addActionListener(this);

         textArea = new JTextArea();
         textArea.setColumns(19);
         textArea.setRows(10);
         textArea.setEditable(false);
         scrollText = new JScrollPane(textArea);    
         contentPane.add(scrollText);  
      //register 'Exit upon closing' as a default close operation
         setDefaultCloseOperation( EXIT_ON_CLOSE );
      }



      public void actionPerformed(ActionEvent event) {//actionperformed.
        // sh = new Shapes(Asize);//.....
         if (event.getSource() instanceof JButton) {
            JButton clickedButton = (JButton) event.getSource();
            try{
               double h = Double.parseDouble(height.getText());
               double r = Double.parseDouble(radius.getText());
               Cylinder c1= new Cylinder(r,h);
               if (clickedButton == valumeButton) {
                //  Cylinder c1= new Cylinder(r,h);
                  String s = "The valume of the height "+h+" and radius "+r+" is : "+Math.round(c1.valume());
                //  sh.addShape(c1);
                  addText(s);
               } 
               else {

                  String s = "The surfaceArea of the height "+h+" and radius "+r+" is : "+Math.round(c1.surfaceArea());
                  //sh.addShape(c1);
                  addText(s);}


               c1.setvalume(c1.valume());
               c1.setsurfaceArea(c1.surfaceArea());
               sh.addShape(c1);
            }
               catch(NumberFormatException e){
                  FrameForError frame1 = new FrameForError();	
                  frame1.setVisible(true);		}

         } 
      }


      private void addText(String s) {
         textArea.append( s + "\n");

      }

   }

تم تعديل هذه المشاركة بواسطة علاء الصالحي في 16 مايو 2012 في 13:24 — السبب: إضافة وسم code

#2

أختي هذه العملية تتم في أول تشغيل للبرنامج فقط

وعند بداية البرنامج تكون المصفوفة لديك خالية تماماً لأنك لم تضيفي أي شيء

يستحسن بك أن تقومي بوضع زر يكون مسؤول عن حفظ البيانات في الملف بعد أن تقومي بإضافتها في الواجهة

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

جزاك الله خيرا أشكر تجاوبك ...

وجدت طريقة لجعل الملف يخزن المعلومات بمجرد إغلاق النافذة بدونإضافة زر وهي استخدام مثود تكتب بداخلها ما تريده أن ينفذ بعد إغلاق النافذة وهي كالتالي :

 addWindowListener(
               new java.awt.event.WindowAdapter(){// the action when user close the window.
                  public void windowClosing(WindowEvent e){                       }});

استخدامي لها في برنامجي :

 frame.addWindowListener(
               new java.awt.event.WindowAdapter(){// the action when user close the window.
                  public void windowClosing(WindowEvent e){
                     try{
                        PrintWriter b=new PrintWriter(new FileOutputStream(new File("Cylender Info.txt")));//save the cylinder object from the array to the file.
                        for(int i=0;i<sh.getnumShapes();i++)
                           b.println(sh.getCylinder());//end loop
                        b.close();

								 Scanner input=new Scanner(new File("Cylender Info.txt"));
								 while(input.hasNext())
								 System.out.println(input.nextLine());
								 input.close();

								}
                        catch(FileNotFoundException ex){
                           System.out.println("file not found!");} }//end windowClosing().
               });//end the action

تم تعديل هذه المشاركة بواسطة سمو الروحـ في 16 مايو 2012 في 19:48

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…