调整框架布局



我想为此帧设置一个特定的布局,为了'准确性,我希望将帧显示在屏幕中心,我试图输入Gridlayout(x,x,Y),具有特定的坐标,但它在Eclipse中给了我警告,我唯一能做的就是设置Null布局,如下所示。

class Login extends JFrame {
/**
 * 
 */
      setTitle("Title");
      setLayout(null);
      .
      .
      .

首先,在大多数情况下,警告不应禁止您编译。其次,您正在滥用构造函数。阅读gridlayout文档-http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

如果要在屏幕中心显示框架,请使用setLocationRelativeTo(null);Gridlayout不用于以像素的方式来输入坐标,而是用您创建的网格来输入坐标。如果要在像素中进行准确的定位,请将布局设置为null,然后在您想要的任何GUI组件上使用setBounds()

例如:

    JLabel aLabel = new JLabel("Title");
    aLabel.setBounds(50, 50, 30, 10); //x, y, width, height

您是否尝试过" setLocationRelativeto(null);"?这应该以框架为中心http://docs.oracle.com/javase/7/docs/api/java/awt/window.html#setlatocationRelatocationRelativeto(Java.awt.awt.component.component com.ponents)

最新更新