Java Tutorial/I18N/ComponentOrientation

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

ComponentOrientation by Component type

import java.awt.ruponent;
import java.awt.ruponentOrientation;
import java.awt.Container;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
public class JComboBoxDemo extends JFrame {
  public JComboBoxDemo() {
    JComboBox itemsComboBox = new JComboBox(new String[]{ "A", "L", "M" });
    itemsComboBox.setEditable(true);
    itemsComboBox.setMaximumRowCount(3);
    this.getContentPane().add(itemsComboBox);
    itemsComboBox.setVisible(true);
    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
    this.validate();
    this.repaint();
  }
  public static void main(String[] args) {
    JFrame frame = new JComboBoxDemo();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
  private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);
    if (c instanceof JMenu) {
      JMenu menu = (JMenu) c;
      int ncomponents = menu.getMenuComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(menu.getMenuComponent(i), o);
      }
    } else if (c instanceof Container) {
      Container container = (Container) c;
      int ncomponents = container.getComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(container.getComponent(i), o);
      }
    }
  }
}