卡布局可以显示卡布局

问题描述

我现在正在学习如何使用cardlayout,并且发现我的项目可以利用将cardlayout插入cardlayout的可能性。据我了解,我可以将任何JPanel或容器设置为cardlayout项/卡,因此我已经编写了代码,并且它显示的唯一内容是第一个组合框

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)