如何在 Java 的 CardLayout 中获得顶级卡片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4413251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the top card in Java's CardLayout
提问by alexcoco
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".
是否有可能在 Java 的 CardLayout 中获得顶级卡片?我尝试遍历每个组件以使用 isVisible() 检查可见性,但似乎它们都是“可见的”。
Edit: By "top card" I mean the one that's currently at the "top", being displayed, not the first or last cards. Also, I don't know if it helps but I'm looking for a JPanel (or a subclass thereof)
编辑:“顶牌”是指当前显示在“顶”的牌,而不是第一张或最后一张牌。另外,我不知道它是否有帮助,但我正在寻找 JPanel(或其子类)
Edit: Code snippet
编辑:代码片段
for (Component component : getComponents()) {
if (component instanceof JPanel && component.isVisible()) {
currentPanel = (JPanel) component;
System.out.println(currentPanel.getClass().getName());
}
}
The above code always prints out every component class's name, regardless if they are the visible card or not.
上面的代码总是打印出每个组件类的名称,不管它们是否是可见的卡片。
Edit: I'm using this as part of a school assignment. I'm not trying to get a freebie here, the assignment does not revolve around this layout. It just seems to be the most convenient layout for changing between panels. My teacher has specified that there is to be no third party code in the project. I've seen the implementation from camickr's link before but I can't use it. I can loosely implement similar features it and maybe give a reference it in the documentation but I can't simply download and use it.
编辑:我将此用作学校作业的一部分。我不是想在这里获得免费赠品,任务不是围绕这个布局展开的。它似乎是在面板之间切换的最方便的布局。我的老师已经指定项目中不能有第三方代码。我以前从 camickr 的链接中看到过实现,但我不能使用它。我可以松散地实现类似的功能,并可能在文档中提供参考,但我不能简单地下载和使用它。
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.
编辑:我试图获得顶级卡片的原因是因为我有一个带有“添加”按钮的工具栏。我想让它通过查看当前正在查看的面板来知道要添加的内容,而不是为我的两个可能的东西中的每一个添加一个按钮。如果有另一种更合适的方法来做到这一点,请告诉我。
Edit: Thanks everyone for helping out. I figured out what the problem was. I guess it was my fault since I didn't provide enough detail. Two of my cards are JScrollPane
s and I also needed to look through its contents to find out if one of those panels was the one I was seeing. I didn't check for isVisible()
on the scroll pane itself, i had been looking at it's contends that are always visible, it was the scroll pane who's visibility I needed to verify.
编辑:感谢大家的帮助。我想出了问题所在。我想这是我的错,因为我没有提供足够的细节。我的两张卡片是JScrollPane
s,我还需要查看它的内容,以确定其中一个面板是否是我看到的那个。我没有检查isVisible()
滚动窗格本身,我一直在查看它始终可见的竞争,这是我需要验证的滚动窗格的可见性。
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;
}
采纳答案by robert_x44
If you modify the code snippet you added to the question you can easily tell which card is visible. In my test frame, when the button is pushed, it prints out the name of the currently visible card, before flipping to the next card. Modifying your code, the important thing to keep in mind is that you need to call getComponents() on the component that actually has the CardLayout. That way, only one of its children is visible. I'm guessing in your code you are calling getComponents on the frame that contains your JPanel with CardLayout.
如果您修改添加到问题中的代码片段,您可以轻松分辨出哪张卡片是可见的。在我的测试框架中,当按下按钮时,它会在翻到下一张卡片之前打印出当前可见卡片的名称。修改您的代码,要记住的重要一点是您需要在实际具有 CardLayout 的组件上调用 getComponents()。这样,只有一个孩子是可见的。我猜在您的代码中,您是在包含带有 CardLayout 的 JPanel 的框架上调用 getComponents 。
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;
}
In the actionPerformed() method, I printed the name of the card, but you have a reference to the currently visible card.
在 actionPerformed() 方法中,我打印了卡片的名称,但您有对当前可见卡片的引用。
回答by camickr
I extended the CardLayout to provide this type of functionality. See Card Layout Focus.
我扩展了 CardLayout 以提供这种类型的功能。请参阅卡片布局重点。
Edit:
编辑:
Then loop through the components looking for the compnent with a ZOrder of 0. This is the one that is painted on top. Actually you don't even have to iterate through all the components, just get the component at position 0. Check out my blog on the Overlap Layout for a more detailed description of how ZOrder works.
然后循环遍历组件,寻找 ZOrder 为 0 的组件。这是绘制在顶部的组件。实际上,您甚至不必遍历所有组件,只需在位置 0 处获取组件即可。查看我关于重叠布局的博客,了解有关 ZOrder 工作原理的更详细说明。
Component visibleComponent = panelWithCardLayout.getComponent(0);
Edit2:
编辑2:
The above edit is wrong. ZOrder has no meaning when used with a CardLayout. So you can't simply get component 0.
上面的编辑是错误的。ZOrder 与 CardLayout 一起使用时没有意义。所以你不能简单地获取组件 0。
回答by aioobe
In the implementation of CardLayout.first()
which is supposed to set the top card visible you have the following:
在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();
}
so I suppose you can get hold of the "top card" by yourContainer.getComponent(0)
.
所以我想你可以通过yourContainer.getComponent(0)
.
回答by road.cleaner
You can set variable that changes depending on the card visibility
您可以设置根据卡片可见性而变化的变量
for example
例如
int cardShow;
and then you set your card visible
然后你设置你的卡片可见
cards.show(card1, "card1");
cardShow = 1;
and when you try to get which card is visible just use
当您尝试获取可见的卡片时,只需使用
if (cardShow == 1)
//code in case of card1 is visible