动态地改变程序的look and feel

上传人:子 文档编号:41783163 上传时间:2018-05-31 格式:DOC 页数:7 大小:31.50KB
返回 下载 相关 举报
动态地改变程序的look and feel_第1页
第1页 / 共7页
动态地改变程序的look and feel_第2页
第2页 / 共7页
动态地改变程序的look and feel_第3页
第3页 / 共7页
动态地改变程序的look and feel_第4页
第4页 / 共7页
动态地改变程序的look and feel_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《动态地改变程序的look and feel》由会员分享,可在线阅读,更多相关《动态地改变程序的look and feel(7页珍藏版)》请在金锄头文库上搜索。

1、动态地改变程序的动态地改变程序的 LookLook andand FeelFeel动态地改变程序的 Look and FeelUIManager.setLookAndFeel(“javax.swing.plaf.metal.MetalLookAndFeel“);SwingUtilities.updateComponentTreeUI(this); /对由this 所指示的组件重新设置外观注意:由于 JFrame、JApplet 等为重量级组件,因此它的外观只与操作系统平台有关系,在相同的操作系统平台下表现出相同的外观。1、Metal 风格 (默认)String lookAndFeel = “j

2、avax.swing.plaf.metal.MetalLookAndFeel“;UIManager.setLookAndFeel(lookAndFeel);2、Windows 风格String lookAndFeel = “com.sun.java.swing.plaf.windows.WindowsLookAndFeel“;UIManager.setLookAndFeel(lookAndFeel);3、Windows Classic 风格String lookAndFeel = “com.sun.java.swing.plaf.windows.WindowsClassicLookAndFee

3、l“;UIManager.setLookAndFeel(lookAndFeel);4、Motif 风格String lookAndFeel = “com.sun.java.swing.plaf.motif.MotifLookAndFeel“;UIManager.setLookAndFeel(lookAndFeel);5、Mac 风格 (需要在相关的操作系统上方可实现)String lookAndFeel = “com.sun.java.swing.plaf.mac.MacLookAndFeel“;UIManager.setLookAndFeel(lookAndFeel);6、GTK 风格 (需

4、要在相关的操作系统上方可实现)String lookAndFeel = “com.sun.java.swing.plaf.gtk.GTKLookAndFeel“;UIManager.setLookAndFeel(lookAndFeel);7、可跨平台的默认风格String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();UIManager.setLookAndFeel(lookAndFeel);8、当前系统的风格String lookAndFeel = UIManager.getSystemLookAndFeelCl

5、assName();UIManager.setLookAndFeel(lookAndFeel);在 Java 中让用户能够动态地更改应用的外观,可以给用户更好地体验,具体的实现方式是:1,先使用 UIManager.setLookAndFeel(String s)方法设定对应的外观2,再使用SwingUtilities.updateComponentTreeUI(Component c)方法立刻更新应用的外观这两个类均在 javax.swing 包中样例:import java.awt.*;import java.awt.event.*;import javax.swing.*;public

6、class LookAndFeelDemo extends JFrame JPanel contentPane = new JPanel();JPanel jPanel1 = new JPanel();JRadioButton jRadioButton1 = new JRadioButton();JRadioButton jRadioButton2 = new JRadioButton();JRadioButton jRadioButton3 = new JRadioButton();JRadioButton jRadioButton4 = new JRadioButton();JRadioB

7、utton jRadioButton5 = new JRadioButton();static LookAndFeelDemo frame;public LookAndFeelDemo() try jbInit();catch(Exception e) public static void main(String args) /Windows 风格的窗口try UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel“); catch (Exception e) frame=new LookAndF

8、eelDemo();frame.setVisible(true);/窗口关闭时清空内存protected void processWindowEvent(WindowEvent e) if (e.getID() = WindowEvent.WINDOW_CLOSING) System.exit(0);private void jbInit() throws Exception contentPane=(JPanel)this.getContentPane();/设置窗口的标题,大小与布局setTitle(“窗口风格设置程序演示“);setSize(new Dimension(400,300);

9、contentPane.setLayout(null);/设置放置单选框容器的布局,位置,大小jPanel1.setLayout(new BoxLayout(jPanel1,BoxLayout.Y_AXIS);jPanel1.setBounds(new Rectangle(10, 10, 380, 280);/分别设置单选框的标题与 ActionCommand 属性(在动作事件中获取该变量)jRadioButton1.setText(“Windows 风格的窗口“);jRadioButton1.setActionCommand(“com.sun.java.swing.plaf.windows.

10、WindowsLookAndFeel“); /Windows 风格,只能在 Windows 系统下使用 jRadioButton2.setText(“跨平台风格的窗口“);jRadioButton2.setActionCommand(“UIManager.getCrossPlatformLookAndFeelClassName()“); / 跨平台风格,此时显示模式不要加引号jRadioButton3.setText(“Java 风格的窗口“);jRadioButton3.setActionCommand(“javax.swing.plaf.metal.MetalLookAndFeel“);

11、/java 平台默认外观,金属质感,可用在所有平台jRadioButton4.setText(“CDE/Motif 风格的窗口“);jRadioButton4.setActionCommand(“com.sun.java.swing.plaf.motif.MotifLookAndFeel“); /Motif风格,仿 UNIX 外观,可用在所有平台jRadioButton5.setText(“苹果机风格的窗口“);jRadioButton5.setActionCommand(“javax.swing.plaf.mac.MacLookAndFeel“); /Macintosh 风格,只能在 Mac

12、intosh 操作系统下使用/创建单选框接收器RadioListener radioListener=new RadioListener();/分别为各个单选框加入动作接收器jRadioButton1.addActionListener(radioListener);jRadioButton2.addActionListener(radioListener);jRadioButton3.addActionListener(radioListener);jRadioButton4.addActionListener(radioListener);jRadioButton5.addActionLi

13、stener(radioListener);/将单选框放入同一个容器内ButtonGroup group = new ButtonGroup();group.add(jRadioButton1);group.add(jRadioButton2);group.add(jRadioButton3);group.add(jRadioButton4);group.add(jRadioButton5);/向容器加入各种控件jPanel1.add(jRadioButton1, null);jPanel1.add(jRadioButton2, null);jPanel1.add(jRadioButton3,

14、 null);jPanel1.add(jRadioButton4, null);jPanel1.add(jRadioButton5, null);contentPane.add(jPanel1, null);/单选框的动作事件处理类class RadioListener implements ActionListener public void actionPerformed(ActionEvent e) String lnfName=e.getActionCommand();/重新设置平台的显示模式tryUIManager.setLookAndFeel(lnfName);catch(Exception el)/更新窗口的风格SwingUtilities.updateComponentTreeUI(frame);

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号