监听器中哪一个适合可见性的JPanel被改变了 [英] JPanel which one of Listeners is proper for visibility is changed
问题描述
AncestorListener
, ComponentListener
或是否存在一些规则或好/坏体验HierarchyListener
使用 JPanel
和 JComponents
监听变更的可见性?
Are there some rules, or good/bad experiences with AncestorListener
, ComponentListener
or HierarchyListener
listening for visibility of changes with JPanel
and JComponents
?
其中一个比其他人更好还是更安全?我特别想知道何时以及如何 JPanel
/ JComponent
被隐藏。
Is one of them better or safer than the others? I would especially like to know about when and how JPanel
/ JComponent
is hidden.
请注意以下代码包含不正确的Swing规则,例如使用 Thread.sleep(int)
,在这种情况下,允许我打印出正确的Swing GUI中监听器
的顺序
Notice the following code contains incorrect Swing rules, like using Thread.sleep(int)
, in this case, to allow me to print-out correct order of Listeners
in Swing GUI
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class CardlayoutTest extends JFrame {
private static final long serialVersionUID = 1L;
public CardLayout card = new CardLayout();
public CardlayoutTest() {
JPanel pnlA = new JPanel(new BorderLayout());
pnlA.add(new JButton("A"), BorderLayout.CENTER);
JPanel pnlB = new JPanel(new BorderLayout());
pnlB.add(new JButton("B"), BorderLayout.CENTER);
JPanel pnlC = new JPanel(new BorderLayout());
pnlC.add(new JButton("C"), BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(card);
add(pnlA, "A");
add(pnlB, "B");
add(pnlC, "C");
pnlA.addAncestorListener(new EventHandler());
pnlB.addAncestorListener(new EventHandler());
pnlC.addAncestorListener(new EventHandler());
pnlA.addHierarchyListener(new EventHandler());
pnlB.addHierarchyListener(new EventHandler());
pnlB.addHierarchyListener(new EventHandler());
pnlA.addComponentListener(new EventHandler());
pnlB.addComponentListener(new EventHandler());
pnlB.addComponentListener(new EventHandler());
}
class EventHandler implements AncestorListener, ComponentListener, HierarchyListener {
@Override
public void ancestorAdded(AncestorEvent event) {
System.out.println("CardlayoutTest.EventHandler.ancestorAdded()");
}
@Override
public void ancestorMoved(AncestorEvent event) {
System.out.println("CardlayoutTest.EventHandler.ancestorMoved()");
}
@Override
public void ancestorRemoved(AncestorEvent event) {
System.out.println("CardlayoutTest.EventHandler.ancestorRemoved()");
}
@Override
public void hierarchyChanged(HierarchyEvent e) {
System.out.println("Components Change: " + e.getChanged());
if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
if (e.getComponent().isDisplayable()) {
System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
} else {
System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
}
}
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
if (e.getComponent().isDisplayable()) {
System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
} else {
System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
}
}
}
public void componentHidden(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Hidden");
}
public void componentMoved(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Moved");
}
public void componentResized(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Resized ");
}
public void componentShown(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Shown");
}
}
public static void main(String[] args) {
CardlayoutTest t = new CardlayoutTest();
t.setSize(500, 500);
System.out.println("CardlayoutTest.main()------------------------ FIRST");
t.card.show(t.getContentPane(), "A");
t.setVisible(true);
System.out.print("\n");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("CardlayoutTest.main()------------------------ SECOND");
t.card.show(t.getContentPane(), "B");
System.out.print("\n");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("CardlayoutTest.main()------------------------ THIRD");
t.card.show(t.getContentPane(), "C");
System.out.print("\n");
}
}
推荐答案
如果您想要完全听取可见性更改 - 使用 ComponentListener
或 ComponentAdapter
:
If you want to listen EXACTLY the visibility changes - use ComponentListener
or ComponentAdapter
:
JPanel panel = new JPanel ();
panel.addComponentListener ( new ComponentAdapter ()
{
public void componentShown ( ComponentEvent e )
{
System.out.println ( "Component shown" );
}
public void componentHidden ( ComponentEvent e )
{
System.out.println ( "Component hidden" );
}
} );
但是这种可见性可能不是您想到的。 isVisible()
标志将是 true
即使组件
没有添加到任何容器
,因此根本没有显示!
But that visibility might not be the one you think about. isVisible()
flag will be true
even if the Component
is not added to any Container
and hence not showing at all!
该可见性a的目的略有不同。您可以使用它来手动隐藏已添加并显示在应用程序中某处的组件
。在这种情况下,(如果你使用 setVisible(false)
)它将被隐藏,并且每个 ComponentListener
都是组件
将被告知该更改。
That visibility a has a slightly different purpose. You can use it to manually hide the Component
that is already added and shown somewhere in your application. In that case, (if you use setVisible(false)
) it will become hidden and every ComponentListener
of that Component
will be informed about that change.
所以,谈论实际可见性......
So, talking about actual visibility...
这是您应该用来聆听实际组件出现/消失的内容:
This is what you should use to listen to actual component appearance/disappearance:
JPanel panel = new JPanel ();
panel.addAncestorListener ( new AncestorListener ()
{
public void ancestorAdded ( AncestorEvent event )
{
// Component added somewhere
}
public void ancestorRemoved ( AncestorEvent event )
{
// Component removed from container
}
public void ancestorMoved ( AncestorEvent event )
{
// Component container moved
}
} );
我总是使用该监听器来确定何时组件
被添加到某处,也可以在移动/移除时进行监听。
I always use that listener to determine when the Component
is added somewhere and also to listen when it is moved/removed.
此外,您始终可以检查组件$ c应用程序用户实际上可以通过调用
isShowing()
方法看到$ c>:
Also, you can always check if the Component
is actually visible to application user by calling isShowing()
method:
boolean userCanSeeThePanel = panel.isShowing();
这将返回 true
仅限该面板被添加到VISIBLE到用户框架和 isVisible()
标志也是真的(它通常是 true
,除非你设置它到 false
)。
This will return true
ONLY if that panel is added to VISIBLE to user frame and isVisible()
flag is also true (it is usually true
, unless you set it to false
).
我想我可以告诉你关于可见性的全部内容。我可能误解了你的问题。如果我在这种情况下错了,请纠正我。
I guess that's all I can tell you about visibility. I might have misunderstood your question. Correct me if I am wrong in that case.
这篇关于监听器中哪一个适合可见性的JPanel被改变了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!