设计一个的类,里面放入一个JPanel为CardLayout,在这个卡片里调用多个JPanel,
由于JPanel的代码长,将他们分在不多的类中,最后在卡片里调用各个JPanel的类,应该怎样的设计?请设计一个例子:
编写一个是CardLayout的JPanel的类和两个普通的Jpanel的类,最后卡片JPanel中能自由切换两个JPanel。 

解决方案 »

  1.   

    class Mypanel1 extends jPanel{}...class Mypanel2 extends jPanel{}....class MainPanel extends jPanel{
     private Mypanel1 panel1;
     private Mypanel2 panel2;
     private CardLayout card;
     public MainPanel(){
      //初始化....
      card.addLayoutComponent(panel1,"卡片1");
       card.addLayoutComponent(panel2,"卡片2");
      this.setLayout(card);
     }
    }然后调用card.show(容器,"卡片1/卡片2")就可以切换了
      

  2.   

    和楼上的差不多,不过,我不明和不是在在那里,而是我不知怎样写好两个JPanel,随便来些内容到里面,来参考下。。
      

  3.   

    类似这样写啊....只贴了一小部分public class MyPanel extends JPanel {
    private JButton restart = new JButton("重新开始");
    private JTextField tf1 = new JTextField("10", 3);
    private JTextField tf2 = new JTextField("0", 3);
    private JLabel jl1 = new JLabel("地雷");
    private JLabel jl2 = new JLabel("时间");
    private JButton[][] buttons;
    private String[][] bombs;
    private JPanel gamebody;
    private Timer timer; public MyPanel(int row, int coloum) {
    super();
    setLayout(new BorderLayout());
    JPanel panel = new JPanel(new FlowLayout());
    Font f = new Font("DotumChe", Font.BOLD, 20);
    timer = new Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
    setTf2(Integer.parseInt(tf2.getText()) + 1 + "");
    }
    });
    timer.start();
    tf1.setFont(f);
    tf2.setFont(f);
    jl1.setFont(new Font("黑体", Font.PLAIN, 16));
    jl2.setFont(new Font("黑体", Font.PLAIN, 16));
    tf1.setHorizontalAlignment(JTextField.RIGHT);
    tf2.setHorizontalAlignment(JTextField.RIGHT);
    tf1.setBackground(Color.BLACK);
    tf1.setForeground(Color.RED);
    tf2.setBackground(Color.BLACK);
    tf2.setForeground(Color.RED);
    tf1.setEditable(false);
    tf2.setEditable(false);
    panel.add(jl1);
    panel.add(tf1);
    panel.add(restart);
    panel.add(jl2);
    panel.add(tf2);
    add(panel, BorderLayout.NORTH);
    add(gamebody, BorderLayout.CENTER);
    }
    。其他方法
    }
      

  4.   

    是这样的吗?
    package layout;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class CardLayoutDemo implements ItemListener {
        JPanel cards; //a panel that uses CardLayout
        final static String BUTTONPANEL = "Card with JButtons";
        final static String TEXTPANEL = "Card with JTextField";
        
        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
            JComboBox cb = new JComboBox(comboBoxItems);
            cb.setEditable(false);
            cb.addItemListener(this);
            comboBoxPane.add(cb);
            
            //Create the "cards".
            JPanel card1 = new JPanel();
            card1.add(new JButton("Button 1"));
            card1.add(new JButton("Button 2"));
            card1.add(new JButton("Button 3"));
            
            JPanel card2 = new JPanel();
            card2.add(new JTextField("TextField", 20));
            
            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, BUTTONPANEL);
            cards.add(card2, TEXTPANEL);
            
            pane.add(comboBoxPane, BorderLayout.PAGE_START);
            pane.add(cards, BorderLayout.CENTER);
        }
        
        public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)evt.getItem());
        }
        
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("CardLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //Create and set up the content pane.
            CardLayoutDemo demo = new CardLayoutDemo();
            demo.addComponentToPane(frame.getContentPane());
            
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
        
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use of bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
      

  5.   

    完成了,原来是我写代码时没有添加add(panel, BorderLayout.NORTH);这句,
    所以效果没有出来。。
    多谢大家的帮助。。
    特别是lacus87