下面列出了javax.swing.JRootPane#getJMenuBar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Dimension computeLayoutSize( Container parent, Function<Component, Dimension> getSizeFunc ) {
JRootPane rootPane = (JRootPane) parent;
Dimension titlePaneSize = (titlePane != null)
? getSizeFunc.apply( titlePane )
: new Dimension();
Dimension contentSize = (rootPane.getContentPane() != null)
? getSizeFunc.apply( rootPane.getContentPane() )
: rootPane.getSize();
int width = Math.max( titlePaneSize.width, contentSize.width );
int height = titlePaneSize.height + contentSize.height;
if( titlePane == null || !titlePane.isMenuBarEmbedded() ) {
Dimension menuBarSize = (rootPane.getJMenuBar() != null)
? getSizeFunc.apply( rootPane.getJMenuBar() )
: new Dimension();
width = Math.max( width, menuBarSize.width );
height += menuBarSize.height;
}
Insets insets = rootPane.getInsets();
return new Dimension(
width + insets.left + insets.right,
height + insets.top + insets.bottom );
}
public void layoutContainer(Container parent)
{
JRootPane root = (JRootPane) parent;
Rectangle bound = root.getBounds();
Insets inset = root.getInsets();
// 获取内容面板实际宽度, 减去左右边框面积
// Calculate the actual width
int w = bound.width - inset.right - inset.left;
// 获取内容面板实际高度, 减去上下边框面积
// Calculate the actual height
int h = bound.height - inset.top - inset.bottom;
// 设置层级面板在根窗格中的位置
// layout LayeredPane
if(root.getLayeredPane() != null)
{
root.getLayeredPane().setBounds(inset.left, inset.top, w, h);
}
// 玻璃窗格是在层级面板中,所以坐标从(0, 0)开始
// layout GlassPane
if(root.getGlassPane() != null)
{
root.getGlassPane().setBounds(inset.left, inset.top, w, h);
}
int nextY = 0;
RootPaneUI rootPaneUI = root.getUI();
if(rootPaneUI instanceof LuckMetalRootPaneUI)
{
// 布局标题面板
// layout TitlePane
Component titlePanel = ((LuckMetalRootPaneUI)rootPaneUI).getTitlePane();
// 如果未取消窗体装饰
if (titlePanel != null)
{
titlePanel.setBounds(0, nextY, w, titlePanel.getHeight());
nextY += titlePanel.getHeight();
}
}
// 布局JMenuBar
// layout JMenuBar
JMenuBar menuBar = root.getJMenuBar();
if(menuBar != null && menuBar.isVisible())
{
menuBar.setBounds(0, nextY, w, menuBar.getPreferredSize().height);
nextY += menuBar.getPreferredSize().getHeight();
}
// 布局内容面板
// layout ContentPane
root.getContentPane().setBounds(0, nextY, w, h - nextY);
}
/**
* Returns the amount of space the layout would like to have.
*
* @param parent the Container for which this layout manager is being
* used
*
* @return a Dimension object containing the layout's preferred size
*/
public Dimension preferredLayoutSize(Container parent) {
Dimension cpd;
Dimension mbd;
Dimension tpd;
int cpWidth = 0;
int cpHeight = 0;
int mbWidth = 0;
int mbHeight = 0;
int tpWidth = 0;
int tpHeight = 0;
Insets i = parent.getInsets();
JRootPane root = (JRootPane) parent;
if (root.getContentPane() != null) {
cpd = root.getContentPane().getPreferredSize();
} else {
cpd = root.getSize();
}
if (cpd != null) {
cpWidth = cpd.width;
cpHeight = cpd.height;
}
if (root.getJMenuBar() != null) {
mbd = root.getJMenuBar().getPreferredSize();
if (mbd != null) {
mbWidth = mbd.width;
mbHeight = mbd.height;
}
}
if (root.getWindowDecorationStyle() != JRootPane.NONE && (root.getUI() instanceof SeaGlassRootPaneUI)) {
JComponent titlePane = ((SeaGlassRootPaneUI) root.getUI()).getTitlePane();
if (titlePane != null) {
tpd = titlePane.getPreferredSize();
if (tpd != null) {
tpWidth = tpd.width;
tpHeight = tpd.height;
}
}
}
return new Dimension(Math.max(Math.max(cpWidth, mbWidth), tpWidth) + i.left + i.right,
cpHeight + mbHeight + tpHeight + i.top + i.bottom);
}
/**
* Returns the minimum amount of space the layout needs.
*
* @param parent the Container for which this layout manager is being
* used
*
* @return a Dimension object containing the layout's minimum size
*/
public Dimension minimumLayoutSize(Container parent) {
Dimension cpd;
Dimension mbd;
Dimension tpd;
int cpWidth = 0;
int cpHeight = 0;
int mbWidth = 0;
int mbHeight = 0;
int tpWidth = 0;
int tpHeight = 0;
Insets i = parent.getInsets();
JRootPane root = (JRootPane) parent;
if (root.getContentPane() != null) {
cpd = root.getContentPane().getMinimumSize();
} else {
cpd = root.getSize();
}
if (cpd != null) {
cpWidth = cpd.width;
cpHeight = cpd.height;
}
if (root.getJMenuBar() != null) {
mbd = root.getJMenuBar().getMinimumSize();
if (mbd != null) {
mbWidth = mbd.width;
mbHeight = mbd.height;
}
}
if (root.getWindowDecorationStyle() != JRootPane.NONE && (root.getUI() instanceof SeaGlassRootPaneUI)) {
JComponent titlePane = ((SeaGlassRootPaneUI) root.getUI()).getTitlePane();
if (titlePane != null) {
tpd = titlePane.getMinimumSize();
if (tpd != null) {
tpWidth = tpd.width;
tpHeight = tpd.height;
}
}
}
return new Dimension(Math.max(Math.max(cpWidth, mbWidth), tpWidth) + i.left + i.right,
cpHeight + mbHeight + tpHeight + i.top + i.bottom);
}
/**
* Returns the maximum amount of space the layout can use.
*
* @param target the Container for which this layout manager is being
* used
*
* @return a Dimension object containing the layout's maximum size
*/
public Dimension maximumLayoutSize(Container target) {
Dimension cpd;
Dimension mbd;
Dimension tpd;
int cpWidth = Integer.MAX_VALUE;
int cpHeight = Integer.MAX_VALUE;
int mbWidth = Integer.MAX_VALUE;
int mbHeight = Integer.MAX_VALUE;
int tpWidth = Integer.MAX_VALUE;
int tpHeight = Integer.MAX_VALUE;
Insets i = target.getInsets();
JRootPane root = (JRootPane) target;
if (root.getContentPane() != null) {
cpd = root.getContentPane().getMaximumSize();
if (cpd != null) {
cpWidth = cpd.width;
cpHeight = cpd.height;
}
}
if (root.getJMenuBar() != null) {
mbd = root.getJMenuBar().getMaximumSize();
if (mbd != null) {
mbWidth = mbd.width;
mbHeight = mbd.height;
}
}
if (root.getWindowDecorationStyle() != JRootPane.NONE && (root.getUI() instanceof SeaGlassRootPaneUI)) {
JComponent titlePane = ((SeaGlassRootPaneUI) root.getUI()).getTitlePane();
if (titlePane != null) {
tpd = titlePane.getMaximumSize();
if (tpd != null) {
tpWidth = tpd.width;
tpHeight = tpd.height;
}
}
}
int maxHeight = Math.max(Math.max(cpHeight, mbHeight), tpHeight);
// Only overflows if 3 real non-MAX_VALUE heights, sum to > MAX_VALUE
// Only will happen if sums to more than 2 billion units. Not likely.
if (maxHeight != Integer.MAX_VALUE) {
maxHeight = cpHeight + mbHeight + tpHeight + i.top + i.bottom;
}
int maxWidth = Math.max(Math.max(cpWidth, mbWidth), tpWidth);
// Similar overflow comment as above
if (maxWidth != Integer.MAX_VALUE) {
maxWidth += i.left + i.right;
}
return new Dimension(maxWidth, maxHeight);
}
/**
* Instructs the layout manager to perform the layout for the specified
* container.
*
* @param parent the Container for which this layout manager is being
* used
*/
public void layoutContainer(Container parent) {
JRootPane root = (JRootPane) parent;
Rectangle b = root.getBounds();
Insets i = root.getInsets();
int nextY = 0;
int w = b.width - i.right - i.left;
int h = b.height - i.top - i.bottom;
if (root.getLayeredPane() != null) {
root.getLayeredPane().setBounds(i.left, i.top, w, h);
}
if (root.getGlassPane() != null) {
root.getGlassPane().setBounds(i.left, i.top, w, h);
}
// Note: This is laying out the children in the layeredPane,
// technically, these are not our children.
if (root.getWindowDecorationStyle() != JRootPane.NONE && (root.getUI() instanceof SeaGlassRootPaneUI)) {
JComponent titlePane = ((SeaGlassRootPaneUI) root.getUI()).getTitlePane();
if (titlePane != null) {
Dimension tpd = titlePane.getPreferredSize();
if (tpd != null) {
int tpHeight = tpd.height;
titlePane.setBounds(0, 0, w, tpHeight);
nextY += tpHeight;
}
}
}
if (root.getJMenuBar() != null) {
boolean menuInTitle = (root.getClientProperty("JRootPane.MenuInTitle") == Boolean.TRUE);
Dimension mbd = root.getJMenuBar().getPreferredSize();
int x = menuInTitle? 20 : 0;
root.getJMenuBar().setBounds(x, menuInTitle ? 0 : nextY, w, mbd.height);
root.getJMenuBar().setOpaque(false);
root.getJMenuBar().setBackground(transparentColor);
if (!menuInTitle) {
nextY += mbd.height;
}
}
if (root.getContentPane() != null) {
/* Dimension cpd = */ root.getContentPane().getPreferredSize();
root.getContentPane().setBounds(0, nextY, w, h < nextY ? 0 : h - nextY);
}
}
/**
* {@inheritDoc}
*/
protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) {
if (state == Which.BACKGROUND_ENABLED_NOFRAME) {
return;
}
Shape s = shapeGenerator.createRoundRectangle(0, 0, (width - 1), (height - 1), CornerSize.FRAME_BORDER,
CornerStyle.ROUNDED, CornerStyle.SQUARE, CornerStyle.SQUARE, CornerStyle.ROUNDED);
g.setPaint(getFrameBorderPaint(s));
g.draw(s);
JMenuBar mb = null;
Component[] cArray = null;
if (c instanceof JInternalFrame) {
JInternalFrame iframe = (JInternalFrame) c;
mb = iframe.getJMenuBar();
cArray = iframe.getContentPane().getComponents();
} else if (c instanceof JRootPane) {
JRootPane root = (JRootPane) c;
mb = root.getJMenuBar();
cArray = root.getContentPane().getComponents();
}
int topToolBarHeight = 0;
int bottomToolBarHeight = 0;
if (cArray != null) {
for (Component comp : cArray) {
if (comp instanceof JToolBar) {
if (toolBarNorthState.isInState((JComponent) comp)) {
topToolBarHeight = comp.getHeight();
} else if (toolBarSouthState.isInState((JComponent) comp)) {
bottomToolBarHeight = comp.getHeight();
}
}
}
}
int titleHeight = TITLE_BAR_HEIGHT;
if (mb != null && c.getClientProperty("SeaGlass.JRootPane.MenuInTitle") == Boolean.TRUE) {
titleHeight += mb.getHeight();
}
if (c.getClientProperty(SeaGlassRootPaneUI.UNIFIED_TOOLBAR_LOOK) == Boolean.TRUE) {
// Draw background gradient.
s = shapeGenerator.createRoundRectangle(1, 1, width - 2, height - 2, CornerSize.FRAME_INNER_HIGHLIGHT,
CornerStyle.ROUNDED, CornerStyle.SQUARE, CornerStyle.SQUARE, CornerStyle.ROUNDED);
g.setPaint(getFrameInteriorPaint(s, titleHeight, topToolBarHeight, bottomToolBarHeight));
g.fill(s);
} else {
// Paint title bar.
s = shapeGenerator.createRoundRectangle(1, 1, width - 2, titleHeight, CornerSize.FRAME_INNER_HIGHLIGHT,
CornerStyle.ROUNDED, CornerStyle.SQUARE, CornerStyle.SQUARE, CornerStyle.ROUNDED);
g.setPaint(getTitleBarInteriorPaint(s, titleHeight));
g.fill(s);
// Paint contents.
s = shapeGenerator.createRoundRectangle(1, titleHeight, width - 2, height - titleHeight - 1, CornerSize.FRAME_INNER_HIGHLIGHT,
CornerStyle.SQUARE, CornerStyle.SQUARE, CornerStyle.SQUARE, CornerStyle.SQUARE);
g.setPaint(c.getBackground());
g.fill(s);
// Draw separator line.
g.setPaint(decodeColor("seaGlassMenuIcon"));
g.drawLine(1, titleHeight, width - 2, titleHeight);
}
s = shapeGenerator.createRoundRectangle(1, 1, width - 3, height - 3, CornerSize.FRAME_INTERIOR);
g.setPaint(getFrameInnerHighlightPaint(s));
g.draw(s);
}