English 中文(简体)
如何获得贾瓦卡的顶卡卡
原标题:How to get the top card in Java s CardLayout

Is it possible to get the top card in Java s CardLayout? I ve tried looping through each component to check for visibility with isVisible() but it seems that they re all "visible".

Edit:我用“顶卡”指目前展示的“顶”卡,而不是头张或最后一张卡。 此外,我不知道这是否有助于,但我看一看一看一只JPanel(或其子等)。

Edit: Code snippet

for (Component component : getComponents()) {
 if (component instanceof JPanel && component.isVisible()) {
  currentPanel = (JPanel) component;
  System.out.println(currentPanel.getClass().getName());
 }
}  

以上代码总是按每个构成部分类别打印,而不论这些类别是可见卡。

Edit:作为学校任务的一部分使用Im。 我不想在这里获得自由,但这项任务并不围绕这一布局。 这似乎只是小组之间变化的最方便的布局。 我的教师已经指出,该项目中没有第三方守则。 我先从足球赛中看到了执行工作,但我可以使用。 我可以松散地实施类似的特征,也许可以在文件中提及,但我只能下载和使用。

Edit: The reason I m trying to get the top card is because I have a toolbar with an "Add" button. Instead of having one add button for each of my two possible things I wanted it to know which to add simply by looking at what panel is currently being viewed. If there is another, more appropriate way to do this please let me know.

http://www.ohchr.org。 感谢大家提供帮助。 我描述了问题。 我认为这是我的过错,因为我没有提供足够的细节。 我的两张卡片<代码>。 JScrollPane,我还需要研究其内容,以发现其中的一个小组是我看到的。 我对关于发烧单本身的<代码>isible()进行了笔录检查,即检查时发现,总是看得见的,正是发卷机需要核实。

public JPanel getCurrentPanel() {
        JPanel currentPanel = null;

        for (Component component : getComponents()) {
            if (component.isVisible()) {
                if (component instanceof JPanel) 
                    currentPanel = (JPanel) component;
                else if (component instanceof JScrollPane)
                    currentPanel = (JPanel) ((JScrollPane) component).getViewport().getComponent(0);
            }
        }

        return currentPanel;
    }
最佳回答

如果你修改该法典,你就会在问题中添加,你可以很容易地告诉什么卡。 在我的测试框架中,当塔顿被推向时,它先将目前可见的卡的名字印在后面。 修改你的法典,必须牢记的是,你需要就实际上有卡迪乌特的构成部分呼吁支持者。 这样,只有一名儿童明显可见。 我在你的法典中猜测,你在把你的团队带上卡莱乌的画面上向各位支持者发出呼吁。

class TestFrame
        extends JFrame
        implements ActionListener
{
    public TestFrame()
    {
        cards = new JPanel(new CardLayout() );
        card_list = new JPanel[5];

        for (int i = 0; i < card_list.length; i++) {
            String text = "Card " + i;
            JPanel card = createCard(text);
            card_list[i] = card;
            cards.add(card, text);
        }

        add(cards);
    }

public JPanel getCurrentCard()
{
    JPanel card = null;

    for (Component comp : cards.getComponents() ) {
        if (comp.isVisible() == true) {
            card = (JPanel)comp;
            System.out.println(card.getName() );
        }
    }
    System.out.println();

    return card;
}

public void actionPerformed(ActionEvent evt)
{
    JPanel card = getCurrentCard();

    CardLayout cl = (CardLayout)(cards.getLayout() );
    cl.next(cards);
}

    public JPanel createCard(String text)
    {
        JPanel card = new JPanel();
        card.setName(text);

        JButton btn = new JButton(text);
        btn.addActionListener(this);

        card.add(btn);
        return card;
    }

    JPanel cards;
    JPanel[] card_list;
}

在“Perform()”方法中,我印刷了该卡的名称,但你提到了目前可见的卡。

问题回答

在实施<代码>CardLayout.first()时,应当将最高卡贴上以下标记:

int ncomponents = parent.getComponentCount();
for (int i = 0 ; i < ncomponents ; i++) {
    Component comp = parent.getComponent(i);
    if (comp.isVisible()) {
        comp.setVisible(false);     // hide previously visible components
        break;
    }
}

if (ncomponents > 0) {
    currentCard = 0;
    parent.getComponent(0).setVisible(true);   // set visibility of component 0
    parent.validate();
}

因此,我相信你可以通过<代码>yourContainer.getComponent(0)持有“顶卡”。

你们能够根据可见度确定变化的变量

例如

int cardShow;

页: 1

cards.show(card1, "card1");
cardShow = 1;

以及当你试图获得明显可见的卡片时

if (cardShow == 1)
//code in case of card1 is visible




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签