Java Tutorial/Swing/Accessible — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:31, 31 мая 2010

14. Associating a Label with a Component

import javax.swing.JLabel;
import javax.swing.JTextField;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTextField component = new JTextField();
    JLabel label = new JLabel("Name:");
    label.setDisplayedMnemonic("N");
    label.setLabelFor(component);
  }
}





14. implement Accessible to make your object accessible

import java.awt.IllegalComponentStateException;
import java.util.Locale;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.accessibility.AccessibleStateSet;
public class Main implements Accessible {
  public AccessibleContext getAccessibleContext() {
    return new AccessibleContext() {
      @Override
      public AccessibleRole getAccessibleRole() {
        return null;
      }
      @Override
      public AccessibleStateSet getAccessibleStateSet() {
        return null;
      }
      @Override
      public int getAccessibleIndexInParent() {
        return 0;
      }
      @Override
      public int getAccessibleChildrenCount() {
        return 0;
      }
      @Override
      public Accessible getAccessibleChild(int i) {
        return null;
      }
      @Override
      public Locale getLocale() throws IllegalComponentStateException {
        return null;
      }
    };
  }
}





14. Setting a Description for Image Icons

import javax.swing.ImageIcon;
public class Main {
  public static void main(String[] argv) throws Exception {
    ImageIcon icon = new ImageIcon("image.gif");
    icon.setDescription("Description of Image");
  }
}





14. Setting a Keyboard Accelerator for a Menu Item

import java.awt.event.KeyEvent;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class Main {
  public static void main(String[] argv) throws Exception {
    JMenuItem item = new JMenuItem("Item");
    item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.SHIFT_MASK));
  }
}





14. Setting a Mnemonic for a Menu for Accessible

import javax.swing.JMenu;
public class Main {
  public static void main(String[] argv) throws Exception {
    JMenu menu = new JMenu("Menu");
    menu.setMnemonic("M");
  }
}





14. Setting a Mnemonic for Buttons for Accessible

import javax.swing.JButton;
import javax.swing.JCheckBox;
public class Main {
  public static void main(String[] argv) throws Exception {
    JButton button = new JButton("Button");
    button.setMnemonic("B");
    JCheckBox checkBox = new JCheckBox("CheckBox");
    checkBox.setMnemonic("C");
  }
}





14. Setting an Accessible Name for an Image Button

import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Main {
  public static void main(String[] argv) throws Exception {
  JButton button = new JButton(new ImageIcon("image.gif"));
    button.setToolTipText("Button Name");
    button.getAccessibleContext().setAccessibleName("Button Name");
  }
}