I want to be able to set a JFrame's contentpane after a button inside one of that frame's JPanels has been clicked.

我希望能够在单击该框架的一个JPanels中的按钮后设置JFrame的contentpane。

My architecture consists of a controller which creates the JFrame and the first JPanel inside of it. From within the first JPanel I'm calling a method: setcontentpane(JPanel jpanel) on the controller. However, instead of loading the passed JPanel it does nothing but removing all Panels (see code below)

我的架构由一个控制器组成,该控制器创建JFrame和它内部的第一个JPanel。从第一个JPanel中我调用一个方法:setcontentpane(JPanel jpanel)在控制器上。但是,它不是加载传递的JPanel,而是删除所有面板(参见下面的代码)

ActionListener inside of the first JPanel:

第一个JPanel里面的ActionListener:

public void actionPerformed(ActionEvent arg0) {
            controller.setpanel(new CustomPanel(string1, string2));
        }

Controller:

控制器:

JFrame frame;

public void setpanel(JPanel panel)
{
    frame.getContentPane().removeAll();
    frame.getContentPane().add(panel);
    frame.repaint();
}

public Controller(JFrame frame)
{
    this.frame=frame;
}

Can anyone tell me what I'm doing wrong? Thanks :)

谁能告诉我我做错了什么?谢谢 :)

3 个解决方案

#1


16

Call revalidate, then repaint. This tells the layout managers to do their layouts of their components:

调用revalidate,然后重新绘制。这告诉布局管理器执行其组件的布局:

JPanel contentPane = (JPanel) frame.getContentPane();

contentPane.removeAll();
contentPane.add(panel);
contentPane.revalidate(); 
contentPane.repaint();

Better though if you just want to swap JPanels is to use a CardLayout and have it do the dirty work.

更好的是,如果你只是想交换JPanels就是使用CardLayout并让它完成肮脏的工作。

更多相关文章

  1. 字体图标的引入和通过媒体查询改变导航样式
  2. HTML样式和常用选择器
  3. 字体图标的引用和自定义样式/媒体查询的使用
  4. 数据库的CURD操作、PDO本质与原理的学习
  5. CSS之伪类选择器和简单盒子简单案例
  6. 伪类选择器与盒模型常用属性
  7. 伪类选择器-结构伪类、根据位置选择匹配
  8. 7.4——常用标签与应用场景之表格与单元格
  9. css伪类选择器和盒模型

随机推荐

  1. Android 各种布局技术-五大布局对象
  2. Android数据的四种存储方式SharedPrefere
  3. Android Camera (android2.2) 资料一
  4. android 关于tts的一些参数
  5. android原生项目整合ReactNative
  6. 搭建android测试环境
  7. android sdk setup时出现:HTTPS SSL erro
  8. Android-Accessibility(Android(安卓)8.0
  9. Android中三星调用系统相机拍照后旋转的
  10. Android(安卓)各版本代号 & Java Source