2015-04-12 66 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