下面列出了javax.swing.JRootPane#FRAME 实例代码,或者点击链接到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();
}
}
/**
* Create the <code>Action</code>s that get associated with the
* buttons and menu items.
*/
private void createActions()
{
closeAction = new CloseAction();
if (getWindowDecorationStyle() == JRootPane.FRAME)
{
iconifyAction = new IconifyAction();
restoreAction = new RestoreAction();
maximizeAction = new MaximizeAction();
setupAction = new AbstractAction(//"Setup ")
UIManager.getString("BETitlePane.setupButtonText",getLocale()))
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(rootPane, "This button just used for demo." +
"In the future,you can customize it.\n" +
"Now, you can set UIManager.put(\"RootPane.setupButtonVisible\", false) to hide it(detault is true).\n" +
"BeautyEye L&F developed by Jack Jiang, you can mail with [email protected]");
}
};
}
}
/**
* Compute height.
*
* @return the int
*/
private int computeHeight()
{
FontMetrics fm = rootPane.getFontMetrics(getFont());
int fontHeight = fm.getHeight();
fontHeight += 7;
int iconHeight = 0;
if (getWindowDecorationStyle() == JRootPane.FRAME)
// ||getWindowDecorationStyle() == JRootPane.PLAIN_DIALOG)//
{
iconHeight = IMAGE_HEIGHT;
}
int finalHeight = Math.max(fontHeight, iconHeight);
return finalHeight+2;//* 改变此处的返回结果将直接改变窗口标题的高度哦
}
/**
* Returns the <code>JMenu</code> displaying the appropriate menu items
* for manipulating the Frame.
*
* @return the j menu
*/
private JMenu createMenu()
{
JMenu menu = new JMenu("");
// menu.setRolloverEnabled(false);//本行一定要!这是Java 1.5之Metal主题的Bug! -- jack,2009-09-11
menu.setOpaque(false);//本行一定要,否则将导致窗口图标区会绘制Menu的背景!这是Java Metal主题的Bug! -- jack,2009-09-11
if (getWindowDecorationStyle() == JRootPane.FRAME
||getWindowDecorationStyle() == JRootPane.PLAIN_DIALOG//现在也给dialog加上菜单项(但只有关闭项)
)
{
addMenuItems(menu);
}
return menu;
}
/**
* Updates state dependant upon the Window's active state.
*
* @param isActive the new active
*/
private void setActive(boolean isActive)
{
Boolean activeB = isActive ? Boolean.TRUE : Boolean.FALSE;
closeButton.putClientProperty("paintActive", activeB);
if (getWindowDecorationStyle() == JRootPane.FRAME)
{
iconifyButton.putClientProperty("paintActive", activeB);
toggleButton.putClientProperty("paintActive", activeB);
}
// Repaint the whole thing as the Borders that are used have
// different colors for active vs inactive
getRootPane().repaint();
}
/**
* <p>安装放大、缩小、关闭按钮<p>
*
* <p>Installation Form button<p>
*/
protected void installBtn()
{
closeBtn = new JButton();
closeBtn.addMouseListener(new CloseMouseAdapter(closeBtn,
LuckRootPaneUIBundle.CLOSE_NORMAL_ICON,
LuckRootPaneUIBundle.CLOSE_ROVER_ICON,
LuckRootPaneUIBundle.CLOSE_PRESSED_ICON));
setBtnAtrr(closeBtn);
add(closeBtn);
// 如果是Frame则显示退出和放大按钮
// If the Frame is displayed exits and zoom buttons
if(initStyle == JRootPane.FRAME)
{
minBtn = new JButton();
minBtn.addMouseListener(new MinMouseAdapter(minBtn,
LuckRootPaneUIBundle.MIN_NORMAL_ICON,
LuckRootPaneUIBundle.MIN_ROVER_ICON,
LuckRootPaneUIBundle.MIN_PRESSED_ICON));
setBtnAtrr(minBtn);
add(minBtn);
maximizeBtn = new JButton();
maximizeBtn.addMouseListener(new MaximizeMouseAdapter(maximizeBtn,
LuckRootPaneUIBundle.MAX_NORMAL_ICON,
LuckRootPaneUIBundle.MAX_ROVER_ICON,
LuckRootPaneUIBundle.MAX_PRESSED_ICON));
setBtnAtrr(maximizeBtn);
add(maximizeBtn);
// 判断窗体是否可以拉伸
// Determine whether the form can be stretched
if(!isResizeableOnInit)
{
maximizeBtn.setVisible(false);
}
}
}
/**
* Adds any sub-Components contained in the <code>MetalTitlePane</code>.
*/
private void installSubcomponents()
{
int decorationStyle = getWindowDecorationStyle();
if (decorationStyle == JRootPane.FRAME||decorationStyle == JRootPane.PLAIN_DIALOG)
{
createActions();
menuBar = createMenuBar();
add(menuBar);
createButtons();
add(closeButton);
//~* RootPane.setupButtonVisible是jb2011自定义的属性哦,目的是控制这个当前用于演示的设置按钮的可见性
Object isSetupButtonVisibleObj = UIManager.get("RootPane.setupButtonVisible");
//如果开发者没有设置此属性则默认认为是true(即显示该按钮)
boolean isSetupButtonVisible = (isSetupButtonVisibleObj==null?true:(Boolean)isSetupButtonVisibleObj);
if(isSetupButtonVisible)
//加入设置按钮
add(setupButton);
if(decorationStyle!=JRootPane.PLAIN_DIALOG)//!
{
add(iconifyButton);
add(toggleButton);
menuBar.setEnabled(false);
}
}
else if
(
// decorationStyle == JRootPane.PLAIN_DIALOG
// ||
decorationStyle == JRootPane.INFORMATION_DIALOG
|| decorationStyle == JRootPane.ERROR_DIALOG
|| decorationStyle == JRootPane.COLOR_CHOOSER_DIALOG
|| decorationStyle == JRootPane.FILE_CHOOSER_DIALOG
|| decorationStyle == JRootPane.QUESTION_DIALOG
|| decorationStyle == JRootPane.WARNING_DIALOG)
{
createActions();
createButtons();
add(closeButton);
}
}
/**
* Determines the Colors to draw with.
*/
private void determineColors()
{
switch (getWindowDecorationStyle())
{
case JRootPane.FRAME:
activeBackground = UIManager.getColor("activeCaption");
activeForeground = UIManager.getColor("activeCaptionText");
activeShadow = UIManager.getColor("activeCaptionBorder");
break;
case JRootPane.ERROR_DIALOG:
activeBackground = UIManager
.getColor("OptionPane.errorDialog.titlePane.background");
activeForeground = UIManager
.getColor("OptionPane.errorDialog.titlePane.foreground");
activeShadow = UIManager
.getColor("OptionPane.errorDialog.titlePane.shadow");
break;
case JRootPane.QUESTION_DIALOG:
case JRootPane.COLOR_CHOOSER_DIALOG:
case JRootPane.FILE_CHOOSER_DIALOG:
activeBackground = UIManager
.getColor("OptionPane.questionDialog.titlePane.background");
activeForeground = UIManager
.getColor("OptionPane.questionDialog.titlePane.foreground");
activeShadow = UIManager
.getColor("OptionPane.questionDialog.titlePane.shadow");
break;
case JRootPane.WARNING_DIALOG:
activeBackground = UIManager
.getColor("OptionPane.warningDialog.titlePane.background");
activeForeground = UIManager
.getColor("OptionPane.warningDialog.titlePane.foreground");
activeShadow = UIManager
.getColor("OptionPane.warningDialog.titlePane.shadow");
break;
case JRootPane.PLAIN_DIALOG:
case JRootPane.INFORMATION_DIALOG:
default:
activeBackground = UIManager.getColor("activeCaption");
activeForeground = UIManager.getColor("activeCaptionText");
activeShadow = UIManager.getColor("activeCaptionBorder");
break;
}
}
/**
* Adds the necessary <code>JMenuItem</code>s to the passed in menu.
*
* @param menu the menu
*/
private void addMenuItems(JMenu menu)
{
Locale locale = getRootPane().getLocale();
menu.setToolTipText(//"窗口相关操作.");
UIManager.getString("BETitlePane.titleMenuToolTipText",getLocale()));
JMenuItem mi;
int mnemonic;
if(getWindowDecorationStyle() == JRootPane.FRAME)//! 只有frame才有这些菜单项
{
mi = menu.add(restoreAction);
mnemonic = BEUtils.getInt("MetalTitlePane.restoreMnemonic",-1);
if (mnemonic != -1)
{
mi.setMnemonic(mnemonic);
}
mi = menu.add(iconifyAction);
mnemonic = BEUtils.getInt("MetalTitlePane.iconifyMnemonic", -1);
if (mnemonic != -1)
{
mi.setMnemonic(mnemonic);
}
if (Toolkit.getDefaultToolkit().isFrameStateSupported(
Frame.MAXIMIZED_BOTH))
{
mi = menu.add(maximizeAction);
mnemonic = BEUtils.getInt("MetalTitlePane.maximizeMnemonic",
-1);
if (mnemonic != -1)
{
mi.setMnemonic(mnemonic);
}
}
menu.add(new JSeparator());
}
mi = menu.add(closeAction);
mnemonic = BEUtils.getInt("MetalTitlePane.closeMnemonic", -1);
if (mnemonic != -1)
{
mi.setMnemonic(mnemonic);
}
}
/**
* Creates the Buttons that will be placed on the TitlePane.
*/
private void createButtons()
{
setupButton = createTitleButton();
setupButton.setAction(setupAction);
// setupButton.setText("设置 ");
// setupButton.putClientProperty("paintActive", Boolean.TRUE);
setupButton.setBorder(handyEmptyBorder);
// setupButton.getAccessibleContext().setAccessibleName("Close");
setupButton.setIcon(UIManager.getIcon("Frame.setupIcon"));
setButtonIcon(setupButton, setupButton.getIcon());// @since 3.5:同时设置rover和pressed时的icon效果
setupButton.setContentAreaFilled(false);
// setupButton.setToolTipText("设置");
closeButton = createTitleButton();
closeButton.setAction(closeAction);
closeButton.setText(null);
closeButton.putClientProperty("paintActive", Boolean.TRUE);
closeButton.setBorder(handyEmptyBorder);
closeButton.getAccessibleContext().setAccessibleName("Close");
closeButton.setIcon(UIManager.getIcon("Frame.closeIcon"));
// setButtonIcon(closeButton, closeButton.getIcon());// @since 3.5:同时设置rover和pressed时的icon效果
closeButton.setRolloverIcon(UIManager.getIcon("Frame.closeIcon_rover"));
closeButton.setPressedIcon(UIManager.getIcon("Frame.closeIcon_pressed"));
closeButton.setContentAreaFilled(false);
closeButton.setToolTipText(//"关闭");
UIManager.getString("BETitlePane.closeButtonToolTipext",getLocale()));
if (getWindowDecorationStyle() == JRootPane.FRAME)
{
maximizeIcon = UIManager.getIcon("Frame.maximizeIcon");
maximizeIcon_rover = UIManager.getIcon("Frame.maximizeIcon_rover");
maximizeIcon_pressed = UIManager.getIcon("Frame.maximizeIcon_pressed");
minimizeIcon = UIManager.getIcon("Frame.minimizeIcon");
minimizeIcon_rover = UIManager.getIcon("Frame.minimizeIcon_rover");
minimizeIcon_pressed = UIManager.getIcon("Frame.minimizeIcon_pressed");
iconifyButton = createTitleButton();
iconifyButton.setAction(iconifyAction);
iconifyButton.setText(null);
iconifyButton.putClientProperty("paintActive", Boolean.TRUE);
iconifyButton.setBorder(handyEmptyBorder);
iconifyButton.getAccessibleContext().setAccessibleName("Iconify");
iconifyButton.setIcon(UIManager.getIcon("Frame.iconifyIcon"));
// setButtonIcon(iconifyButton, iconifyButton.getIcon());// @since 3.5:同时设置rover和pressed时的icon效果
iconifyButton.setRolloverIcon(UIManager.getIcon("Frame.iconifyIcon_rover"));
iconifyButton.setPressedIcon(UIManager.getIcon("Frame.iconifyIcon_pressed"));
iconifyButton.setContentAreaFilled(false);
iconifyButton.setToolTipText(//"最小化");
UIManager.getString("BETitlePane.iconifyButtonToolTipText",getLocale()));
toggleButton = createTitleButton();
toggleButton.setAction(restoreAction);
toggleButton.putClientProperty("paintActive", Boolean.TRUE);
toggleButton.setBorder(handyEmptyBorder);
toggleButton.getAccessibleContext().setAccessibleName("Maximize");
toggleButton.setIcon(maximizeIcon);
// setButtonIcon(toggleButton, toggleButton.getIcon());// @since 3.5:同时设置rover和pressed时的icon效果
toggleButton.setRolloverIcon(maximizeIcon_rover);
toggleButton.setPressedIcon(maximizeIcon_pressed);
toggleButton.setContentAreaFilled(false);
toggleButton.setToolTipText(//"最大化");
UIManager.getString("BETitlePane.toggleButtonToolTipText",getLocale()));
}
}
/**
* Sets the state of the window. If <code>updateRegardless</code> is
* true and the state has not changed, this will update anyway.
*
* @param state the state
* @param updateRegardless the update regardless
*/
private void setState(int state, boolean updateRegardless)
{
Window w = getWindow();
if (w != null && getWindowDecorationStyle() == JRootPane.FRAME)
{
if (this.state == state && !updateRegardless)
{
return;
}
Frame frame = getFrame();
if (frame != null)
{
JRootPane rootPane = getRootPane();
if (((state & Frame.MAXIMIZED_BOTH) != 0)
&& (rootPane.getBorder() == null || (rootPane
.getBorder() instanceof UIResource))
&& frame.isShowing())
{
rootPane.setBorder(null);
}
else if ((state & Frame.MAXIMIZED_BOTH) == 0)
{
// This is a croak, if state becomes bound, this can
// be nuked.
rootPaneUI.installBorder(rootPane);
}
if (frame.isResizable())
{
if ((state & Frame.MAXIMIZED_BOTH) != 0)
{
updateToggleButton(restoreAction, minimizeIcon, minimizeIcon_rover, minimizeIcon_pressed);
maximizeAction.setEnabled(false);
restoreAction.setEnabled(true);
}
else
{
updateToggleButton(maximizeAction, maximizeIcon, maximizeIcon_rover, maximizeIcon_pressed);
maximizeAction.setEnabled(true);
restoreAction.setEnabled(false);
}
if (toggleButton.getParent() == null
|| iconifyButton.getParent() == null)
{
add(toggleButton);
add(iconifyButton);
revalidate();
repaint();
}
toggleButton.setText(null);
}
else
{
maximizeAction.setEnabled(false);
restoreAction.setEnabled(false);
if (toggleButton.getParent() != null)
{
remove(toggleButton);
revalidate();
repaint();
}
}
}
else
{
// Not contained in a Frame
maximizeAction.setEnabled(false);
restoreAction.setEnabled(false);
iconifyAction.setEnabled(false);
remove(toggleButton);
remove(iconifyButton);
revalidate();
repaint();
}
closeAction.setEnabled(true);
this.state = state;
}
}
/**
* Renders the TitlePane.
*
* @param g the g
*/
public void paintComponent(Graphics g)
{
// As state isn't bound, we need a convenience place to check
// if it has changed. Changing the state typically changes the
if (getFrame() != null)
{
setState(getFrame().getExtendedState());
}
JRootPane rootPane = getRootPane();
Window window = getWindow();
boolean leftToRight = (window == null) ? rootPane
.getComponentOrientation().isLeftToRight() : window
.getComponentOrientation().isLeftToRight();
boolean isSelected = (window == null) ? true : window.isActive();
int width = getWidth();
int height = getHeight();
Color background;
Color foreground;
Color darkShadow;
if (isSelected)
{
background = activeBackground;
foreground = activeForeground;
darkShadow = activeShadow;
}
else
{
background = inactiveBackground;
foreground = inactiveForeground;
darkShadow = inactiveShadow;
// bumps = inactiveBumps;
}
//----------------------------------------------- 标题背景
paintTitlePane(g,0,0,width,height,isSelected);
//----------------------------------------------- 标题文字和图片
int xOffset = leftToRight ? 5 : width - 5;
if (getWindowDecorationStyle() == JRootPane.FRAME||getWindowDecorationStyle() ==JRootPane.PLAIN_DIALOG)
{
xOffset += leftToRight ? IMAGE_WIDTH + 5 : -IMAGE_WIDTH - 5;
}
String theTitle = getTitle();
if (theTitle != null)
{
FontMetrics fm = MySwingUtilities2.getFontMetrics(rootPane, g);
int yOffset = ((height - fm.getHeight()) / 2) + fm.getAscent();
Rectangle rect = new Rectangle(0, 0, 0, 0);
if (iconifyButton != null && iconifyButton.getParent() != null)
{
rect = iconifyButton.getBounds();
}
int titleW;
if (leftToRight)
{
if (rect.x == 0)
{
rect.x = window.getWidth() - window.getInsets().right - 2;
}
titleW = rect.x - xOffset - 4;
theTitle = MySwingUtilities2.clipStringIfNecessary(rootPane, fm,
theTitle, titleW);
}
else
{
titleW = xOffset - rect.x - rect.width - 4;
theTitle = MySwingUtilities2.clipStringIfNecessary(rootPane, fm,
theTitle, titleW);
xOffset -= MySwingUtilities2.stringWidth(rootPane, fm, theTitle);
}
int titleLength = MySwingUtilities2.stringWidth(rootPane, fm,theTitle);
g.setColor(foreground);
MySwingUtilities2.drawString(rootPane, g, theTitle, xOffset, yOffset);
xOffset += leftToRight ? titleLength + 5 : -5;
}
}
public void layoutContainer(Container c)
{
boolean leftToRight = (window == null) ? getRootPane()
.getComponentOrientation().isLeftToRight() : window
.getComponentOrientation().isLeftToRight();
int w = getWidth();
int x;
int y = 3;
int spacing;
int buttonHeight;
int buttonWidth;
if (closeButton != null && closeButton.getIcon() != null)
{
buttonHeight = closeButton.getIcon().getIconHeight();
buttonWidth = closeButton.getIcon().getIconWidth();
}
else
{
buttonHeight = IMAGE_HEIGHT;
buttonWidth = IMAGE_WIDTH;
}
// assumes all buttons have the same dimensions
// these dimensions include the borders
x = leftToRight ? w : 0;
spacing = 5;
x = leftToRight ? spacing : w - buttonWidth - spacing;
if (menuBar != null)
{
//* js 2010-03-30
//* 原MetalTitledPane的Bug:当存在关闭按钮时,窗口图标的大小是已关闭按钮的大小来决定的,这样不合理
menuBar.setBounds(x
, y + 2//+2偏移量由Jack Jiang于2012-09-24日加上,目的是使得图标与文本保持在同一中心位置
// TODO 目前BueautyEye和MetalLookAndFeel的标题图标位置算法都有优化的余地:y轴坐标自动按title高度居中会
// 适用更多场景,现在的算法如果title高度变的很大,则这些位置都是固定。不过按MetalLNF的思路,这些高度
// 是与整体美感一样,不应被随意改动的,也可以说不需要优化,目前就这么的吧,没有关系。
, IMAGE_HEIGHT,IMAGE_WIDTH);//buttonWidth, buttonHeight);
}
x = leftToRight ? w : 0;
spacing = 4;
x += leftToRight ? -spacing - buttonWidth : spacing;
if (closeButton != null)
{
closeButton.setBounds(x, y, buttonWidth, buttonHeight);
}
if (!leftToRight)
x += buttonWidth;
if (getWindowDecorationStyle() == JRootPane.FRAME)
{
if (Toolkit.getDefaultToolkit().isFrameStateSupported(
Frame.MAXIMIZED_BOTH))
{
if (toggleButton.getParent() != null)
{
spacing = 2;//10!!!
x += leftToRight ? -spacing - buttonWidth : spacing;
toggleButton.setBounds(x, y, buttonWidth, buttonHeight);
if (!leftToRight)
{
x += buttonWidth;
}
}
}
if (iconifyButton != null && iconifyButton.getParent() != null)
{
spacing = 2;
x += leftToRight ? -spacing - buttonWidth : spacing;
iconifyButton.setBounds(x, y, buttonWidth, buttonHeight);
if (!leftToRight)
{
x += buttonWidth;
}
}
//“设置”按钮
if(setupButton != null )
{
spacing = 2;
int stringWidth = BEUtils.getStrPixWidth(setupButton.getFont(), setupButton.getText());
x += leftToRight ? -spacing - buttonWidth - stringWidth : spacing;
setupButton.setBounds(x, y, buttonWidth + stringWidth, buttonHeight);
if (!leftToRight)
{
x += buttonWidth;
}
}
}
buttonsWidth = leftToRight ? w - x : x;
}