Java by API/javax.swing/JRootPane

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

JRootPane: addAncestorListener(AncestorListener listener)

 

import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class Main {
  public static void main (String args[]) {
    JFrame f = new JFrame("Ancestor Sampler");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AncestorListener ancestorListener = new AncestorListener() {
      public void ancestorAdded(AncestorEvent ancestorEvent) {
        System.out.println ("Added");
      }
      public void ancestorMoved(AncestorEvent ancestorEvent) {
        System.out.println ("Moved");
      }
      public void ancestorRemoved(AncestorEvent ancestorEvent) {
        System.out.println ("Removed");
      }
    };
    f.getRootPane().addAncestorListener(ancestorListener);
    f.getRootPane().setVisible(false);
    f.getRootPane().setVisible(true);
    f.setSize (300, 200);
    f.setVisible (true);
  }
}





JRootPane.FRAME

 
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Adornment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}





JRootPane: getAccessibleContext()

 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.accessibility.AccessibleContext;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame implements ActionListener {
  JButton jb;
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AccessibleContext ac = getAccessibleContext();
    ac.setAccessibleDescription("Accessibility Demo1 description.");
    ac = getRootPane().getAccessibleContext();
    ac.setAccessibleName("Root pane");
    ac.setAccessibleDescription("Root pane description");
    ac = getGlassPane().getAccessibleContext();
    ac.setAccessibleName("Glass pane");
    ac.setAccessibleDescription("Glass pane description");
    ac = getLayeredPane().getAccessibleContext();
    ac.setAccessibleName("Layered pane");
    ac.setAccessibleDescription("Layered pane description");
    ac = getContentPane().getAccessibleContext();
    ac.setAccessibleName("Content pane");
    ac.setAccessibleDescription("Content pane description");
    JPanel p = new JPanel();
    ac = p.getAccessibleContext();
    ac.setAccessibleName("Panel");
    ac.setAccessibleDescription("Panel description");
    jb = new JButton("Press Me");
    jb.addActionListener(this);
    jb.setToolTipText("Press me for accessibility information.");
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 75);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    dumpInfo(getAccessibleContext());
  }
  void dumpInfo(AccessibleContext ac) {
    System.out.println("Name = " + ac.getAccessibleName());
    System.out.println("Description = " + ac.getAccessibleDescription());
    int nChildren = ac.getAccessibleChildrenCount();
    for (int i = 0; i < nChildren; i++)
      dumpInfo(ac.getAccessibleChild(i).getAccessibleContext());
  }
  public static void main(String[] args) {
    new MainClass("Accessibility Demo1");
  }
}





JRootPane: getContentPane()

 
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class MainClass {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRootPane root = f.getRootPane(); 
    Container content = root.getContentPane(); 
    content.add(new JButton("Hello")); 
    f.pack();
    f.setVisible(true);
  }
}





JRootPane: setDefaultButton(JButton defaultButton)

 
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
public class Main extends JPanel {
  public static void main(String[] a) {
    JDialog f = new JDialog();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    JButton btOK = new JButton("Press Enter to click me, I am the default.");
    btOK.setToolTipText("Save and exit");
    f.getRootPane().setDefaultButton(btOK);
    JPanel p = new JPanel();
    p.add(btOK);
    p.add(new JButton("I am NOT the default."));
    f.getContentPane().add(p);
    f.pack();
    f.setSize(new Dimension(300, 200));
    f.setVisible(true);
  }
}





JRootPane: setWindowDecorationStyle(int style)

 
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class MainClass {
  public static void main(final String args[]) {
    JFrame frame = new JFrame("Adornment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(true);
    frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
    frame.setSize(300, 100);
    frame.setVisible(true);
  }
}