[英]Replacing current JPanel from JButton inside the JPanel not the JFrame
I have a JFrame and I use it as the main one and change JPanels inside the same JFrame. 我有一个JFrame,我用它作为主要的一个,并在同一个JFrame中更改JPanels。 The first JPanel is already inside the JFrame ,so I can replace it by "this.removeall() then ,this.setContentPane(new JPanel2)" because "this" here invoke the JFrame itself.
第一个JPanel已经在JFrame中,所以我可以用“this.removeall()然后,this.setContentPane(new JPanel2)”替换它,因为“this”在这里调用JFrame本身。
In the other JPanel, I want to make a JButton to replace the current JPanel with another one. 在另一个JPanel中,我想创建一个JButton来替换当前的JPanel。 The problem is that I can`t do the same code before because "this" here invoke the JPanel not the JFrame.
问题是我之前不能执行相同的代码,因为“this”在这里调用JPanel而不是JFrame。
The question is how can I write the code inside the JPanel allow me to get the JFrame to change its "setContentPane" to view the second Panel? 问题是如何在JPanel中编写代码允许我让JFrame更改其“setContentPane”以查看第二个Panel?
I have found the answer in using CardLayout .. this video helped me solve the problem 我找到了使用CardLayout的答案..这个视频帮助我解决了这个问题
I want to write the code in the buttonActionListner in the JPanel.
我想在JPanel的buttonActionListner中编写代码。 I will not write that code in the JFrame, so every JPanel can be added and removed to call another one independently
我不会在JFrame中编写该代码,因此可以添加和删除每个JPanel以独立调用另一个JPanel
and 和
@AndrewThompson-In the design of the interface we used buttons not CardLayout or Tabs.
@ AndrewThompson-在界面设计中我们使用的按钮不是CardLayout或Tabs。 I wanted to make all the application Panels and change them one by one in one JFrame
我想制作所有应用程序面板并在一个JFrame中逐个更改它们
have to call for parent ( JFrame
, JPanel
), 必须要求父母(
JFrame
, JPanel
),
validate(); // for Java7 revalidate() for JFrame
repaint();
after remove an add a new JPanel
, only once time and as last code line 删除后添加一个新的
JPanel
,只需一次和最后的代码行
as @AndrewThompson
mentioned better, easiest and simpliest way could be to use CardLayout 正如
@AndrewThompson
提到的更好,最简单和最简单的方法可能是使用CardLayout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.