can卡布局显示卡布局

6ovsh4lw  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(200)

我在学习如何使用 CardLayout 现在我发现我的项目可能需要插入 CardLayout 进入 CardLayout . 据我所知,我可以设置任何 JPanel 或容器作为 CardLayout 物品/卡片,所以我编写了代码,它只显示第一个组合框

我尝试了很多可能性,结果基本相同。我只是不知道我还能做什么,所以我要你帮忙。
(是的,这是甲骨文的代码。我在学习,好吗?我需要基础来理解它是如何工作的,然后我懒得重写它。这就是为什么我要包括甲骨文的版权)

/*
 * 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();
            }
        });
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题