将JPanel定位在特定位置的JFrame中 [英] Positioning a JPanel in a JFrame at specific position

查看:549
本文介绍了将JPanel定位在特定位置的JFrame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

它是这样的:

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

我不需要LayoutManager作为JPanel的位置,因为我需要将JPanel放在特定位置(如示例中的150,150)。
但是,如果我像上面的代码一样执行panel.setLocation(150,150),没有任何反应,JPanel仍然在框架的北中心(如果我更改x,y数字而不是150,150)。

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).

它显示如下:

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

推荐答案

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

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

this.getContentPanel().setLayout(null);

这篇关于将JPanel定位在特定位置的JFrame中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆