给大家介绍两种很好玩的Look And Feel:<o:p></o:p>
NapkinLookAndFeel和LiquidLookAndFeel.
效果图分别如下
NapkinLookAndFeel
LiquidLookAndFeel
<o:p></o:p>
测试代码如下:
DemoGui.java
import java.awt.BorderLayout;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.sourceforge.napkinlaf.NapkinLookAndFeel;

public class DemoGui extends JFrame ...{
private JPanel pane = new JPanel( new BorderLayout() );
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JButton button;
private JComboBox combox = new JComboBox();
private JCheckBox jcb = new JCheckBox();
private JMenuBar menuBar = new JMenuBar();
private JMenu menu = new JMenu( "File" );
private JMenuItem mi1 = new JMenuItem( "拷贝" );
private JMenuItem mi2 = new JMenuItem( "Cut" );
private JMenuItem mi3 = new JMenuItem( "Paste" );
private JToggleButton jtb = new JToggleButton( "Button1" );
private JToggleButton jtb1 = new JToggleButton( "Button2" );
private JToggleButton jtb2 = new JToggleButton( "Button3" );
private JToggleButton jtb3 = new JToggleButton( "暗流" );

/**//*private JToggleButton jtb = new JToggleButton( "<html><font color='red'>暗流</font></html>" );
private JToggleButton jtb1 = new JToggleButton( "<html><font color='green'>暗流</font></html>" );
private JToggleButton jtb2 = new JToggleButton( "<html><font color='blue'>暗流</font></html>" );
private JToggleButton jtb3 = new JToggleButton( "<html><font color='yellow'>暗流</font></html>" );*/

static ...{

try ...{
UIManager.setLookAndFeel( "com.birosoft.liquid.LiquidLookAndFeel" );
//UIManager.setLookAndFeel( new NapkinLookAndFeel() );
} catch (UnsupportedLookAndFeelException e1) ...{
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DemoGui() ...{
try ...{
button = new JButton( new String( "Button".getBytes( "ISO-8859-1" )) );
} catch (UnsupportedEncodingException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
mi1.setMnemonic( 'd' );
menu.add( mi1 );
menu.add( mi2 );
menu.add( mi3 );
menuBar.add( menu );
menuBar.add( jtb1 );
menuBar.add( jtb2 );
menuBar.add( jtb3 );
panel1.add( button );
panel1.add( jcb );
panel1.add( combox );
panel1.add( jtb );
pane.add( panel1, BorderLayout.PAGE_START );
pane.add( panel2, BorderLayout.PAGE_END );
this.setJMenuBar( menuBar );
this.setContentPane( pane );
//this.setUndecorated( true );
//this.getRootPane().setWindowDecorationStyle( JRootPane.FRAME );
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setBounds( 300, 300, 600, 400 );
this.setVisible( true );
}
public static void main( String[] args ) ...{
new DemoGui();
}
}
大家看到NapkinLookAndFeel那个图里面有乱码,那是因为NapkinLookAndFeel不支持中文,比较遗憾,我猜想很可能是他使用的字体不支持中文。不过有一种很恶心的方式可以让他支持中文:那就是在所有使用中文的地方都采用html,比如:
Private JButton button = new JButton( “<html><font color=’red’>暗流</font></html>” );不过这样button的字体就不是Napkin的风格了,大家可以试下。
还有代码中有这样两行:
this.setUndecorated( true );
this.getRootPane().setWindowDecorationStyle( JRootPane.FRAME );这两行的作用是:不使用默认的decoration,而使用你使用的LookAndFeel类提供的Decoration, 不过不是所有的LookAndFeel都提供decoration, 比如NapkinLookAndFeel就不提供,而LiquidLookAndFeel里面就提供,大家可以把这两行取消注释试一下。
还有一点提醒大家注意,我的测试代码里把
UIManager.setLookAndFeel( "com.birosoft.liquid.LiquidLookAndFeel" );
//UIManager.setLookAndFeel( new NapkinLookAndFeel() );放在static初始化块里面了,这不是必须的,但是你必须保证把这段代码放在你生成所有的Component/Jcomponent之前,否则LookAndFeel不能生效。
参考资料:
Napkin和Liquid都可以在sourceforge上找的到。
本文介绍并演示了NapkinLookAndFeel和LiquidLookAndFeel两种Java界面风格,通过示例代码展示了如何设置这两种风格,并讨论了NapkinLookAndFeel对中文的支持问题。
7765

被折叠的 条评论
为什么被折叠?



