我需要帮助将 JPanel 定位到 Jframe 中的特定位置。
我在扩展 JFrame 的类中有一个 JPanel,我需要将此 JPanel 放在特定的 x,y 位置。
是这样的:
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
Run Code Online (Sandbox Code Playgroud)
我不需要 LayoutManager 来定位 JPanel 的位置,因为我需要将该 JPanel 放在特定位置(例如示例中的 150,150)。但是,如果我像上面的代码中那样执行 panel.setLocation(150,150) ,则不会发生任何事情,并且 JPanel 仍位于框架的北中心(如果我更改 x,y 数字而不是 150,150)。
它显示如下:
框架的内容面板中,默认的布局管理器是 BorderLayout (NWSE+C)。做:
this.getContentPanel().setLayout(null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68573 次 |
| 最近记录: |