【问题标题】:How do you return the JPanel in the center of a JFrame using a BorderLayout?如何使用 BorderLayout 返回 JFrame 中心的 JPanel?
【发布时间】:2016-07-28 15:49:31
【问题描述】:

到目前为止,我发现 this post 很有帮助。

但我无法让它工作,尤其是这部分.getLayoutComponent

这就是我希望该方法在类中的外观:

public JPanel getCentrePanel() {

    return [CENTER PANEL WOULD BE RETURNED HERE];
}

【问题讨论】:

  • 这应该可以工作return yourContainer.getLayout().getLayoutComponent(BorderLayout.CENTER));
  • 我不确定我是否有一个容器,我的 JFrame (frmAdmin) 是一个 BorderLayout,并且我有想要在中心返回的面板 (BorderLayout.CENTER)。

标签: java swing return jpanel


【解决方案1】:

您可以从JFrame 的内容窗格中获取布局。

BorderLayout layout = (BorderLayout) frmAdmin.getContentPane().getLayout();
return (JPanel) layout.getLayoutComponent(BorderLayout.CENTER);

【讨论】:

  • 首先感谢您的快速响应,但我无法让它工作,如下所示:gyazo.com/54942f9dffa53261214b2167c0d3fb87 错误 1:“类型不匹配:无法从 LayoutManager 转换为 BorderLayout”错误 2:“The AdminView 类型的方法 getContentPane() 未定义”错误 3:“类型不匹配:无法从 Component 转换为 JPanel”
  • 将布局投射到BorderLayout
  • 非常感谢,我现在一切正常,应该早点告诉你。真的拯救了我的课程作业,干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多