中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 测试
SwingX: Translucent Panels 101
作者:未知 时间:2005-08-10 22:32 出处:Java频道 责编:chinaitpower
              摘要:SwingX: Translucent Panels 101
SwingX is a new Java.net project that has been making some noise for a.) what it will be providing in terms of features, and b.) it's lofty goals in terms of implementation timelines. For more information, see the SwingX project home page . SwingX is a branch off of the JDNC project .

Now, I realize, if you looked at the website above in a timely basis from when I wrote this tip, you're probably surprised that I am writing a tutorial about a project that - as of yet - doesn't have any releases. Hey, what can I say, I'm a sucker for bleeding edge - and some of this stuff is really cool.

Ok, so I certainly can't cover everything from SwingX because a.) a lot of it isn't done and b.) there is a lot of stuff! Today, however, I want to cover just one component - JXPanel - that, for the most part, is complete. Besides, if they hold true to their schedule, their first release will be in late June which, scary as it is, is just around the corner.

To start working with SwingX, just visit the CVS Access Options page (requires login), and download the source. Then, bundle it into your classpath as you see fit. Enough with the configuration unpleasantries, let's get on to the JXPanel.

JXPanel is an extension of the javax.swing.JPanel that only does one thing - it adds translucent rendering. Long story short, that means that a JXPanel (and any components in a JXPanel) will render based on an 'alpha-channel' setting, which is essentially a percentage of how opaque things should be.

JXPanels are just like JPanels, but have the one additional property - the 'alpha' property - so really, the usage of a JXPanel is very unextrordinary. That being said, you do have to be somewhat creative to come up with a valid use case for utilizing these panels. There are many classes that extend the JXPanel that add further functionality, but most of those aren't quite done yet - so we'll get to those when they are available.

Here is what JXPanel configuration code would look like:

// JPanel code.
JPanel panel = new JPanel();
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
 
// JXPanel code.
JXPanel xpanel = new JXPanel();
xpanel.add(new JButton("Button 1"));
xpanel.add(new JButton("Button 2"));
xpanel.setAlpha(.5f); // 50% opaque. .35 would be 35%, .75 would be 75%, etc.

The most common use of the JXPanel will most likely be with a JLayeredPane. Because of that, I have implemented a simple class using the JLayeredPane container, that renders buttons on multiple layers. Here is a screen-shot using Java 5's Ocean UI ( note that I am pressing down the bottom button to show that components do still work ):

... and here is the same screenshot using the JGoodies Plastic Look and Feel ( note in this case I am mousing over the 2nd-from-the-bottom button, again, to show things still work ).

Here is my test class in total:

package com.javalobby.tnt.swingx;
 
import java.awt.*;
 
import javax.swing.*;
 
import org.jdesktop.swingx.JXPanel;
 
import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
 
public class JXPanelTest extends JPanel {
	private static final int NUM_LAYERS = 5;
	private JLayeredPane layeredPane;
 
	public JXPanelTest() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		// Create and set up the layered pane.
		layeredPane = new JLayeredPane();
		layeredPane.setPreferredSize(new Dimension(300, 310));
		// This is the origin of the first label added.
		Point origin = new Point(10, 20);
 
		// This is the offset for computing the origin for the next label.
		int offset = 35;
 
		// Add several overlapping panels to the layered pane
		// using absolute positioning/sizing.
		for (int i = 0; i < NUM_LAYERS; i++) {
			JComponent component = createColoredBox(i,
					origin);
			layeredPane.add(component, new Integer(i));
			origin.x += offset;
			origin.y += offset;
		}
		add(layeredPane);
	}
 
	private JComponent createColoredBox(int layer, Point origin) {
			
		JXPanel panel = new JXPanel();
		panel.setLayout(new GridLayout());
		panel.setAlpha(.5f);
		panel.add(new JButton("Button on Layer: " + layer));
		panel.setBounds(origin.x, origin.y, 140, 140);
		return panel;
	}
 
	/**
	 * Create the GUI and show it. For thread safety, this method should be
	 * invoked from the event-dispatching thread.
	 */
	private static void createAndShowGUI() {
		try {
			UIManager.setLookAndFeel(PlasticXPLookAndFeel.class.getName());
		}
		catch(Exception e) { }
		// Make sure we have nice window decorations.
		JFrame.setDefaultLookAndFeelDecorated(true);
 
		// Create and set up the window.
		JFrame frame = new JFrame("JXPanelTest");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		// Create and set up the content pane.
		JComponent newContentPane = new JXPanelTest();
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);
 
		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}
 
	public static void main(String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}

Until next time,

R.J. Lorimer

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有