下面显示的两个示例相同。两者都应该产生相同的结果,例如生成JPanel上显示的图像的坐标。示例1,完美工作(打印图像的坐标),但是示例2为坐标返回0。

我想知道为什么,因为在两个示例中添加了面板后,我都将固定的(true)放在。唯一的区别是使用的示例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,width = 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

其他提示

这种异常的常见原因是未能开始 美东时间. 。在这种情况下,我无法从您的代码中分辨出什么不同:特别是,尚不清楚第二个示例在哪里打印。

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);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top