[Core Java. Volume I. Fundamentals, 8th Edition]-7,8

Swing与AWT

From now on, we say “Swing” when we mean the “painted” user interface classes, and we say“AWT”(Abstract Window Toolkit) when we mean the underlying mechanisms of the windowing toolkit, such as event handling. 


获取系统可用字体

 String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 
 for (String fontName : fontNames) 
      System.out.println(fontName); 
} 


窗体大小的设置

 If your frame contains only standard components such as buttons and text fields,  you can simply call thepack method to set the frame size. The frame will be set to the  smallest size that contains all components. It is quite common to set the main frame of a program to themaximum size. As of Java SE 1.4, you can simply maximize a  frame by calling 

                        frame.setExtendedState(Frame.MAXIMIZED_BOTH); 


关于重绘
force repainting of the screen, call the repaint method instead of  paintComponent. The repaint method will cause paintComponent to be called for all components, with a properly configured Graphics object。

浮点数的表示

float f = 1.2; // Error 
float f = 1.2F; //Ok 


读取图片文件

                      String filename = "..."; 
                      Image image = ImageIO.read(new File(filename)); 


                      String urlname = "..."; 
                      Image image = ImageIO.read(new URL(urlname)); 

                         g.drawImage(image, x, y, null); 


-----------第八章-------------------

给按钮加监听
you can use an anonymous inner class: 

                     loadButton.addActionListener(new ActionListener() 
                        { 
                           public void actionPerformed(ActionEvent event) 
                           { 
                              frame.loadData(); 
                           } 
                        }); 
       But the EventHandler class can create such a listener automatically, with the call (好混乱,还是上面那个好)
                     EventHandler.create(ActionListener.class, frame, "loadData") 
action为接口而不是类



给面板添加键盘事件

                     InputMap imap = panel.getInputMap(JComponent.WHEN_FOCUSED); 
                     imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow"); 
                     ActionMap amap = panel.getActionMap(); 
                     amap.put("panel.yellow", yellowAction); 

                  It is customary to use the string "none" for a do-nothing action. That makes it easy to  deactivate a key

                     imap.put(KeyStroke.getKeyStroke("ctrl C"), "none"); 

完整示例1-3楼

更改系统鼠标形状


                      public void mouseMoved(MouseEvent event) 
                      { 
                         if (find(event.getPoint()) == null) 
                            setCursor(Cursor.getDefaultCursor()); 
                         else 
                            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 
                      } 


更改自定义鼠标形状

 NOTE: You can also define your own cursor types through the use of the createCustomCursor  method in the Toolkit class: 

                             Toolkit tk = Toolkit.getDefaultToolkit(); 
                              Image img = tk.getImage("dynamite.gif"); 
                              Cursor dynamiteCursor = tk.createCustomCursor(img, new Point(10, 10), "dynamite stick"); 

The first parameter of the createCustomCursor points to the cursor image. The second parameter gives the offset of the “hot spot” of the cursor. The third parameter is a string that describes the cursor. This string can be used for accessibility support. For example, a screen reader program can read the cursor shape description to a user who is visually impaired or who simply is not facing the screen.


 Mouse Events 
 You do not need to handle mouse events explicitly if you just want the user to be able to click on a button or menu. These mouse operations are handled internally by the various components in the user interface. However, if you want to enable the user to draw with the mouse, you will need to trap mouse move, click, and drag events. 


键盘+鼠标

You use bit masks to test which modifiers have been set. In the original API, two of the button masks equal two keyboard modifier masks, namely 

                     if ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0) 
                        . . . // code for right click 


                     BUTTON1_DOWN_MASK (鼠标左键)
                     BUTTON2_DOWN_MASK (键)
                     BUTTON3_DOWN_MASK (键)
                     SHIFT_DOWN_MASK 
                     CTRL_DOWN_MASK 
                     ALT_DOWN_MASK 
                     ALT_GRAPH_DOWN_MASK 
                     META_DOWN_MASK 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值