Java/Swing JFC/RootPane

Материал из Java эксперт
Версия от 06:45, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

An example of interacting directly with the JRootPane of a JFrame

 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O"Reilly 
*/
// RootExample.java
//An example of interacting directly with the JRootPane of a JFrame.
//
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class RootExample {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRootPane root = f.getRootPane(); // XXX Pay attention to these
    Container content = root.getContentPane(); // XXX lines. They get more
    content.add(new JButton("Hello")); // XXX explanation in the book.
    f.pack();
    f.setVisible(true);
  }
}





Another example of interacting with the root pane

 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O"Reilly 
*/
// RootExample2.java
//Another example of interacting with the root pane. We set the menubar
//for the frame directly through the root pane in this example.
//
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRootPane;
public class RootExample2 {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JRootPane root = f.getRootPane();
    // Create a menu bar
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);
    menu.add("Open");
    menu.add("Close");
    root.setJMenuBar(bar);
    // Add a button to the content pane
    root.getContentPane().add(new JButton("Hello World"));
    // Display the UI
    f.pack();
    f.setVisible(true);
  }
}





Interact directly with the JRootPane of a JFrame

 
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class Main {
  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);
  }
}





Make a JFrame looks like a JDialog

 
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class Main extends JFrame {
  public Main() {
    setTitle("like JDialog");
    setSize(new Dimension(500, 100));
    setUndecorated(true);
    setResizable(false);
    getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  public static void main(String[] args) {
    new Main().setVisible(true);
  }
}





No direct interaction with JRootPane

 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O"Reilly 
*/
// RootExample3.java
//Similar to RootExample2, but this version uses the setJMenuBar() method
//from JFrame to attach the menu. No (direct) interaction with JRootPane
//is needed.
//
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class RootExample3 extends JFrame {
  public RootExample3() {
    super("RootPane Menu Demo");
    setSize(220, 100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    // Create a menu bar
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);
    menu.add("Open");
    menu.add("Close");
    setJMenuBar(bar);
    // Add a button to the content pane
    getContentPane().add(new JButton("Hello World"));
  }
  public static void main(String[] args) {
    RootExample3 re3 = new RootExample3();
    re3.setVisible(true);
  }
}