2017-10-07 95 views
0

我試圖在一個FlowLayout中並排顯示多個CardLayouts。一切運行良好,但沒有出現在窗口中。我如何讓FlowLayout顯示CardLayouts及其組件?在FlowLayout中並排嵌套多個CardLayouts

我已經閱讀了所有我能找到的相關docs,並且沒有發現它們對這個問題非常有幫助。

這裏是我的示例代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


@SuppressWarnings("serial") 
public class RenderTest extends JPanel{ 
    private JFrame window; 
    private FlowLayout topLevelLayout; 
    private Slot[] slots; 

    public static void main(String[] args) { 
     RenderTest instance = new RenderTest(); 
     instance.init(); 
    } 

    private void init(){ 
     window = new JFrame("Render Test"); 

     topLevelLayout = new FlowLayout(); 
     window.setLayout(topLevelLayout); 
     window.setResizable(true); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setLocationRelativeTo(null); 

     slots = new Slot[]{new Slot(0), new Slot(2)}; 

     window.add(slots[0]); 
     window.add(slots[1]); 

     window.pack(); 
     window.setVisible(true); 
    } 

    private class Slot extends JPanel{ 
     JPanel panel; 
     CardLayout cardLayout; 
     public Slot(int index){ 
      RemoveButton remove = new RemoveButton(index); 
      AddButton add = new AddButton(index); 
      cardLayout = new CardLayout(); 
      panel = new JPanel(); 
      panel.setLayout(cardLayout); 
      cardLayout.addLayoutComponent(add.getPanel(), "add"); 
      cardLayout.addLayoutComponent(remove.getPanel(), "show"); 
      topLevelLayout.addLayoutComponent("card"+index, panel); 
     } 
     private JPanel getPanel(){ 
      return this.panel; 
     } 
     private CardLayout getCardLayout(){ 
      return this.cardLayout; 
     } 
    } 

    private class AddButton extends JPanel{ 
     JPanel panel; 
     private AddButton(int index){ 
      panel = new JPanel(); 
      JButton addButton = new JButton("+"); 

      addButton.setVerticalTextPosition(AbstractButton.CENTER); 
      addButton.setHorizontalTextPosition(AbstractButton.CENTER); 
      addButton.setActionCommand("add"+index); 
      addButton.addActionListener(new Button()); 

      panel.add(addButton); 
     } 
     private JPanel getPanel(){ 
      return this.panel; 
     } 
    } 

    private class RemoveButton extends JPanel{ 
     JPanel panel; 
     private RemoveButton(int index){ 
     panel = new JPanel(); 
     JButton removeButton = new JButton("-"); 

     removeButton.setVerticalTextPosition(AbstractButton.CENTER); 
     removeButton.setHorizontalTextPosition(AbstractButton.CENTER); 
     removeButton.setActionCommand("remove"+index); 
     removeButton.addActionListener(new Button()); 

     panel.add(removeButton); 
     } 
     private JPanel getPanel(){ 
      return this.panel; 
     } 
    } 

    class Button implements ActionListener{ 
     public void actionPerformed(ActionEvent e) { 
      System.out.println(e.getActionCommand()); 

      if (e.getActionCommand().equals("add0")){ 
       slots[0].getCardLayout().show(getParent(), "show"); 
      } 
      if (e.getActionCommand().equals("add1")){ 
       slots[1].getCardLayout().show(getParent(), "show"); 
      } 
      if (e.getActionCommand().equals("remove0")){ 
       slots[0].getCardLayout().show(getParent(), "hide"); 
      } 
      if (e.getActionCommand().equals("remove1")){ 
       slots[1].getCardLayout().show(getParent(), "hide"); 
      } 
     } 
    } 
} 

更新後的代碼使用this代替new JPanel S:https://pastebin.com/e0fhkaen

得到它的工作:引擎收錄5XrFYarD

回答

1

裏面的Slot類,不創建一個新的JPanel,您應該只使用

this.setLayout(cardLayout); 

因爲這個類延伸JPanel

現在,您只需在框架中添加兩個空的JPanel即可。

這同樣適用於我把它更新到https://pastebin.com/e0fhkaen其他類(AddButtonRemoveButton

+0

,但它仍然不會顯示任何東西。 – bloxz64