2017-10-07 3 views
0

複数のCardLayoutsを並べて表示しようとしていますが、すべてFlowLayoutの内側にあります。すべてうまく動作しますが、何もウィンドウに表示されません。 FlowLayoutにCardLayoutsとそのコンポーネントを表示させるにはどうすればよいですか?複数のCardLayoutsをFlowLayoutに並べてネストする

私は既にこの問題に非常に役立つと私は見つけることができないすべての関連docsを読んでいる。ここで

は私のサンプルコードです:代わりにnew JPanel秒のthisを使用する

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

更新コード:https://pastebin.com/e0fhkaen

それが働いて得た:Slotクラスインサイドペーストビン5XrFYarD

答えて

1

を、ないではありません新しいJPanelを作成してください。

this.setLayout(cardLayout); 

このクラスはJPanelに拡張されています。

今のように、フレームに空の2つの空き文字を追加するだけです。JPanel

同じことが、私はhttps://pastebin.com/e0fhkaenにそれを更新し、他のクラス(AddButtonRemoveButton

+0

のために行くが、それはまだ何も表示されません。 – bloxz64

関連する問題