SwingSpeciality

来源:互联网 发布:Windows icinga监控 编辑:程序博客网 时间:2024/05/30 02:22
SwingSpecialityDemo.java
001 package cn.rolia.learning.swing;
002 /*
003 此程序演示Swing的特性
004   JComponent类的特殊功能又分为:
005 
006   1) 边框设置:使用setBorder()方法可以设置组件外围的边框,使用一个EmptyBorder对象能在组件周围留出空白。
007 
008 
009   2) 双缓冲区:使用双缓冲技术能改进频繁变化的组件的显示效果。与AWT组件不同,JComponent组件默认双缓冲区,不必自己重写代码。如果想关闭双缓冲区,可以在组件上施加setDoubleBuffered(false)方法。
010 
011   3) 提示信息:使用setTooltipText()方法,为组件设置对用户有帮助的提示信息。
012 
013   4) 键盘导航:使用registerKeyboardAction( ) 方法,能使用户用键盘代替鼠标来驱动组件。JComponent类的子类AbstractButton还提供了便利的方法--用setMnemonic( )方法指明一个字符,通过这个字符和一个当前L&F的特殊修饰共同激活按钮动作。
014 
015   5) 可插入L&F:每个Jcomponent对象有一个相应的ComponentUI对象,为它完成所有的绘画、事件处理、决定尺寸大小等工作。 ComponentUI对象依赖当前使用的L&F,用UIManager.setLookAndFeel( )方法可以设置需要的L&F.
016 
017   6) 支持布局:通过设置组件最大、最小、推荐尺寸的方法和设置X、Y对齐参数值的方法能指定布局管理器的约束条件,为布局提供支持。
018 
019  另外还有菜单的使用方法
020 */
021 import java.awt.*;
022 //import java.io.File;
023 //import java.awt.event.*;
024 import javax.swing.*;
025 //import javax.swing.event.MenuKeyEvent;
026 //import javax.swing.event.MenuKeyListener;
027 
028 public class SwingSpecialityDemo
029 {
030  public static void main(String args[])
031  {
032   try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
033   JFrame fr = new JFrame("BorderLayout");
034   fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
035 
036   JMenuBar bar = new JMenuBar();
037   JMenu m1 = new JMenu("File");
038   JMenu m2 = new JMenu("Edit");
039   JMenuItem m3 = new JMenuItem("Help");
040   JMenuItem mi1 = new JMenuItem("Open");
041   JMenuItem mi2 = new JMenuItem("Close");
042   m1.add(m2);
043   m1.add(m3);
044   m2.add(mi1);
045   m2.add(mi2);
046   bar.add(m1);
047   //bar.add(m2);
048   
049   SwingSpecialityPanel panel = new SwingSpecialityPanel();
050 
051   fr.setJMenuBar(bar);
052   fr.setContentPane(panel);
053   fr.pack();
054   fr.setVisible(true)
055  }
056  /*
057  private class OpenListener implements MenuKeyListener
058  {
059   public void nemuKeyPressed(MenuKeyEvent e)
060   {
061    JFileChooser chooser = new JFileChooser ();
062    int status = chooser.showOpenDialog(null);//
063    if (status != JFileChooser.APPROVE_OPTION)
064    JOptionPane.showMessageDialog (null,"No File Choosen!");
065    else
066    {
067     File file = chooser.getSelectedFile();
068     SwingSpecialityPanel panel = new SwingSpecialityPanel();
069     panel.getCenterButton().setText("File name: " + file.getName());
070    }
071   }
072   public void menuKeyReleased(MenuKeyEvent e){}
073   public void menuKeyTyped(MenuKeyEvent e){}
074  }*/
075 }
076 
077 class SwingSpecialityPanel extends JPanel
078 {
079  /**
080    
081    */
082   private static final long serialVersionUID = 1L;
083 JButton north;
084  JButton east;
085  JButton west;
086  JButton south;
087  JButton center;
088  public SwingSpecialityPanel()
089  {
090   setLayout(new BorderLayout());
091   setPreferredSize(new Dimension(300,300));
092   setBackground(Color.red);
093   
094   north = new JButton("North");
095   north.setMnemonic ('n');//标签中的字符会带有下划线,表明是快捷方式
096         //用户可以在按住Alt的同时按快捷键字符激活此按键
097   
098   south = new JButton("South");
099   south.setToolTipText ("South Button");
100   
101   east = new JButton("East");
102   east.setEnabled(false);//如果有些组件不应被使用,我们可以禁用这些组件
103   
104   west = new JButton("West");
105   west.setDoubleBuffered(false);
106   
107   center = new JButton("Center");
108   center.setBorder (BorderFactory.createTitledBorder ("Title"));
109   
110   add("North", north);//第一个参数表示把按钮添加到容器的North区域
111   add("South", south);
112   add("East", east);
113   add("West", west);
114   add("Center",center);
115  }
116  
117  public JButton getCenterButton()
118  {
119   return center;
120  }
121 }
原创粉丝点击