Swing Hacks 学习笔记

Chapter 1. Basic JComponents

Hack 11. Create Menus with Drop Shadows

Most custom effects require either subclassing a component or messing with graphics overlays. I tried a variety of techniques to create this hack, but I kept coming across the same problem over and over. If I wanted to draw a shadow, I had to change the sizing of each menu item, plus its background, plus the pop-up frame itself. That is a lot of components to manage. It would be a lot simpler if I could tell the components to make themselves a little bit bigger and give me the extra slice of screen real estate to draw in. The solution was right under my nose: the border. Every Swing component can use a custom border, without subclassing, and the border will automatically resize the component to fit. If the border is lopsided, then it will create a kind of shadow effect. Perfect!

Every standard Swing component is actually drawn by a UI helper class, and pop-up menus are no exception. I took the BasicPopupMenuUI in the javax. swing.plaf.basic package and created a subclass called CustomPopupMenuUI (shown in Example 1-25). It only does two things special: adds a custom border to the pop up's parent panel and sets the panel to be transparent.

Note: 

1.when or where we should add the custom border to the parent of the JPopupMenu?

In the getPopup() method of the subclass of BasicPopupMenuUI.

2.The comment of

public static ComponentUI createUI(JComponent c){

throw new Error("ComponentUI.createUI not implemented.");

}

in ComponentUI.java :

Returns an instance of the UI delegate for the specified component. Each subclass must provide its own static createUI method that returns an instance of that UI delegate subclass. If the UI delegate subclass is stateless, it may return an instance that is shared by multiple components. If the UI delegate is stateful, then it should return a new instance per component. The default implementation of this method throws an error, as it should never be invoked.

(this method will be invoked by java reflect system)

Java编程语言一般是和重要的企业级应用程序联系在一起的,运行它们的服务器可能主位于蒙大拿的某个冰冷的机房里。不过,是桌面应用催生了Java,未来也必将是桌面应让Java能更加光彩夺目。通过AWT和现在的Swing,Java为编写图形化用户界面提供了丰富的客户端API。不过想要让Java、AWT和Swing达到最佳性能并不容易(也不方便),特别是在模拟交互式Web站点或像Windows XP和Mac OS X这样的操作系统外观时更是如此。\r\n 本书能帮助JAVA开发人员跳过Swing的基础节直接让桌面应用程序贴近现实。如果读者是一个Java开人员,并想开发带有一流外观的企业级应用程序,那就应该掌握Swing。不过对Swing的hack并不仅仅局限于对它的正常使用,而是关于一些读者或许要经过多年才能领会到的技巧。它们是一些富有创造性的、原创的甚至是诡异的hack,能让读者发出由衷的惊叹:“我可没想过居然能用Swing干这个!”\r\n 在本书中,读者将学到:\r\n 过滤不用的列表控件,对用户的输入做出合适的反应;\r\n 为列表和组合框组件提供拖放功能;\r\n 在图形组件之间实现动态效果,并能创建动态列表式界面;\r\n 控制任何方面,从鼠标指针到光标,乃至闪动键盘灯;\r\n 在Swing中构建分层、覆盖组件并灵活使用透明窗格屏蔽Swing接口内部的复杂细节和伸缩性。 ================================== 对学习SWING很有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值