2015-04-12 59 views
1

我的软件布局有点像向导基础。所以基础面板分为两个JPanel s。一个从未改变的左侧面板。还有一个与CardLayout配合使用的右侧面板。它有许多子面板,并通过一种方法显示它们中的每一个。如何从独立面板更换CardLayout面板?

我可以轻松地从一个内部面板去另一个。但我想在左侧面板上有一个按钮并更换右侧的面板。

这里是一个示例代码,你可以运行它:

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(); 
    } 
} 

左侧

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); 
    } 
} 

右侧

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); 
    } 
} 

为右侧内板:

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")); 
    } 
} 

这是一个图片给你的想法: enter image description here

正如我解释,我可以旅行“从第一个”通过使用这种方法:mainPanel.showPanel("secondPage");mainPanel.showPanel("firstPage");的“”第二页“”。

但我在左侧栏中也有一个JButton,我用同样的方法显示CardLayout的第二个面板。但它不起作用。它虽然没有提供任何错误。

任何想法如何从面板外改变这些CardLayout面板?

回答

4

问题是LeftBar已将mainPanel成员初始化为MainPanel的新实例。因此,您有两个MainPanel实例,一个分配在Base中并添加到框架中,另一个分配在LeftBar中。

因此LeftBarMainPanel的第二个实例上执行mainPanel.showPanel("secondPage");,它甚至不是视觉层次结构的一部分。要解决这个问题,只需将MainPanel的现有实例传递给LeftBar的构造函数。您已在FirstPageSecondPage中执行此操作。

+1

非常感谢。有效。 – Dan

+0

@欢迎您! :) – tenorsax

+1

我有另一个类似这个问题。但我为此另外提出了一个问题。你能看看吗? [链接](http://stackoverflow.com/questions/29597973/why-doesnt-actionlistener-work-in-the-controller) – Dan