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

تبديل الفريم بالابليت

بدأه sunn في 7 سبتمبر 2011 · 13 رد · 355 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

استخدم في برنامجي مجموعة من الفريمز وكل فريم فيه زر عند الضغط عليه يظهر فريم اخر ويختفي الفريم الحالي في التنفيذ العادي للبرنامج استخدمت الميثود setVisible(false)

فنجح الامر ولكن عند تنفيذ البرنامج كابليت فان الفريم لا تختفي وانما تصغر فقط فهل من طربقة لاخفاء الفريم في الابليت ؟؟

واذا لا توجد طريقة واردت استبدال الفريمز ببانيل ( يعني اعمل بانيل اضيفها على فريم وعند تحديث الفريم تظهر بانيل مختلفة ) فما هي الطريقة لعمل ذلك وكيف يمكنني تحديث الفريم بحيث تتغير محتوياته بالكامل؟؟؟؟

ارجو ان تكونوا فهمتم السؤال :wacko:

#2

اين قمتي باستدعاء الحدث الخاص بالفريم؟ من اي دالة ؟ هل ممكن تضعي الكود لو كان طولة معقول؟

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#3

هو طويل شوي

هاد اللي بتحكم فيه بالفريم

public class AddOption extends JPanel  implements ActionListener{
JPanel boardPanel=new JPanel();

protected UndoManager undoManager = new UndoManager();

SolitaireForm s;
SolitaireForm1 s1;
SolitaireForm2 s2;
SolitaireForm3 s3;
SolitaireForm4 s4;
SolitaireForm5 s5;
//NewJPanel button=new NewJPanel();

int c=-1;
JPanel button=new JPanel(new GridLayout(2,3,50,50));
JPanel head=new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel p1_1=new JPanel(new FlowLayout());
JPanel p1=new JPanel(new BorderLayout());
/*JPanel p2=new JPanel(new BorderLayout());
JPanel p3=new JPanel(new BorderLayout());
JPanel p4=new JPanel(new BorderLayout());
JPanel p5=new JPanel(new BorderLayout());JPanel p6=new JPanel(new BorderLayout());
JPanel p7=new JPanel(new BorderLayout());*/
JButton jButton1=new JButton("",new ImageIcon(this.getClass().getResource("Images\\option11.jpg")));
    JButton jButton2=new JButton("",new ImageIcon(this.getClass().getResource("Images\\option22.jpg")));
    JButton jButton3=new JButton("",new ImageIcon(this.getClass().getResource("Images\\option33.jpg")));
    JButton jButton4=new JButton("",new ImageIcon(this.getClass().getResource("Images\\option44.jpg")));
   JButton jButton5=new JButton("",new ImageIcon(this.getClass().getResource("Images\\option55.jpg")));
   JButton jButton6=new JButton("",new ImageIcon(this.getClass().getResource("Images\\option66.jpg")));
PegSolitaireGame2 peg=new PegSolitaireGame2();
// SolitaireForm s;
static JFrame solFrame=new JFrame();
static JFrame solFrame1=new JFrame();
static JFrame solFrame2=new JFrame();
static JFrame solFrame3=new JFrame();
static JFrame solFrame4=new JFrame();
static JFrame solFrame5=new JFrame();

    public AddOption() {

        if(c==-1){
           p1_1. setBackground(new Color(205,133,63));
           button. setBackground(new Color(205,133,63));
         jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
jButton4.addActionListener(this);
jButton5.addActionListener(this);
jButton6.addActionListener(this);
        button.add(jButton1);
         button.add(jButton2);
          button.add(jButton3);
           button.add(jButton4);
            button.add(jButton5);
             button.add(jButton6);
             p1_1.add(button,BorderLayout.CENTER);
             Font font1=new Font("SansSerif", Font.BOLD, 16);
    	JLabel sort=new JLabel("---------- Choose a form of the Game ----------");
    	sort.setFont(font1);
    	head.add(sort);
    	head.setBorder(new TitledBorder(BorderFactory.createBevelBorder(1,Color.green,Color.black,Color.green,Color.BLACK),""));
    	p1.add(head,BorderLayout.NORTH);
         p1.add(p1_1,BorderLayout.CENTER);
             add(p1,BorderLayout.CENTER);

        }


//add(new JLabel("-----------------------"),BorderLayout.WEST);


    }



    public void actionPerformed(ActionEvent e) {

         //-----------------b1----------------------
      if(e.getSource()==jButton1){
c=0;
            try {
                //init();
                hideVisible();
                s = new SolitaireForm();
                solFrame.add(s);
                solFrame.setVisible(true);
                solFrame.setSize(1100,700);
                solFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();

            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }
               }

      //------------------b2---------------------
     if(e.getSource()==jButton2){
c=1;
 try {hideVisible();
                //init();
                s1 = new SolitaireForm1();
                solFrame1.add(s1);
                solFrame1.setVisible(true);
                solFrame1.setSize(1100,700);
                solFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame1.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();
            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
      //----------------b3-----------------------
     if(e.getSource()==jButton3){
c=2;
 try {hideVisible();
                //init();
                s2 = new SolitaireForm2();
                solFrame2.add(s2);
                solFrame2.setVisible(true);
                solFrame2.setSize(1100,700);
                solFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame2.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();
            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
      //---------------b4------------------------
     if(e.getSource()==jButton4){
c=3;
 try {
                hideVisible();
                s3 = new SolitaireForm3();
                solFrame3.add(s3);
                solFrame3.setVisible(true);
                solFrame3.setSize(1100,700);
                solFrame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame3.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();
            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
      //-------------b5--------------------------
     if(e.getSource()==jButton5){
c=4;
 try {
                hideVisible();
                s4 = new SolitaireForm4();
                solFrame4.add(s4);
                solFrame4.setVisible(true);
                solFrame4.setSize(1100,700);
                solFrame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame4.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();
            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
      //--------------b6-------------------------
     if(e.getSource()==jButton6){
c=5;
 try {
                hideVisible();
                s5 = new SolitaireForm5();
                solFrame5.add(s5);
                solFrame5.setVisible(true);
                solFrame5.setSize(1100,700);
                solFrame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame5.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();
            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }  catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

      }
public static void hideVisible(){

    if(solFrame.isVisible())solFrame.setVisible(false);
    if(solFrame1.isVisible())solFrame1.setVisible(false);
    if(solFrame2.isVisible())solFrame2.setVisible(false);
    if(solFrame3.isVisible())solFrame3.setVisible(false);
    if(solFrame4.isVisible())solFrame4.setVisible(false);
    if(solFrame5.isVisible())solFrame5.setVisible(false);
}

            }

وهاد ال main

public class PegSolitaireGame2 extends JApplet {
    static JFrame frame=new JFrame();
    JPanel p1=new JPanel(new FlowLayout());
@Override
    public void init() {
p1.setBackground(new Color(205,133,63));
p1.add(new AddOption(),BorderLayout.CENTER);
add(p1,BorderLayout.CENTER);
       }
  public static void hideFrame(){
      frame.setVisible(false);
  }
  public static void showFrame(){
      frame.setVisible(true);

  }

    public static void main (String[] args) {

    	frame.pack();
    	PegSolitaireGame2 applet=new PegSolitaireGame2();
    	frame.add(applet);
    	applet.init();
    	frame.setSize(1100,700);
        //frame.pack();
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocationRelativeTo(null);
    	frame.setVisible(true);
}


    }
#4

لا افهم لماذا قمتي بوضع الدالة main داخل الابليت فانتي لست بحاجه لها ، قومي باستخدام الدوال الخاصة بالابليت مثل init, start, stop, paint

بالاضافه الى ذلك من اين ستقومين باستدعاء الدوال hideFrame, showFrame ؟

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#5

انا استخدم ال main اثناء البرمجة حتى يسهل علي اختبار البرنامج باستخدام run

اما بالنسبة للدالة hideFrame فانا استخدمها كما هو واضح في الكود الاول لاخفاء الشاشة الرئيسية (التي تظهر في بداية تشغيل البرنامج)

اما الدالة showFrame فاستخدمها في الكلاسات التانية لاظهار الشاشة الرئيسية عن طريق الضغط على زر

انا مشكلتي في التحديث فهناك طريقتين اما ان استخدم البانيل وعند الضغط على زر يتم عرض بانيل مختلفة على نفس الفريم وتختفي البانيل الاولى

او ان استخدم فريم واستخدم الميثود setvisible ولكن كما ذكرت في البداية فانه عند تنفيذه كابليت لا يختفي الفريم وانما يصغر فقط

ارجو ان تكون فهمت علي

#6

قصدي وين استدعيتي الدالتين؟

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#7

قلت اني استدعيت الدالة hideFrame من الكلاس AddOption (الكود الاول ) في ال listener للازرار

اما الدالة showFrame استخدمها في الكلاسات التانية (لم اضع كودها لانه طويل )ايضا في ال listener للازرار

تم تعديل هذه المشاركة بواسطة sunn في 8 سبتمبر 2011 في 20:55

#8

انا شايف انكي استدعيتي الداله hideVisible وليس hideFrame

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#9

هذا الكود في السطر اللي قبل catch

   PegSolitaireGame2.hideFrame();

if(e.getSource()==jButton1){
c=0;
            try {
                //init();
                hideVisible();
                s = new SolitaireForm();
                solFrame.add(s);
                solFrame.setVisible(true);
                solFrame.setSize(1100,700);
                solFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                solFrame.setLocationRelativeTo(null);
                PegSolitaireGame2.hideFrame();

            } catch (IOException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RuntimeException ex) {
                Logger.getLogger(AddOption.class.getName()).log(Level.SEVERE, null, ex);
            }
               }

تم تعديل هذه المشاركة بواسطة sunn في 8 سبتمبر 2011 في 21:06

#10

قومي باضافة

System.out.println();

داخل الاكشن ليسنر للتأكدي انه يتم استدعاء الدالة actionPerformed

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#11

التنفيذ تمام وهو ينفذ المطلوب والكود صحيح وما فيه اخطاء

ولكني اريد ان تخبرني بطريقة للتحديث (كما قلت في الرد الرابع)

#12

شوفي في الابليت الدالة init تستخدم لتحميل الموارد الثقيلة والتي تحتاج وقت مثل الصور وغيرها وعمل تهيئة للعناصر

الدالة start هي الدالة التي يتم استدعائها كل مرة تقومين بالضغط على الزر start من القائمة

الدالة stop عكس start

الدالة paint هي التي تقوم بتحديث الرسم

يعني الدالة init تستدعى مرة واحدة والموارد التي قمتي بعمل تهيئه لها تضل حتى اغلاق الابليت

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#13

سوف احاول استخدام الدالة paint ارى ماذا يحصل شكرا لك

#14
sunn كتب:

سوف احاول استخدام الدالة paint ارى ماذا يحصل شكرا لك

فقط للتأكد جربي ان تسحبي الكود اللي في الدالة Main الى init وقومي بتشغيل الابليت باستخدام الــapplet viewer او باستخدام المتصفح

بالتوفيق

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

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