单击按钮后更改Frame的内容窗格
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并让它完成肮脏的工作。
更多相关文章
- 字体图标的引入和通过媒体查询改变导航样式
- HTML样式和常用选择器
- 字体图标的引用和自定义样式/媒体查询的使用
- 数据库的CURD操作、PDO本质与原理的学习
- CSS之伪类选择器和简单盒子简单案例
- 伪类选择器与盒模型常用属性
- 伪类选择器-结构伪类、根据位置选择匹配
- 7.4——常用标签与应用场景之表格与单元格
- css伪类选择器和盒模型