Java by API/java.awt.event/HierarchyEvent — различия между версиями

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

Версия 17:43, 31 мая 2010

HierarchyEvent: getChangeFlags()

 
import java.awt.BorderLayout;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    HierarchyListener hierarchyListener = new HierarchyListener() {
      public void hierarchyChanged(HierarchyEvent e) {
        long flags = e.getChangeFlags();
        System.out.println(e.getSource());        
        if ((flags & HierarchyEvent.SHOWING_CHANGED) ==
             HierarchyEvent.SHOWING_CHANGED) {
          splitPane.setDividerLocation(.75);
        }
      }
    };
    splitPane.addHierarchyListener(hierarchyListener);
    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);
    
    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);
  }
}





HierarchyEvent.SHOWING_CHANGED

 
import java.awt.BorderLayout;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    HierarchyListener hierarchyListener = new HierarchyListener() {
      public void hierarchyChanged(HierarchyEvent e) {
        long flags = e.getChangeFlags();
        System.out.println(e.getSource());        
        if ((flags & HierarchyEvent.SHOWING_CHANGED) ==
             HierarchyEvent.SHOWING_CHANGED) {
          splitPane.setDividerLocation(.75);
        }
      }
    };
    splitPane.addHierarchyListener(hierarchyListener);
    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);
    
    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);
  }
}