Positioning a JPanel in a JFrame at specific position

怎甘沉沦 提交于 2019-12-01 06:31:35

Of the frame's content panel, the layout manager by default is a BorderLayout (NWSE+C). Do:

this.getContentPanel().setLayout(null);
Konzern

using setBounds(new Rectangle(96, 67, 98, 41)); you can do this... just see the example

/**
 * This method initializes this
 *
 * @return void
 */
private void initialize() {
    this.setSize(300, 200);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
}

/**
 * This method initializes jContentPane
 *
 * @return javax.swing.JPanel
 */
private JPanel getJContentPane() {
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setLayout(null);
        jContentPane.add(getLoginButton(), null);
    }
    return jContentPane;
}

}

A JFrame object uses borderlayout manager by default and JPanels use framelayout manager by default. If you want to use absolute positioning then you MUST use null layout because any other layout manager will use setLocation() and setSize() methods according to its own moods and not according to how YOU want it.

Summary: Use setLayout(null) and use null manager and then use setBounds(X,Y,width,height) method.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!