Java Tutorial/Swing/JRootPane

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

Customizing JRootPane Look and Feel

Property StringObject TypeRootPane.actionMapActionMapRootPane.ancestorInputMapInputMapRootPane.colorChooserDialogBorderBorderRootPane.defaultButtonWindowKeyBindingsObject[ ]RootPane.errorDialogBorderBorderRootPane.fileChooserDialogBorderBorderRootPane.frameBorderBorderRootPane.informationDialogBorderBorderRootPane.plainDialogBorderBorderRootPane.questionDialogBorderBorderRootPane.warningDialogBorderBorderRootPaneUIString


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);
  }
}





JRootPane

The JRootPane class acts as a container delegate for the top-level Swing containers.


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);
  }
}





Setting the default button

  1. Dark border
  2. pressing the Enter key to trigger button"s event



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class SettingDefaultButtonJRootPane {
  public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button4 = new JButton("AAA");
    frame.add(button4,"Center");
    frame.add(new JButton("BBB"),"South");
    JRootPane rootPane = frame.getRootPane();
    rootPane.setDefaultButton(button4);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Setting Window Decoration Style

import javax.swing.JFrame;
import javax.swing.JRootPane;
public class AdornSample {
  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);
  }
}