2

以下に示す2つの例は同じです。どちらも同じ結果を生成することになっています。たとえば、JPanelに表示される画像の座標を生成します。例1は完全に機能します(画像の座標を出力します)が、例2は座標に0を返します。

どちらの例でも、パネルを追加した後にsetvisible(true)を配置したので、なぜだろうと思っていました。唯一の違いは、使用した例1extends 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();
    }
   }
4

3 に答える 3

2

add()問題は、2番目の例では、コンポーネントがコンテナに追加される前(を呼び出すことによって)およびフレームのコンテンツがレイアウトされる前(を呼び出すことによって)にコンポーネントの境界を出力しようとしていること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、width = 0、height = 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、width = 23、height = 16]

于 2010-03-09T02:14:24.910 に答える
0

このような異常の一般的な原因は、EDTでの開始に失敗することです。この場合、コードから何が違うのかわかりません。特に、2番目の例がどこに出力されるかは明確ではありません。

于 2010-03-09T02:07:12.453 に答える
0

contoh

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);
    }
}
于 2017-07-14T16:31:31.037 に答える