⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hierlistenerdemo.java

📁 《java事件处理指南》一书的代码,好东西
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class HierListenerDemo extends JFrame implements ActionListener{   private JButton button, removePanelButton, removeButton;   private JPanel centerPanel;   private JTextField jtf;   public HierListenerDemo()   {/*  A JButton is placed on a JFrame.  The button registers   *//*  a HierarchyListener.                                     */      button = new JButton("Button");      button.setBorder(BorderFactory.createRaisedBevelBorder());      button.addHierarchyListener(new HierListener());/*  Two more buttons are placed on the JFrame.  These buttons   *//*  are used to change the visibility of the center panel.      *//*  These buttons register an ActionListener.                   */      removePanelButton = new JButton("Hide Panel");      removePanelButton.setActionCommand("hide");      removePanelButton.setBorder(BorderFactory.createRaisedBevelBorder());      removePanelButton.addActionListener(this);      removeButton = new JButton("Show Panel");      removeButton.setActionCommand("show");      removeButton.setBorder(BorderFactory.createRaisedBevelBorder());      removeButton.addActionListener(this);      jtf = new JTextField(15);      jtf.setEditable(false);      centerPanel = new JPanel();      centerPanel.setName("center");      centerPanel.add(button);      JPanel southPanel = new JPanel();      southPanel.add(jtf);      southPanel.add(removePanelButton);      southPanel.add(removeButton);      getContentPane().add(centerPanel, BorderLayout.CENTER);      getContentPane().add(southPanel, BorderLayout.SOUTH);      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      setBounds(100, 100, 400, 200);      setVisible(true);   }/*  Pressing one of the two buttons on the bottom of the JFrame   *//*  changes the visibility of the center panel.  This causes the  *//*  JButton contained by the center panel to generate a           *//*  HierarchyEvent.                                               */   public void actionPerformed(ActionEvent event)   {      if ( event.getActionCommand().equals("hide"))      {         centerPanel.setVisible(false);      }      if ( event.getActionCommand().equals("show"))      {         centerPanel.setVisible(true);      }      invalidate();      validate();   }/*  The HierarchyListener is implemented as an inner class.  When   *//*  the button on the center panel fires a HierarchyEvent, it is    *//*  sent to the hierarchyChanged() method.  This method determines  *//*  if the event was due to a change in the visibility of some      *//*  part of the component hierarchy.  The JTextField is updated to  *//*  indicate if the JButton has become visible or invisible because *//*  some part of its component hierarchy (the center panel) has     *//*  become visible or invisible.                                    */   class HierListener implements HierarchyListener   {      public void hierarchyChanged(HierarchyEvent event)      {         if ( event.getChangeFlags() == HierarchyEvent.SHOWING_CHANGED )         {            if ( event.getChanged().isVisible() )            {               jtf.setText("Button is visible");            }            else            {               jtf.setText("Button is not visible");            }         }      }   }   public static void main(String args[])   {      HierListenerDemo demo = new HierListenerDemo();   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -