السلام عليكم ورحمة الله وبركاته ..
أتمنى مساعدتي فلدي برنامج يتكون من إنشاء نافذة لجعل المستخدم يدخل حجم المصفوفة التي سأستفيد منها في حفظ الأوبجيكت المتكون في ال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");
}
}