簡體   English   中英

檢查CardLayout中是否存在名稱中的卡

[英]Check if a card with a name is present in a CardLayout

我有一個CardLayout,我只根據需要添加卡片。 因此,當需要顯示特定卡(由其名稱標識)時,我需要一種方法來檢查具有該名稱的卡是否已經存在,以便我可以相應地顯示或創建它。

根據CardLayout文檔

使用addLayoutComponent翻轉到使用指定名稱添加到此布局的組件。 如果不存在這樣的組件,則沒有任何反應。

因此,如果我要求它顯示尚未添加的卡,則不會拋出任何錯誤。 我找不到任何能讓我檢查卡是否存在的API。

那么,這可能嗎? 如果不是如何解決這個問題呢? 有一個解決方案,我手動記住我添加了什么卡但感覺搖擺應該能夠處理這個。

CardLayout API無法檢查是否已添加具有給定名稱的組件。

如果你真的想這樣做(但我強烈建議AGAINST這樣做),那么你可以在容器使用的CardLayout上使用反射 ,並讀取它的vector字段,然后檢查每個條目(類型為CardLayout$Card )給定的名稱。 如你所見,這看起來像一個黑客,如果有一天CardLayout被重構,它可能會破壞(目前的實施非常難看)。

最好的方法是直接跟蹤某個Set<String>字段中所有已添加子項的名稱。 無論如何,這真的不是什么大不了的事。

因此,當需要顯示特定卡(由其名稱標識)時,我需要一種方法來檢查具有該名稱的卡是否已經存在,以便我可以相應地顯示或創建它。

  1. 獲取容器中顯示的當前組件
  2. 嘗試顯示不同的卡
  3. 獲取現在顯示在容器中的組件
  4. 如果兩個組件相同,則不會發生任何事情,您需要創建卡並將其添加到容器中。

這種方法可以幫助您自己管理卡片組。

編輯:

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

public class CardLayoutTest implements ActionListener
{
    JPanel cards;

    public void addComponentToPane(Container pane) {
        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = { "Red", "Orange", "Green", "Yellow", "Blue"};
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addActionListener(this);
        comboBoxPane.add(cb);

        cards = new JPanel(new CardLayout());

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

        JPanel red = new JPanel();
        red.setBackground(Color.RED);
        red.setPreferredSize( new Dimension(200, 50) );
        cards.add(red, "Red");

        JPanel green = new JPanel();
        green.setBackground(Color.GREEN);
        green.setPreferredSize( new Dimension(200, 50) );
        cards.add(green, "Green");

        JPanel blue = new JPanel();
        blue.setBackground(Color.BLUE);
        blue.setPreferredSize( new Dimension(200, 50) );
        cards.add(blue, "Blue");
    }

    public void actionPerformed(ActionEvent e)
    {
        Component visible = getVisibleCard();

        JComboBox comboBox = (JComboBox)e.getSource();
        String item = comboBox.getSelectedItem().toString();
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, item);

        //  change code below to create and show your card.

        if (visible == getVisibleCard())
            JOptionPane.showMessageDialog(cards, "Card (" + item + ") not found");

    }

    private Component getVisibleCard()
    {
        for(Component c: cards.getComponents())
        {
            if (c.isVisible())
                return c;
        }

        return null;
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("CardLayoutTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CardLayoutTest demo = new CardLayoutTest();
        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();
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM