gpt4 book ai didi

java - 更改了其中一个监听器适合可见性的 JPanel

转载 作者:IT老高 更新时间:2023-10-28 20:39:53 24 4
gpt4 key购买 nike

AncestorListenerComponentListenerHierarchyListener 是否有一些规则或好的/坏的体验,使用 JPanel 监听更改的可见性JComponents?

其中一个比其他的更好或更安全吗?我特别想知道 JPanel/JComponent 何时以及如何隐藏。

注意以下代码包含不正确的 Swing 规则,例如使用 Thread.sleep(int),在这种情况下,我可以打印出正确的 Listeners 顺序在 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");
}
}

最佳答案

如果您想准确监听可见性变化 - 使用 ComponentListenerComponentAdapter:

    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 即使 Component 未添加到任何 Container 并因此不会显示在全部!

可见性 a 的目的略有不同。您可以使用它来手动隐藏已添加并显示在应用程序某处的 Component。在这种情况下,(如果您使用 setVisible(false))它将被隐藏,并且该 Component 的每个 ComponentListener 都会被告知该更改.

所以,谈论实际可见性...

这是您应该用来收听实际组件出现/消失的内容:

    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
}
} );

我总是使用该监听器来确定何时将 Component 添加到某处,并在移动/移除它时进行监听。

此外,您可以随时通过调用 isShowing() 方法检查 Component 是否对应用程序用户真正可见:

boolean userCanSeeThePanel = panel.isShowing();

仅当面板被添加到 VISIBLE 到用户框架并且 isVisible() 标志也为真(通常为 true,除非您将其设置为 false)。

关于可见性,我想这就是我能告诉你的全部内容了。我可能误解了你的问题。如果我在这种情况下错了,请纠正我。

关于java - 更改了其中一个监听器适合可见性的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10880326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com