首页 > 解决方案 > Card Layout 可以显示 Card Layout

问题描述

我现在正在学习如何使用CardLayout,我发现我的项目可以使用CardLayout插入CardLayout. 据我所知,我可以将任何JPanel或容器设置为CardLayout项目/卡片,因此我编写了代码,它唯一显示的是第一个组合框

图片1

我尝试了很多可能性,输出基本相同。我只是不知道我还能做什么,这就是我向你寻求帮助的原因。

(是的,它是根据 Oracle 的代码制作的。我正在学习,好吗?我需要基础来了解它是如何工作的,然后我懒得重写它。这就是为什么我包含 Oracle 的版权)

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

package UserInterface;

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

public class Przyciski implements ItemListener {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 50;

    JPanel cards;
    JPanel functions;
    JComboBox cb;
    JComboBox fccb;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";

public void addComponentToPane(Container pane) {
        JPanel functionChoosingPane = new JPanel(); //use FlowLayout
        String functionChooseComboBoxItems[] = { "Time Schedule", "Test Card" };
        fccb = new JComboBox(functionChooseComboBoxItems);
        fccb.setEditable(false);
        fccb.addItemListener(this);
        functionChoosingPane.add(fccb);

        JPanel comboBoxPane = new JPanel(); //use FlowLayout
        String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
        cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);

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

        JPanel testCard = new JPanel();
        testCard.add(new JButton("Test Button"));

        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);

        Container downPane = new Container();

        downPane.add(comboBoxPane, BorderLayout.PAGE_START);
        downPane.add(cards, BorderLayout.CENTER);

        functions = new JPanel(new CardLayout());
        functions.add(downPane, "Time Schedule");
        functions.add(testCard, "Test card");
        pane.add(functionChoosingPane, BorderLayout.PAGE_START);
        pane.add(functions, BorderLayout.CENTER);

    }

    public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, (String) evt.getItem());
    }


    private static void createAndShowGUI(){
        JFrame frame = new JFrame("School organisation system");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Przyciski demo = new Przyciski();
        demo.addComponentToPane(frame.getContentPane());

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

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

标签: javaswingeventsawtcardlayout

解决方案


推荐阅读