正文

SwingSpeciality用法2005-09-22 17:22:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/hurongliang/5112.html

分享到:

/*此程序演示Swing的特性  JComponent类的特殊功能又分为:   1) 边框设置:使用setBorder()方法可以设置组件外围的边框,使用一个EmptyBorder对象能在组件周围留出空白。   2) 双缓冲区:使用双缓冲技术能改进频繁变化的组件的显示效果。与AWT组件不同,JComponent组件默认双缓冲区,不必自己重写代码。如果想关闭双缓冲区,可以在组件上施加setDoubleBuffered(false)方法。   3) 提示信息:使用setTooltipText()方法,为组件设置对用户有帮助的提示信息。   4) 键盘导航:使用registerKeyboardAction( ) 方法,能使用户用键盘代替鼠标来驱动组件。JComponent类的子类AbstractButton还提供了便利的方法--用setMnemonic( )方法指明一个字符,通过这个字符和一个当前L&F的特殊修饰共同激活按钮动作。   5) 可插入L&F:每个Jcomponent对象有一个相应的ComponentUI对象,为它完成所有的绘画、事件处理、决定尺寸大小等工作。 ComponentUI对象依赖当前使用的L&F,用UIManager.setLookAndFeel( )方法可以设置需要的L&F.   6) 支持布局:通过设置组件最大、最小、推荐尺寸的方法和设置X、Y对齐参数值的方法能指定布局管理器的约束条件,为布局提供支持。  另外还有菜单的使用方法*/import java.awt.*;import java.io.File;import java.awt.event.*;import javax.swing.*;import javax.swing.event.MenuKeyEvent;import javax.swing.event.MenuKeyListener; public class SwingSpecialityDemo{ public static void main(String args[]) {  try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}  JFrame fr = new JFrame("BorderLayout");  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   JMenuBar bar = new JMenuBar();  JMenu m1 = new JMenu("File");  JMenu m2 = new JMenu("Edit");  JMenuItem m3 = new JMenuItem("Help");  JMenuItem mi1 = new JMenuItem("Open");  JMenuItem mi2 = new JMenuItem("Close");  m1.add(m2);  m1.add(m3);  m2.add(mi1);  m2.add(mi2);  bar.add(m1);  //bar.add(m2);    SwingSpecialityPanel panel = new SwingSpecialityPanel();   fr.setJMenuBar(bar);  fr.setContentPane(panel);  fr.pack();  fr.show();  } /* private class OpenListener implements MenuKeyListener {  public void nemuKeyPressed(MenuKeyEvent e)  {   JFileChooser chooser = new JFileChooser ();   int status = chooser.showOpenDialog(null);//   if (status != JFileChooser.APPROVE_OPTION)   JOptionPane.showMessageDialog (null,"No File Choosen!");   else   {    File file = chooser.getSelectedFile();    SwingSpecialityPanel panel = new SwingSpecialityPanel();    panel.getCenterButton().setText("File name: " + file.getName());   }  }  public void menuKeyReleased(MenuKeyEvent e){}  public void menuKeyTyped(MenuKeyEvent e){} }*/} class SwingSpecialityPanel extends JPanel{ JButton north, east, west, south,center; public SwingSpecialityPanel() {  setLayout(new BorderLayout());  setPreferredSize(new Dimension(300,300));  setBackground(Color.red);    north = new JButton("North");  north.setMnemonic ('n');//标签中的字符会带有下划线,表明是快捷方式        //用户可以在按住Alt的同时按快捷键字符激活此按键    south = new JButton("South");  south.setToolTipText ("South Button");    east = new JButton("East");  east.setEnabled(false);//如果有些组件不应被使用,我们可以禁用这些组件    west = new JButton("West");  west.setDoubleBuffered(false);    center = new JButton("Center");  center.setBorder (BorderFactory.createTitledBorder ("Title"));    add("North", north);//第一个参数表示把按钮添加到容器的North区域  add("South", south);  add("East", east);  add("West", west);  add("Center",center); }  public JButton getCenterButton() {  return center; }}

阅读(3955) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册