سؤال

الأمثلة التي تظهر أدناه هي نفسها. من المفترض أن ينتج كلاهما نفس النتيجة، مثل إنشاء إحداثيات الصور المعروضة على JPanel. مثال 1، يعمل بشكل مثالي (طباعة إحداثيات الصور)، ومع ذلك مثال 2 إرجاع 0 للتنسيق.

كنت أتساءل لماذا لأنني وضعت محصورة (صحيح) بعد إضافة اللوحة، في كلتا الأمثلة. الفرق الوحيد هو هذا المثال 1 المستخدمة extends JPanel ومثال 2. extends JFrame

مثال 1:

    public class Grid extends JPanel{
       public static void main(String[] args){
          JFrame jf=new JFrame();
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
          final Grid grid = new Grid();
          jf.add(grid);
          jf.pack();

          Component[] components = grid.getComponents();        
          for (Component component : components) {
           System.out.println("Coordinate: "+ component.getBounds());       
          }   

          jf.setVisible(true);        
        }
    }

مثال 2:

public class Grid extends JFrame {

  public Grid () {
    setLayout(new GridBagLayout());
    GridBagLayout m = new GridBagLayout();
    Container c = getContentPane();
    c.setLayout (m);
    GridBagConstraints con = new GridBagConstraints();

    //construct the JPanel
    pDraw = new JPanel();
    ...
    m.setConstraints(pDraw, con);
    pDraw.add (new GetCoordinate ()); // call new class to generate the coordinate
    c.add(pDraw);

    pack();
    setVisible(true);
    }

    public static void main(String[] args) {
       new Grid();
    }
   }
هل كانت مفيدة؟

المحلول

المشكلة هي أنه في المثال الثاني، تحاول طباعة حدود مكون قبل إضافة المكون إلى حاويةها (عن طريق الاتصال add()) وقبل وضع محتويات الإطار (عن طريق الاتصال pack()).

إليكم محاولتي لإعادة إنتاج مثال 1. ...

إليكم محاولتي إعادة إنشاء مثال 2. أضفت SwingUtilities اتصل بوضع الأشياء في الخيط الأيمن، وقد ملأت محتويات GetCoordiates منشئ بمساعدة من تعليقاتك:

class GetCoordinate extends JLabel {
    public GetCoordinate() {
        setText("Foo!");
        System.out.println("Coordinate: " + this.getBounds());
    }
}

public class Grid extends JFrame {
    public Grid() {
        setLayout(new GridBagLayout());
        GridBagLayout m = new GridBagLayout();
        Container c = getContentPane();
        c.setLayout(m);
        GridBagConstraints con = new GridBagConstraints();

        // construct the JPanel
        final JPanel pDraw = new JPanel();
        m.setConstraints(pDraw, con);
        pDraw.add(new GetCoordinate()); // call new class to generate the
                                        // coordinate
        c.add(pDraw);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Grid();
            }
        });
    }
}

تماما كما وصفت، فإنه يطبع حجم الصفر:

التنسيق: java.awt.Rectangle [x = 0، y = 0، العرض = 0، الارتفاع = 0

ومع ذلك، إذا قمت بطباعة الحجم بعد إضافة المكون، فقد تم تعبئة الإطار، يجب أن تعمل. إليك نسخة معدلة من مثالي 2، حيث أضفت طريقة GetCoordinate.printBounds() والاتصال بهذه الطريقة تمت إضافة كل شيء ووضعه:

class GetCoordinate extends JLabel {
    public GetCoordinate() {
        setText("Foo!");
        // Let's not try to do this here anymore...
//        System.out.println("Coordinate: " + this.getBounds());
    }

    public void printBounds() // <-- Added this method
    {
        System.out.println("Coordinate: " + this.getBounds());
    }
}

public class Grid extends JFrame {
    public Grid() {
        setLayout(new GridBagLayout());
        GridBagLayout m = new GridBagLayout();
        Container c = getContentPane();
        c.setLayout(m);
        GridBagConstraints con = new GridBagConstraints();

        // construct the JPanel
        final JPanel pDraw = new JPanel();
        m.setConstraints(pDraw, con);
        final GetCoordinate content = new GetCoordinate();
        pDraw.add(content);
        c.add(pDraw);

        pack();
        setVisible(true);
        content.printBounds();  // <-- Added this
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Grid();
            }
        });
    }
}

مع هذه التغييرات، أحصل على إخراج وحدة التحكم التالية، بما في ذلك حجم غير صفري للمحتوى الخاص بي:

التنسيق: Java.awt.Rectangle [x = 5، y = 5، العرض = 23، الارتفاع = 16

نصائح أخرى

سبب شائع لهذه الشاذة يفشل في البدء في بتوقيت شرق الولايات المتحدة. وبعد في هذه الحالة، لا أستطيع أن أقول من التعليمات البرمجية ما هو مختلف: على وجه الخصوص، ليس من الواضح أين يطبع المثال الثاني.

كونه

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LENOVO G40
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new FrmMenuUTama().setVisible(true);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top