2017-10-07 56 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