CardLayout .show()方法不起作用

时间:2013-04-18 11:21:03

标签: java swing jpanel cardlayout

嗨,我对Java很新,我正在尝试学习在窗口构建器中使用卡片功能,但是我在使用show方法时遇到了问题。当我单击按钮时,它不会更改为所需的卡。我一直在松散地关注this tutorial

这是我的代码:

    public class HomeScreen {

    private final String WEIGHTS = "Weights panel";
    private final String EXCERCISE = "Excercise panel";
    private final String CARDIO = "Cardio panel";       

    private JPanel cards;
    //private CardLayout cardLayout = new CardLayout();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                //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();
                    }
                 });
            }
        });

    }

    public void addComponentToPane(Container pane) {
        final JPanel background = new JPanel();
        background.setBackground(Color.DARK_GRAY);
        background.setLayout(new CardLayout(0, 0));
        JButton btnHome = new JButton();
        btnHome.setForeground(Color.LIGHT_GRAY);
        btnHome.setFont(new Font("Tekton Pro Cond", Font.PLAIN, 22));
        btnHome.setBackground(Color.DARK_GRAY);
        btnHome.setText("Home");
        background.add(btnHome, "name_107366949842088");

        JPanel cardio = new JPanel();
        JPanel exercise = new JPanel();
        JPanel weights = new JPanel();

        JButton btnWeights_1 = new JButton("Weights");
        btnWeights_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Button Pressed");
                CardLayout cL = (CardLayout) (cards.getLayout()) ;
                //cL.next(cards);
                cL.show(cards, WEIGHTS);
            }
        });         

        JButton btnCardio = new JButton("Cardio");

        /*
         * Add new cards 
         */
        cards = new JPanel(new CardLayout());
        cards.add(exercise, EXCERCISE);
        exercise.setLayout(new BoxLayout(exercise, BoxLayout.X_AXIS));
        exercise.add(btnWeights_1);
        exercise.add(btnCardio);
        cards.add(cardio,CARDIO);
        cards.add(weights,"Weights panel");

        JButton btnLegs = new JButton("Running");
        cardio.add(btnLegs);

        JButton btnSwimming = new JButton("Swimming");
        cardio.add(btnSwimming);

        JButton btnCycling = new JButton("Cycling");
        cardio.add(btnCycling);

        JButton btnBoxing = new JButton("Boxing");
        cardio.add(btnBoxing);
        cards.add(weights);

        JButton btnBiceps = new JButton("Biceps");
        weights.add(btnBiceps);

        JButton btnLegs_1 = new JButton("Legs");
        weights.add(btnLegs_1);

        JButton btnBack = new JButton("Back");
        weights.add(btnBack);

        JButton btnChest = new JButton("Chest");
        weights.add(btnChest);

        pane.add(background, BorderLayout.PAGE_START);
        pane.add(cards,BorderLayout.PAGE_END);
    }

    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.
           HomeScreen demo = new HomeScreen();
           demo.addComponentToPane(frame.getContentPane());

           //Display the window.
           frame.pack();
           frame.setVisible(true);
       }
}

1 个答案:

答案 0 :(得分:1)

添加的额外调用包含Cardlayout约束。因为Swing中的组件只能有一个父组件,所以此行将取代之前指定约束的调用。

cards.add(weights); 

应删除此声明。