java程序的界面编程详解

java程序的界面编程详解

在Java中可以为程序自定义程序界面选择Windows、Unix、Java或Macintosh外观。甚至可以在程序运行时让用户自由的选择外观。

UIManager和SwingUtilities类

UIManager类保留了当前外观的信息,而SwingUtilities提供了几种实用的方法,尤其是UIManager类提供的静态方法setLookAndFeel。

一般的“界面”类如下:

javax.swing.plaf.metal.MetalLookAndFeel;

com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

com.sun.java.swing.plaf.motif.MotifLookAndFeel;

 

可以来创建以下界面:

1:Java界面

2:Unix界面

3:与操作系统和谐的界面

4:跨平台的界面

 

定义Java的界面:

try

{

      String style = "javax.swing.plaf.metal.MetalLookAndFeel";

      UIManager.setLookAndFeel(style);

}

catch (Exception e)

{

      System.err.println("Exception: " + e);

}

 

定义与Unix界面:

try

{

      String style = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

      UIManager.setLookAndFeel(style);

}

catch (Exception e)

{

      System.err.println("Exception: " + e);

}

 

定义与操作系统和谐的界面:

try

{

      String style = UIManager.getSystemLookAndFeelClassName();

      UIManager.setLookAndFeel(style);

}

catch (Exception e)

{

      System.err.println("Exception: "  + e);

}

 

定义类似跨平台的界面:

try

{

      String style = UIManager.getCrossPlatformLookAndFeelClassName();

      UIManager.setLookAndFeel(style);

}

catch (Exception e)

{

      System.err.println("Exception: " + e);

}

 

下面是一段完整的换界面的代码:

package WindowStyle; import javax.swing.*; import java.awt.FlowLayout; import java.awt.Container; import java.awt.event.*; public class windowstyle extends JFrame implements ActionListener { private JButton windLook = new JButton("Windows"); private JButton unixLook = new JButton("Unix"); private JButton javaLook = new JButton("Java"); private JLabel label= new JLabel("FlySky Young"); private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent we) { System.exit(0); } } public windowstyle() { super("JFrame with Buttons"); Container content = getContentPane(); content.setLayout(new FlowLayout()); content.add(label); content.add(windLook); content.add(unixLook); content.add(javaLook); //添加按钮监听事件 windLook.addActionListener(this); unixLook.addActionListener(this); javaLook.addActionListener(this); validate(); pack(); setVisible(true); } public void actionPerformed(ActionEvent ae) { String style = "javax.swing.plaf.metal.MetalLookAndFeel"; if (ae.getSource() == windLook) { style = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; } else if (ae.getSource() == unixLook) { style = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; } try { UIManager.setLookAndFeel(style); SwingUtilities.updateComponentTreeUI(this); pack(); } catch (Exception e) { System.err.println("Exception: " + e); } } public static void main(String args[]) { windowstyle style = new windowstyle(); } }

转载于:https://www.cnblogs.com/JPAORM/archive/2010/11/02/2509919.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值