控件位置可以配置的Swing桌面
用過Wordpress或者Joomla的朋友一定對(duì)他們的靈活的頁面布局印象深刻。在Joomla中,你可以將一個(gè)控件,放在頁面的任何一個(gè)位置,例如:左邊,右邊,菜單,底部等等。
所以我也嘗試著在Swing桌面上實(shí)現(xiàn)類似的功能,思考以后發(fā)現(xiàn)其實(shí)swing實(shí)現(xiàn)這種功能比利用html頁面實(shí)現(xiàn)該功能還簡(jiǎn)單。
首先我們定義一個(gè)位置接口,實(shí)現(xiàn)該接口的類就標(biāo)示它的位置
- public interface Arrange {
- public String getComponentName();
- }
第二:繼承Arrange 接口,定義不用的位置接口,分別有
- public interface ArrangeBottom extends Arrange
- public interface ArrangeLeft extends Arrange
- public interface ArrangeLogo extends Arrange
- public interface ArrangeMainBottom extends Arrange
- public interface ArrangeMenuBar extends Arrange
- public interface ArrangeRight extends Arrange
- public interface ArrangeToolBar extends Arrange
上面的接口標(biāo)示的位置見下圖:
第三:我們寫一個(gè)面板,實(shí)現(xiàn)位置接口
例如:public class ZPanel extends JPanel implements ArrangeRight
- protected void paintComponent(Graphics g) {
- if (null != wallpaper) {
- processBackground(g);
- }
- System.out.println("f:paintComponent(Graphics g)");
- }
- public void setBackground() {
- wallpaper = new javax.swing.ImageIcon(getClass()
- .getResource("/com/peraglobal/workspace/initcompt/picpanel/kutoku.jpg"));
- this.repaint();
- }
第四:在配置文件中配置這個(gè)類
打開配置文件按zlconfig.xml
編寫 <bean class="com.peraglobal.workspace.initcompt.picpanel.ZPanel" ></bean>
第五:加載配置文件
我們利用Spring將配置文件中實(shí)現(xiàn)了某一接口的類全部拿出,并且加載到指定的位置:
- Map<?, ?> lefts = context.getBeansOfType(ArrangeLeft.class);
- ArrangeLeft agLeft;
- leftPanel.setLayout(new BorderLayout());
- JTabbedPane tabLeft = new JTabbedPane();
- leftPanel.add(tabLeft);
- Iterator<?> it = lefts.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
- Object key = entry.getKey();
- Class<?> newClass = Class.forName((String) key);
- agLeft = (ArrangeLeft) newClass.newInstance();
- tabLeft.add((Component) agLeft, agLeft.getComponentName());
- }
第六:我們看到我們剛才寫的一個(gè)Panel已經(jīng)加載到了右邊的面板中
原文鏈接:http://javapub.iteye.com/blog/865475
【編輯推薦】