首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Root Pane Container(4)

2012-09-06 
Root Pane Container(四)表8-12中列出了JInternalFrame的可用UIResource相关属性的集合。对于JInternalFram

Root Pane Container(四)

Root Pane Container(4)

Root Pane Container(4)

表8-12中列出了JInternalFrame的可用UIResource相关属性的集合。对于JInternalFrame常量,有60个不同的属性,包括内部窗体的标题面板的属性。

JInternalFrame UIResource元素

?

属性字符串

对象类型

InternalFrame.actionMap

ActionMap

InternalFrame.activeBroderColor

Color

InternalFrame.activeTitleBackground

Color

InternaleFrame.activeTitleForeground

Color

InternalFrame.activeTitleGradient

List

InternalFrame.border

Border

InternalFrame.borderColor

Color

InternalFrame.borderDarkShadow

Color

InternalFrame.borderHighlight

Color

InterenalFrame.borderLight

Color

InternaleFrame.borderShadow

Color

InternaleFrame.borderWidth

Integer

InternalFrame.closeButtonToolTip

String

InternalFrame.closeIcon

Icon

InternalFrmae.closeSound

String

InternalFrame.icon

Icon

InternalFrame.iconButtonToolTip

String

InternalFrame.iconifyIcon

Icon

InternalFrame.inactiveBorderColor

Color

InternalFrame.inactiveTitleBackground

Color

InternalFrame.inactiveTitleForeground

Color

InternalFrame.inactiveTitleGradient

List

InternalFrame.layoutTitlePaneAtOrigin

Boolean

InternalFrame.maxButtonToolTip

String

InternalFrame.maximizeIcon

Icon

InternalFrame.maximizeSound

String

InternalFrame.minimizeIcon

Icon

InternalFrame.minimizeIconBackground

Color

InternalFrame.minimizeSound

String

InternalFrame.optionDialogBorder

Border

InternalFrame.paletteBorder

Border

InternalFrame.paletteCloseIcon

Icon

InternalFrame.paletteTitleHeight

Integer

InternaleFrame.resizeIconHighlight

Color

InternalFrame.resizeIconShadow

Color

InternalFrame.restoreButtonToolTip

String

InternalFrame.restoreDownSound

String

InternalFrame.restoreUpSound

String

InternalFrame.titlebuttonHeight

Integer

InternalFrame.titleButtonWidth

Integer

InternalFrame.titleFont

Font

InternalFrame.titlePaneHeight

Integer

InternalFrame.useTaskBar

Boolean

InternalFrame.windowBindings

Object[]

InternalFrameTitlePane.closebuttonAccessibleName

String

InternalFrameTitlePane.closebuttonText

String

InternalFrameTitlePane.closeIcon

Icon

InternalFrameTitlePane.iconifyButtonAccessibleName

String

InternalFrameTitlePane.iconifyIcon

Icon

InternalFrameTitlePane.maximizeButtonAccessiblName

String

InternalFrameTitlePane.maximizeButtonText

String

InternalFrameTitlePane.minimizeIcon

Icon

InternalFrameTitlePane.moveButtonText

String

InternalFrameTitlePane.restoreButtonText

String

InternalFrameTitlePane.sizeButtonText

String

InternalFrameTitlePane.titlePaneLayout

LayoutManager

InternalFrameTitlePaneUI

String

InternalFrameUI

String

除了表8-12中许多可配置属性以外,对于Metal观感,我们还可以通过特殊的客户端属性JInternalFrame.isPalette来将内部窗体设计为一个palette。当设置为Boolean.TRUE时,内部窗体的外观会与其他窗体略微不同,并且具有较短的标题栏,如图8-6所示。

Root Pane Container(4)

如果我们同时在桌面的PALETTE_LAYER上添加了一个内部窗体,则这个窗体会位于其他所有窗体之上(如图8-6所示):

JInternalFrame palette = new JInternalFrame("Palette", true, false, true, false); palette.setBounds(150, 0, 100, 100); palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); desktop.add(palette, JDesktopPane.PALETTE_LAYER);

?

创建图8-6所示的程序的完整代码显示在本章稍后的列表8-6中。

修改JDesktopIcon

JInternalFrame依赖一个内联类JDesktopIcon来为JInternalFrame的图标化显示提供UI委托。这个类只是用来这种功能的一个特殊的JComponent,而不是如其名字暗示的一个特殊的Icon实现。事实上,JDesktopIcon类的注释表明这个类是临时的,所以我们不应直接对其进行自定义。(当然,这个类会存在一段时间。)

如果我们需要自定义JDesktopIcon,我们可以修改一些UIResource相关的属性。表8-13列出了JDesktopIcon组件的八个UIResource相关属性。

JInternalFrame.JDesktopIcon UIResource元素

属性字符串

对象类型

DesktopIcon.background

Color

DesktopIcon.border

Border

DesktopIcon.font

Font

DesktopIcon.foreground

Color

DesktopIcon.icon

Icon

DesktopIcon.width

Integer

DesktopIcon.windowBindings

Object[]

DesktopIconUI

String

8.6.2 JDesktopPane类

与内部窗体组合配合使用的另一个类就是JDesktopPane类。桌面面板的目的就是包含内部窗体集合。当内部窗体被包含在一个桌面面板中时,他们将其行为的大部分委托给桌面面板的桌面管理器。我们将会在本章稍后详细了解DesktopManager接口。

创建JDesktopPane

JDesktopPane只有一个无参数的构造函数。一旦创建,我们通常将其放在由BorderLayout管理的容器的中部。这可以保证桌面占据容器的所有空间。

将内部窗体添加到JDesktopPane

JDesktopPane并没有实现RootPaneContainer。我们并不能直接将组件添加到JRootPane内的不同面板中,而是直接将其添加到JDesktopPane:

desktop.add(anInternalFrame);

JDesktopPane属性

如表8-14所示,JDesktopPane有八个属性。位于allFrames属性数组索引0外的JInternalFrame是位于桌面前面的内部窗体(JInternalFrame f = desktop.getAllFrames()[0])。除了获取JDesktopPane中的所有窗体以外,我们还可以仅获取特定层的窗体:public JInternalFrame[] getAllFramesInLayer(int layer)。

可用的dragMode属性设置可以为类的LIVE_DRAG_MODE与OUTLINE_DRAG_MODE常量。

JDesktopPane属性

属性名
数据类型

访问性

accessibleContext
AccessibleContext

只读

allFrames
JInternalFrame[]

只读

desktopManager
DesktopManager

读写

dragMode
int

读写绑定

opaque
boolean

只读

selectedFrame
JInternalFrame

读写

UI
DesktopPaneUI

读写

UIClassID
String

只读

自定义JDesktopPane观感

回到图8-5,我们可以看到JDesktopPane中的JInternalFrame对象。JDesktopPane的基本观感与每一个观感相同。如表8-15所示,对JDesktopPane并没有太多可以配置的UIResource相关属性。

JDesktopPane UIResource元素

属性字符串

对象类型

desktop

Color

Desktop.ancestorInputMap

InputMap

Desktop.background

Color

Desktop.windowBindings

Object[]

DesktopPane.actionMap

ActionMap

DesktopPaneUI

String

完整的桌面示例

现在我们已经了解了主要的桌面相关类,现在我们来看一下完整的示例。基本的过程包括创建一组JInternalFrame对象,然后将放在一个JDesktopPane中。如果需要,可以对每一个内部窗体的单个组件进行事件处理,也可以对单个窗体进行事件处理。在这个示例中简单的使用了前面的列表8-5中所给出的InternalFrameIconifyListener类来监听正在图标化和取消图标化的内容窗体。

图8-6显示了程序启动时的样子。一个特定的内部窗体被设计为palette,并且允许了拖放模式。

列表8-6显示了这个示例的完整代码。

package swingstudy.ch08; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.event.InternalFrameListener; public class DesktopSample { /** * @param args */ public static void main(final String[] args) { // TODO Auto-generated method stub Runnable runner = new Runnable() { public void run() { String title = (args.length==0 ? "Desktop Sample" : args[0]); JFrame frame = new JFrame(title); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JDesktopPane desktop = new JDesktopPane(); JInternalFrame internalFrames[] = { new JInternalFrame("Can Do All", true, true, true, true), new JInternalFrame("Not Resizable", false, true, true, true), new JInternalFrame("Not Closable", true, false, true, true), new JInternalFrame("Not Maximizable", true, true, false, true), new JInternalFrame("Not Iconifiable", true, true, true, false) }; InternalFrameListener internalFrameListener = new InternalFrameIconifyListener(); int pos = 0; for(JInternalFrame internalFrame: internalFrames) { // Add to desktop desktop.add(internalFrame); // Position and size internalFrame.setBounds(pos*25, pos*25, 200, 100); pos++; // Add listener for iconification events internalFrame.addInternalFrameListener(internalFrameListener); JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER); internalFrame.add(label, BorderLayout.CENTER); // Make visible internalFrame.setVisible(true); } JInternalFrame palette = new JInternalFrame("Palette", true, false, true, false); palette.setBounds(350, 150, 100, 100); palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); desktop.add(palette, JDesktopPane.PALETTE_LAYER); palette.setVisible(true); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); frame.add(desktop, BorderLayout.CENTER); frame.setSize(500, 300); frame.setVisible(true); } }; EventQueue.invokeLater(runner); } }

?

DesktopManager接口

使用桌面的最后一部分就是桌面管理器了,他是DesktopManager接口的实现,其定义如下:

?

当JInternalFrame位于JDe

public interface DesktopManager { public void activateFrame(JInternalFrame frame); public void beginDraggingFrame(JComponent frame); public void beginResizingFrame(JComponent frame, int direction); public void closeFrame(JInternalFrame frame); public void deactivateFrame(JInternalFrame frame); public void deiconifyFrame(JInternalFrame frame); public void dragFrame(JComponent frame, int newX, int newY); public void endDraggingFrame(JComponent frame); public void endResizingFrame(JComponent frame); public void iconifyFrame(JInternalFrame frame); public void maximizeFrame(JInternalFrame frame); public void minimizeFrame(JInternalFrame frame); public void openFrame(JInternalFrame frame); public void resizeFrame(JComponent frame, int newX, int newY, int newWidth, int newHeight); public void setBoundsForFrame(JComponent frame, int newX, int newY, int newWidth, int newHeight); }

?

sktopPane中时,他们不应尝试例如图标化或是最大化的操作。相反,他们应该请求他们所安装在的桌面面板的桌面管理器来执行这些操作:

getDesktopPane().getDesktopManager().iconifyFrame(anInternalFrame);

?

DefaultDesktopManager类提供了DesktopManager的一个实现。如果默认实现还足够,观感会提供他们自己的DesktopManager实现类,例如Windows观感的WindowsDesktopManager。我们也可以定义自己的管理器,但是通常并不需要这样。

8.7 小结

在本章中,我们探讨了JRootPane类,以及如何实现依据JRootPane对内部组件进行管理的RootPaneContainer接口。我们同时了解了在Swing中我们如何使用JFrame, JDialog, JWindow, JApplet或是JInternalFrame类的JRootPane。根面板可以借助JLayeredPane来布局组件,其方式是工具提示文本以及弹出菜单总是显示在相关联组件的上面。

JInternalFrame同时也可以存在于桌面环境中,在这种情况下,JDesktopPane以及DesktopManager管理如何以及在哪里放置并显示内部窗体。我们还可以通过将InternalFrameListener实现也JInternalFrame关联来响应内部窗体事件。

在第9章中,我们将会探讨Swing库中的特定弹出组件:JColorChooser, JFileChooser, JOptionPane以及ProgressMonitor

热点排行