[英]How to change CardLayout panels from a separate panel?
My software layout is kinda wizard-base. 我的软件布局有点像向导基础。 So the base panel is divided into two
JPanel
s. 因此,基础面板分为两个
JPanel
。 One left panel which never changes. 左侧面板永不改变。 And one right panel that works with
CardLayout
. 还有一个与
CardLayout
一起使用的右面板。 It has many sub-panels and show each one of them by a method. 它具有许多子面板,并通过一种方法显示每个子面板。
I can easily go from one inner panel to another one. 我可以轻松地从一个内面板转到另一个内面板。 But I want to have a button in left panel and change panels of the right side.
但是我想在左侧面板中有一个按钮,并在右侧面板中进行更改。
Here is a sample code which you can run it: 这是一个示例代码,您可以运行它:
BASE: 基础:
public class Base {
JFrame frame = new JFrame("Panel");
BorderLayout bl = new BorderLayout();
public Base(){
frame.setLayout(bl);
frame.setSize(800, 600);
frame.add(new LeftBar(), BorderLayout.WEST);
frame.add(new MainPanel(), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
new Base();
}
}
Left side 左边
public class LeftBar extends JPanel{
JButton button;
MainPanel mainPanel = new MainPanel();
public LeftBar(){
setPreferredSize(new Dimension(200, 40));
setLayout(new BorderLayout());
setBackground(Color.black);
button = new JButton("Show Second Page");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
mainPanel.showPanel("secondPage");
}
});
add(button, BorderLayout.NORTH);
}
}
Right Side 右边
public class MainPanel extends JPanel {
private CardLayout cl = new CardLayout();
private JPanel panelHolder = new JPanel(cl);
public MainPanel(){
FirstPage firstPage = new FirstPage(this);
SecondPage secondPage = new SecondPage(this);
setLayout(new GridLayout(0,1));
panelHolder.add(firstPage, "firstPage");
panelHolder.add(secondPage, "secondPage");
cl.show(panelHolder, "firstPage");
add(panelHolder);
}
public void showPanel(String panelIdentifier){
cl.show(panelHolder, panelIdentifier);
}
}
Inner panels for right side: 右侧内面板:
public class FirstPage extends JPanel {
MainPanel mainPanel;
JButton button;
public FirstPage(MainPanel mainPanel) {
this.mainPanel = mainPanel;
setBackground(Color.GRAY);
button = new JButton("Show page");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
mainPanel.showPanel("secondPage");
}
});
add(button);
}
}
public class SecondPage extends JPanel{
MainPanel mainPanel;
JButton button;
public SecondPage(MainPanel mainPanel){
this.mainPanel = mainPanel;
setBackground(Color.white);
add(new JLabel("This is second page"));
}
}
And this is a picture to give you the idea: 这是给您这个想法的图片:
As I explained, I can travel "from first" page to "second page" by using this method: mainPanel.showPanel("secondPage");
正如我所解释的,可以使用以下方法将“从第一页”移动到“第二页” :
mainPanel.showPanel("secondPage");
or mainPanel.showPanel("firstPage");
或
mainPanel.showPanel("firstPage");
. 。
But I also have a JButton
in the left bar, which I call the same method to show the second panel of the CardLayout. 但是我在左侧栏中还有一个
JButton
,我调用了相同的方法来显示CardLayout的第二个面板。 But it does not work. 但这行不通。 It doesnt give any error though.
它没有给出任何错误。
Any idea how to change these CardLayout
panels from outside of panels? 知道如何从面板外部更改这些
CardLayout
面板吗?
The problem is that LeftBar
has mainPanel
member that is initialized to a new instance of MainPanel
. 问题在于
LeftBar
具有mainPanel
成员,该成员已初始化为MainPanel
的新实例。 So you have two instances of MainPanel
, one allocated in Base
and added to the frame, the other one allocated in LeftBar
. 所以,你有两个实例
MainPanel
,在一个分配的Base
,并添加到帧,另一个在分配LeftBar
。
So LeftBar
executes mainPanel.showPanel("secondPage");
因此,
LeftBar
执行mainPanel.showPanel("secondPage");
LeftBar
on a second instance of MainPanel
which is not even a part of a visual hierarchy. 在
MainPanel
的第二个实例上,该实例甚至不属于可视层次结构。 To fix this just pass an existing instance of MainPanel
to the constructor of LeftBar
. 要解决此问题,只需将
MainPanel
的现有实例MainPanel
给MainPanel
的构造LeftBar
。 You already do this in FirstPage
and SecondPage
. 您已经在
FirstPage
和SecondPage
执行了此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.