下面列出了怎么用javax.swing.JRootPane的API类实例代码及写法,或者点击链接到github查看源代码。
protected void frameStateChanged() {
if( window == null || rootPane.getWindowDecorationStyle() != JRootPane.FRAME )
return;
if( window instanceof Frame ) {
Frame frame = (Frame) window;
boolean resizable = frame.isResizable();
boolean maximized = ((frame.getExtendedState() & Frame.MAXIMIZED_BOTH) != 0);
iconifyButton.setVisible( true );
maximizeButton.setVisible( resizable && !maximized );
restoreButton.setVisible( resizable && maximized );
} else {
// hide buttons because they are only supported in frames
iconifyButton.setVisible( false );
maximizeButton.setVisible( false );
restoreButton.setVisible( false );
revalidate();
repaint();
}
}
protected void installTitlePane(JRootPane root, LuckTitlePanel titlePane, Window window)
{
JLayeredPane layeredPane = root.getLayeredPane();
JComponent oldTitlePane = getTitlePane();
if (oldTitlePane != null)
{
oldTitlePane.setVisible(false);
layeredPane.remove(oldTitlePane);
}
if (titlePane != null)
{
titlePane.setOpaque(true);
layeredPane.add(titlePane, JLayeredPane.FRAME_CONTENT_LAYER);
titlePane.setVisible(true);
}
this.titlePane = titlePane;
}
/**
* Manage low level key strokes ESCAPE : Close the window
*
* @return
*/
protected JRootPane createRootPane() {
JRootPane rootPane = new JRootPane();
KeyStroke strokeEscape = KeyStroke.getKeyStroke("ESCAPE");
@SuppressWarnings("serial")
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
}
};
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(strokeEscape, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", actionListener);
return rootPane;
}
/**
* Uninstalls any state that <code>installClientDecorations</code> has
* installed.
* <p>
* NOTE: This may be called if you haven't installed client decorations
* yet (ie before <code>installClientDecorations</code> has been invoked).
*
* @param root the root
*/
private void uninstallClientDecorations(JRootPane root)
{
uninstallBorder(root);
uninstallWindowListeners(root);
setTitlePane(root, null);
uninstallLayout(root);
// We have to revalidate/repaint root if the style is JRootPane.NONE
// only. When we needs to call revalidate/repaint with other styles
// the installClientDecorations is always called after this method
// imediatly and it will cause the revalidate/repaint at the proper
// time.
int style = root.getWindowDecorationStyle();
if (style == JRootPane.NONE)
{
root.repaint();
root.revalidate();
}
// Reset the cursor, as we may have changed it to a resize cursor
if (window != null)
{
window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
window = null;
}
protected JInternalFrame createInstanceImpl() {
JInternalFrame frame = new JInternalFrame(title, resizable, closable, maximizable, iconable) {
protected JRootPane createRootPane() {
return _rootPane == null ? null : _rootPane.createInstance();
}
public void addNotify() {
try {
// Doesn't seem to work correctly
setClosed(_isClosed);
setMaximum(_isMaximum);
setIcon(_isIcon);
setSelected(_isSelected);
} catch (PropertyVetoException ex) {}
}
};
return frame;
}
/**
* Installs the necessary Listeners on the parent <code>Window</code>, if
* there is one.
*
* <p>This takes the parent so that cleanup can be done from <code>
* removeNotify</code>, at which point the parent hasn't been reset yet.</p>
*
* @param root the JRootPane.
* @param parent The parent of the JRootPane
*/
private void installWindowListeners(JRootPane root, Component parent) {
if (parent instanceof Window) {
window = (Window) parent;
} else {
window = SwingUtilities.getWindowAncestor(parent);
}
if (window != null) {
if (mouseInputListener == null) {
mouseInputListener = createWindowMouseInputListener(root);
}
window.addMouseListener(mouseInputListener);
window.addMouseMotionListener(mouseInputListener);
if (windowListener == null) {
windowListener = createFocusListener();
window.addWindowListener(windowListener);
}
}
}
private void resetState() {
state = STATE_NOOP;
JRootPane pane = SwingUtilities.getRootPane(comp);
glass.setVisible(false);
if (pane != null && oldGlass != null) {
// when clicking results in hidden slide window, pne can be null?
// how to avoid?
JComponent current = (JComponent) pane.getGlassPane();
if (current instanceof GlassPane) {
pane.setGlassPane(oldGlass);
}
}
if( null != comp )
comp.setCursor(null);
oldGlass = null;
startPoint = null;
}
private static JLabel findNotificationLabel (Container container) {
for (Component component : container.getComponents ()) {
if (component.getClass ().getName ().indexOf (NOTIFICATION_LABEL_NAME) != -1) {
return (JLabel) component;
}
if (component instanceof JRootPane) {
JRootPane rp = (JRootPane) component;
return findNotificationLabel (rp.getContentPane ());
}
if (component instanceof JPanel) {
JPanel p = (JPanel) component;
return findNotificationLabel (p);
}
}
return null;
}
protected void initGUI()
{
// set dialog full transparent
setUndecorated(true);
AWTUtilities.setWindowOpaque(this, false);
this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
// init gui
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.setOpaque(false);
setContentPane(contentPane);
// others setup
this.setFocusable(false);
this.setFocusableWindowState(false);
// this.setAlwaysOnTop(true);
this.setVisible(false);
}
protected JInternalFrame createInstanceImpl() {
JInternalFrame frame = new JInternalFrame(title, resizable, closable, maximizable, iconable) {
protected JRootPane createRootPane() {
return _rootPane == null ? null : _rootPane.createInstance();
}
public void addNotify() {
try {
// Doesn't seem to work correctly
setClosed(_isClosed);
setMaximum(_isMaximum);
setIcon(_isIcon);
setSelected(_isSelected);
} catch (PropertyVetoException ex) {}
}
};
return frame;
}
/**
* Sets the window title pane -- the JComponent used to provide a plaf a way
* to override the native operating system's window title pane with one
* whose look and feel are controlled by the plaf. The plaf creates and sets
* this value; the default is null, implying a native operating system
* window title pane.
*
* @param root content the <code>JComponent</code> to use for the
* window title pane.
* @param titlePane the SeaGlassTitlePane.
*/
private void setTitlePane(JRootPane root, JComponent titlePane) {
JLayeredPane layeredPane = root.getLayeredPane();
JComponent oldTitlePane = getTitlePane();
if (oldTitlePane != null) {
oldTitlePane.setVisible(false);
layeredPane.remove(oldTitlePane);
}
if (titlePane != null) {
layeredPane.add(titlePane, JLayeredPane.FRAME_CONTENT_LAYER);
titlePane.setVisible(true);
}
this.titlePane = titlePane;
}
public Dimension maximumLayoutSize(Container parent)
{
Insets insets = parent.getInsets();
JRootPane root = (JRootPane) parent;
Dimension cpd = null;
if (root.getContentPane() != null)
{
cpd = root.getContentPane().getMaximumSize();
}
else
{
cpd = root.getSize();
}
return getDimension(insets, cpd.width, cpd.height);
}
private Component getLayeredPane() {
Container parent = null;
if (this.owner != null) {
parent = this.owner instanceof Container ? (Container) this.owner : this.owner.getParent();
}
for (Container p = parent; p != null; p = p.getParent()) {
if (p instanceof JRootPane) {
if (p.getParent() instanceof JInternalFrame) {
continue;
}
parent = ((JRootPane) p).getLayeredPane();
} else if (p instanceof Window) {
if (parent == null) {
parent = p;
}
break;
} else if (p instanceof JApplet) {
break;
}
}
return parent;
}
/**
* Sets the window title pane -- the JComponent used to provide a plaf a
* way to override the native operating system's window title pane with
* one whose look and feel are controlled by the plaf. The plaf creates
* and sets this value; the default is null, implying a native operating
* system window title pane.
*
* @param root the root
* @param titlePane the title pane
*/
private void setTitlePane(JRootPane root, JComponent titlePane)
{
JLayeredPane layeredPane = root.getLayeredPane();
JComponent oldTitlePane = getTitlePane();
if (oldTitlePane != null)
{
oldTitlePane.setVisible(false);
layeredPane.remove(oldTitlePane);
}
if (titlePane != null)
{
layeredPane.add(titlePane, JLayeredPane.FRAME_CONTENT_LAYER);
titlePane.setVisible(true);
}
this.titlePane = titlePane;
}
/**
* Does the work required to make the parameter JDialog be hidden when pressing escape
*
* @param popup
*/
public static void setupHideOnEscape(final JDialog popup)
{
Action aa = new AbstractAction()
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent event)
{
popup.setVisible(false);
}
};
final String mapKey = "escapePressed";
JRootPane root = popup.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke, mapKey);
root.getActionMap().put(mapKey, aa);
}
/**
* Installs the necessary state onto the JRootPane to render client
* decorations. This is ONLY invoked if the <code>JRootPane</code> has a
* decoration style other than <code>JRootPane.NONE</code>.
*
* @param root the JRootPane.
*/
private void installClientDecorations(JRootPane root) {
installBorder(root);
if (root.getParent() instanceof JFrame || root.getParent() instanceof JDialog) {
if (PlatformUtils.isMac()) {
makeFrameBackgroundTransparent(root);
} else {
shapeWindow(root);
}
}
JComponent titlePane = createTitlePane(root);
setTitlePane(root, titlePane);
installWindowListeners(root, root.getParent());
installLayout(root);
if (window != null) {
root.revalidate();
root.repaint();
}
}
/**
* Overrides {@link JDialog#createRootPane()} to return a root pane that
* hides the window when the user presses the ESCAPE key.O
*/
protected JRootPane createRootPane()
{
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
JRootPane rootPane = new JRootPane();
rootPane.registerKeyboardAction(new ActionListener()
{
public void actionPerformed(ActionEvent actionEvent)
{
setVisible(false);
}
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
return rootPane;
}
/**
* Maps {@code JDialog.getRootPane()} through queue
*/
public JRootPane getRootPane() {
return (runMapping(new MapAction<JRootPane>("getRootPane") {
@Override
public JRootPane map() {
return ((JDialog) getSource()).getRootPane();
}
}));
}
static void install( JRootPane rootPane ) {
if( !isSupported() )
return;
// check whether root pane already has a parent, which is the case when switching LaF
if( rootPane.getParent() != null )
return;
// Use hierarchy listener to wait until the root pane is added to a window.
// Enabling JBR decorations must be done very early, probably before
// window becomes displayable (window.isDisplayable()). Tried also using
// "ancestor" property change event on root pane, but this is invoked too late.
HierarchyListener addListener = new HierarchyListener() {
@Override
public void hierarchyChanged( HierarchyEvent e ) {
if( e.getChanged() != rootPane || (e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) == 0 )
return;
Container parent = e.getChangedParent();
if( parent instanceof Window )
install( (Window) parent );
// use invokeLater to remove listener to avoid that listener
// is removed while listener queue is processed
EventQueue.invokeLater( () -> {
rootPane.removeHierarchyListener( this );
} );
}
};
rootPane.addHierarchyListener( addListener );
}
private void removeFloater() {
if (mFloater != null) {
JRootPane rootPane = getRootPane();
Container parent = mFloater.getParent();
Rectangle bounds = mFloater.getBounds();
UIUtilities.convertRectangle(bounds, parent, rootPane);
if (parent != null) {
parent.remove(mFloater);
}
mFloater = null;
if (rootPane != null) {
rootPane.repaint(bounds);
}
}
}
@Override
public void installUI( JComponent c ) {
super.installUI( c );
rootPane = (JRootPane) c;
if( rootPane.getWindowDecorationStyle() != JRootPane.NONE )
installClientDecorations();
if( canUseJBRCustomDecorations )
JBRCustomDecorations.install( rootPane );
}
/**
* Uninstalls the previously installed <code>LayoutManager</code>.
*
* @param root the root
*/
private void uninstallLayout(JRootPane root)
{
if (savedOldLayout != null)
{
root.setLayout(savedOldLayout);
savedOldLayout = null;
}
}
/**
* @return the top level layered pane which contains the owner.
*/
private Container getLayeredPane() {
// The code below is copied from PopupFactory#LightWeightPopup#show()
Container parent = null;
if (owner != null) {
parent = owner instanceof Container ? (Container) owner : owner.getParent();
}
// Try to find a JLayeredPane and Window to add
for (Container p = parent; p != null; p = p.getParent()) {
if (p instanceof JRootPane) {
if (p.getParent() instanceof JInternalFrame) {
continue;
}
parent = ((JRootPane) p).getLayeredPane();
// Continue, so that if there is a higher JRootPane, we'll
// pick it up.
} else if (p instanceof Window) {
if (parent == null) {
parent = p;
}
break;
} else if (p instanceof JApplet) {
// Painting code stops at Applets, we don't want
// to add to a Component above an Applet otherwise
// you'll never see it painted.
break;
}
}
return parent;
}
/**
* Constructs a Swing based input method window.
*/
public InputMethodJFrame(String title, InputContext context) {
super(title);
//InputMethodJFrame never has LookAndFeel decoration
if(JFrame.isDefaultLookAndFeelDecorated())
{
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
}
if (context != null) {
this.inputContext = context;
}
setFocusableWindowState(false);
}
/**
* Constructs a Swing based input method window.
*/
public InputMethodJFrame(String title, InputContext context) {
super(title);
//InputMethodJFrame never has LookAndFeel decoration
if(JFrame.isDefaultLookAndFeelDecorated())
{
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
}
if (context != null) {
this.inputContext = context;
}
setFocusableWindowState(false);
}
/**
* Make the OK button the default. This should be called
* <EM>after</EM> you add this panel to a dialog/frame, not
* before otherwise you will get an <TT>IllegalStateException</TT>
* exception.
*
* @param IllegalStateException
* Thrown if <TT>null</TT> <TT>JRootPane</TT>. I.E. component
* hasn't been added to a frame, dialog etc.
*/
public synchronized void makeOKButtonDefault()
throws IllegalStateException
{
JRootPane root = getRootPane();
if (root == null)
{
throw new IllegalStateException("Null RootPane so cannot set default button");
}
// root.setDefaultButton(_okBtn);
}
public static void installEscapeCloseOperation(final JDialog dialog) {
Action dispatchClosing = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(
dialog, WindowEvent.WINDOW_CLOSING));
}
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
escapeStroke, dispatchWindowClosingActionMapKey);
root.getActionMap().put(dispatchWindowClosingActionMapKey, dispatchClosing);
}
/**
* Constructs a Swing based input method window.
*/
public InputMethodJFrame(String title, InputContext context) {
super(title);
//InputMethodJFrame never has LookAndFeel decoration
if(JFrame.isDefaultLookAndFeelDecorated())
{
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
}
if (context != null) {
this.inputContext = context;
}
setFocusableWindowState(false);
}
@Override
protected JRootPane createRootPane() {
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
JRootPane rootPane = new JRootPane();
rootPane.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
escapeKeyProc();
}
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
return rootPane;
}
@Override
protected JRootPane createRootPane() {
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
JRootPane rootPane = new JRootPane();
rootPane.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
escapeKeyProc();
}
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
return rootPane;
}