Frage

Die beiden unten gezeigten Beispiele sind gleich. Beide sollen das gleiche Ergebnis erzeugen, z. B. die Koordinaten von Bildern erzeugen, die auf JPanel angezeigt werden. Beispiel 1 funktioniert perfekt (drucken Sie die Koordinaten von Bildern), beispielsweise 2, die 0 für die Koordinate zurückgibt.

Ich habe mich gefragt, warum ich in beiden Beispielen das Setvisible (True) nach dem Hinzufügen des Panels eingestellt habe. Der einzige Unterschied besteht darin, dass Beispiel 1 verwendet wird extends JPanel und Beispiel 2 extends JFrame

BEISPIEL 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);        
        }
    }

Beispiel 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();
    }
   }
War es hilfreich?

Lösung

Das Problem ist, dass Sie im zweiten Beispiel versuchen, die Grenzen einer Komponente auszudrucken add()) und bevor der Inhalt des Rahmens festgelegt wurde (durch Anrufe pack()).

Hier ist mein Versuch, Beispiel 1. ...

Hier ist mein Versuch, Beispiel 2. Ich habe das hinzugefügt SwingUtilities Rufen Sie an, um die Dinge in den richtigen Thread zu setzen, und ich füllte den Inhalt des GetCoordiates Konstruktor mit Hilfe Ihrer Kommentare:

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();
            }
        });
    }
}

Genau wie Sie beschrieben haben, druckt es eine Größe von Null aus:

Koordinate: java.awt.Rectangle [x = 0, y = 0, width = 0, Höhe = 0

Wenn Sie jedoch die Größe nach dem Hinzufügen der Komponente ausdrucken und der Rahmen gepackt haben, sollte sie funktionieren. Hier ist eine modifizierte Version meines Beispiels 2, bei der ich eine Methode hinzugefügt habe GetCoordinate.printBounds() und nennen Sie diese Methode, was alles hinzugefügt und angelegt wurde:

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();
            }
        });
    }
}

Mit diesen Änderungen erhalte ich die folgende Konsolenausgabe, einschließlich einer Größe von ungleich Null für meinen Inhalt:

Koordinate: java.awt.Rectangle [x = 5, y = 5, width = 23, Höhe = 16

Andere Tipps

Eine häufige Ursache für solche Anomalien ist nicht, mit dem zu beginnen Sommerzeit. In diesem Fall kann ich nicht aus Ihrem Code erkennen, was anders ist: Insbesondere ist nicht klar, wo das zweite Beispiel druckt.

konto

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);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top