accessibilitydemo2.java

来自「java 完全探索的随书源码」· Java 代码 · 共 95 行

JAVA
95
字号
// AccessibilityDemo2.java

import javax.accessibility.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class AccessibilityDemo2 extends JFrame
{
   AccessibilityDemo2 (String title)
   {
      super (title);

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      JToolBar toolBar = new JToolBar ();

      Action a = new AbstractAction ("Demo")
                     {
                        public void actionPerformed (ActionEvent e)
                        {
                           System.out.println ("Action taken.");
                        }
                     };

      JButton b = toolBar.add (a);
      b.setText ("Demo Button");
      b.setToolTipText ("Press me to take action.");

      JMenu mainMenu = new JMenu ("Menu");
      JMenuItem mi = mainMenu.add (a);
      mi.getAccessibleContext ().setAccessibleName ("Menu item");

      JMenuBar mb = new JMenuBar ();
      mb.add (mainMenu);
      setJMenuBar (mb);

      JPanel pane = new JPanel ();
      pane.setLayout (new BorderLayout ());
      pane.setPreferredSize (new Dimension (200, 100));
      pane.add (toolBar, BorderLayout.NORTH);
      setContentPane (pane);

      pack ();
      setVisible (true);
   }

   public static void main (String [] args)
   {
      AccessibilityDemo2 ad2;
      ad2 = new AccessibilityDemo2 ("Accessibility Demo2");

      try
      {
          Thread.sleep (5000);
      }
      catch (InterruptedException e) {}

      ad2.dumpActionInfo (ad2.getAccessibleContext ());
   }

   void dumpActionInfo (AccessibleContext ac)
   {
      AccessibleAction aa = ac.getAccessibleAction ();

      if (aa != null)
      {
          String s = ac.getAccessibleName ();
          System.out.println (s);

          int count = aa.getAccessibleActionCount ();

          for (int i = 0; i < count; i++)
          {
               s = aa.getAccessibleActionDescription (i);
               System.out.println ("Description = " + s);
          }
      }

      int nChildren = ac.getAccessibleChildrenCount ();

      for (int i = 0; i < nChildren; i++)
           dumpActionInfo (ac.getAccessibleChild (i)
                             .getAccessibleContext ());
   }
}

⌨️ 快捷键说明

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