Java 在特定位置的 JFrame 中定位 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20196403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:11:36  来源:igfitidea点击:

Positioning a JPanel in a JFrame at specific position

javaswingjpanel

提问by alecarnevale

I need an help for positioning a JPanel in a specific position into a Jframe.

我需要帮助将特定位置的 JPanel 定位到 Jframe 中。

I have a JPanel in a class who extends JFrame, and I need to put this JPanel in a specific x,y position.

我在扩展 JFrame 的类中有一个 JPanel,我需要将这个 JPanel 放在特定的 x,y 位置。

It's something like this:

它是这样的:

public class Frame extends JFrame {

    public Frame(){

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(250, 250);
        this.setSize(300,300);
        this.setVisible(true);

        JPanel panel = new JPanel();
        this.add(panel);
        panel.setLocation(150, 150);
        panel.add(new JButton("Hello!")); // just to show JPanel

    }//Frame()
}//Frame

I don't need a LayoutManager for the position of the JPanel, because I need to put that JPanel in a specific position (like 150,150 of the example). But if I do panel.setLocation(150,150) like in the code above, nothing happens, and JPanel remain in the north-centre of the frame (also if I change x,y number instead of 150,150).

我不需要用于 JPanel 位置的 LayoutManager,因为我需要将该 JPanel 放在特定位置(例如示例的 150,150)。但是,如果我像上面的代码那样执行 panel.setLocation(150,150),则什么也不会发生,并且 JPanel 会保留在框架的北中心(如果我更改了 x,y 数字而不是 150,150)。

And it shows like this:

它显示如下:

http://i.imgur.com/Te3z0kv.png

http://i.imgur.com/Te3z0kv.png

采纳答案by Joop Eggen

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

在框架的内容面板中,默认布局管理器是 BorderLayout (NWSE+C)。做:

this.getContentPanel().setLayout(null);

回答by user3015246

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.

JFrame 对象默认使用边框布局管理器,JPanels 默认使用框架布局管理器。如果你想使用绝对定位,那么你必须使用空布局,因为任何其他布局管理器都会根据自己的心情而不是你想要的方式使用 setLocation() 和 setSize() 方法。

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

总结:使用 setLayout(null) 并使用空管理器,然后使用 setBounds(X,Y,width,height) 方法。

回答by Konzern

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

使用 setBounds(new Rectangle(96, 67, 98, 41)); 你可以这样做……看看例子

/**
 * 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;
}

}

}