ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
±±¾©Ê¥Ë¼Ô°¿Æ¼¼ÓÐÏÞ¹«Ë¾
http://www.shengsiyuan.com
±¾½²ÄÚÈÝ£º

¶ÔÓÚSwing

Swing×é¼þºÍÈÝÆ÷
1. Swing:
? Êǵڶþ´úGUI¿ª·¢¹¤¾ß
? Ëü½¨Á¢ÔÚAWTÖ®ÉÏ£¬µ«ÓÃа汾µÄ×é¼þÌæ
  ´úÁ˾ɰ汾µÄ×é¼þ¡£
? ËüÌṩÁËÐí¶àеÄ×é¼þºÍÏà¹ØµÄAPI
2. The Swing API ·¢²¼ÔÚ:
  ¨C JDK 1.2
  ¨C JFC 1.1 (for use with JDK 1.1)

3. The Swing API ¹¦ÄÜÊÇÇ¿´óµÄ£¬Áé»îµÄºÍ¹ã
  ·ºµÄ¡£ÀýÈç JFC 1.1 ÓÐ 15 public
  packages ¡£
 ÎÒÃǾ­³£ÓõÄÓÐÁ½¸ö°ü£º
  ¨C javax.swing
  ¨C javax.swing.event

  ×¢£ºAWT components ÔÚ java.awt package ÖÐ
1   ¸ÅÀÀ
2   Swing Components ·ÖÀà
3   ¶¥²ãÈÝÆ÷
4   JFrame
5   JDialog
6   JPanel
7   ´´½¨²Ëµ¥
8   JComponent Class
1. ¸ÅÀÀ£º




SwingApplication ´´½¨ÁËËĸö³£ÓÃµÄ Swing components:
? a frame, or main window (JFrame)
? a panel (JPanel)
? a button (JButton)
? a label (JLabel)
ÈÝÆ÷Óë×é¼þ°üº¬¼Ì³Ð¹Øϵͼ±í£º
Your Application
                                Swing
  AWT
                                          AWT
Component                                             Java 2D
   s        Window   Dialog    Frame
                                          Event

Button                                              Drag and Drop
Frame
ScrollBar    Font    Color    Graphics   Tool Kit
                                                    Accessibility
¡­


                         AWT

                         JFC
JButton
                                               JCheckBoxMenultem
             AbstractButton
                               JMenultem             JMenu
                                              JRadioButtonMnultem

                                                  JCheckBox
             JComboBox        JToggleButton
                                                 JRadioButton
                 JLabel
                  JList
JComponent
               JMenuBar
                JPanel
             JPopupMenu
               JScrollBar
                                JEditorPane       JTextPane
              JScrollPane
                                 JTextArea
             JTextComponent
                                 JTextField     JPasswordField
JColorChooser
               JFileChooser
              JInternalFrame
              JLayeredPane         JDesktopPane
               JOptionPane
               JProgressBar
                JRootPane          JPopupMenuSeparator
JComponent      JSeparator
                   JºÝºÝߣr           JToolBarSeparator
                JSplitPane
               JTabbedPane
                   JTable
                 JToolBar
                  JToolTip
                    JTree
                 JViewport

             JIternalFrame.JDesktopIcon
2 Swing Components ·ÖÀࣺ
? ¶¥²ãÈÝÆ÷
? ÖмäÈÝÆ÷
? Ô­×Ó×é¼þ
¶¥²ãÈÝÆ÷
java ÌṩÁËÈý¸ö¶¥²ãÈÝÆ÷£ºJFrame, JDialog, ºÍ
  JApplet¡£
¶¥²ãÈÝÆ÷

Ìص㣺
? ÏÔʾÔÚÆÁÄ»ÉϵÄÿ¸ö×é¼þ¶¼±ØÐëÔÚÒ»¸ö°üº¬¼Ì
  ³ÐÖС£Ã¿Ò»¸ö°üº¬¼Ì³Ð¶¼ÓÐÒ»¸ö¶¥²ãÈÝÆ÷×÷Ϊ
  ËüµÄ¸ù¡£

? ÿһ¸ö¶¥²ãÈÝÆ÷¶¼ÓÐÒ»¸öcontent pane £¬Ëü
  °üº¬Á˶¥²ãÈÝÆ÷ÖеÄËùÓÐ×é¼þ

? ²Ëµ¥ÔÚ¶¥²ãÈÝÆ÷ÖУ¬µ«ÔÚcontent pane Ö®Íâ
  ¡£
¶¥²ãÈÝÆ÷

°üº¬¼Ì³ÐͼÈçÏ£º
¶¥²ãÈÝÆ÷
1. ÔÚContent Pane ÖÐÌí¼Ó×é¼þ

    ÓÐÁ½ÖÖ·½Ê½£º
?    topLevelContainer.getContentPane().add(yellowLabel,
     BorderLayout.CENTER);

»ò£º
?     JPanel contentPane = new JPanel();
        ¡­ ¡­
      topLevelContainer.setContentPane(contentPane);
¶¥²ãÈÝÆ÷
2. Ìí¼Ó²Ëµ¥Ìõ
?   JMenuBar cyanMenuBar = new JMenuBar();
?   frame.setJMenuBar(cyanMenuBar);
JFrame
ÊÇÒ»¸ö´øÓбêÌâºÍ¿É±ä´óС±ß¿òµÄ´°¿Ú¡£

ÈκÎÒ»¸öͼÐνçÃæÓ¦ÓóÌÐò¶¼±ØÐëÖÁÉÙÓÐÒ»¸ö
  JFrame.

²Î¼û³ÌÐò£º
FrameDemo.java
TopLevelDemo.java
panel

?   ΪÆäËü×é¼þÌṩһ¸öÈÝÆ÷¡£±ãÓÚ×éÖ¯ºÍÏÔ
    ʾ×é¼þ¡£
?   »¹¿ÉÒÔ°üº¬×Ópanel

²Î¼û³ÌÐò£ºMyPanel.java
´´½¨²Ëµ¥£º
²½Ö裺
1.´´½¨Ò»¸ö MenuBar ¶ÔÏó, ½«Æä¼ÓÈëÒ»¸ö²Ëµ¥ÈÝÆ÷£¬
   ÀýÈ磺 Frame .

2.´´½¨ Ò»¸ö»ò¶à¸öMenu ¶ÔÏó,½«Æä¼ÓÈëmenu bar
   ¶ÔÏó.

3.´´½¨ Ò»¸ö»ò¶à¸öMenuItem objects,¶ÔÏó,½«Æä¼Ó
   ÈëÏàÓ¦µÄmenu object.

²Î¼û³ÌÐò£ºMenuTest.java
JComponent Class
   ³ýÁ˶¥²ãÈÝÆ÷£¬SwingÖÐËùÓÐ×é¼þ£¨ÖмäÈÝÆ÷ºÍ
   Ô­×Ó×é¼þ£©¶¼´ÓJComponentÀàÖм̳С£

ÀýÈ磺 JPanel, JScrollPane, JButton, JTable£¨ ÒÔ
   J¿ªÍ·µÄ×é¼þÀࣩ¡£
Component

Container

JComponent
JComponent Class
JComponent Class API£º
 Ëü´ÓComponent and Container ¼Ì³ÐÁËÐí¶à·½·¨£¬Í¬Ê±
  Ò²ÌṩÁËһЩеķ½·¨¡£ËüΪËüµÄ¼Ì³ÐÕßÌṩÁËÈçÏÂ
  ³£Óù¦ÄÜ£º
?   Customizing Component Appearance
?   Setting Component State
?   Handling Events
?   Painting Components
?   Dealing with the Containment Hierarchy
?   Laying Out Components
?   Getting Size and Position Information
?   Specifying Absolute Size and Position
? BorderWindow.java

? FlowWindow.java

? GridWindow.java

? MultiListener.java
ColorÀà
? ColorÀཫÑÕÉ«°´ÕÕsRGB±ê×¼¸ñʽ½øÐзâ×°£¬¸Ã¸ñ
  ʽÖк졢ÂÌ¡¢À¶ÈýÔ­É«µÄÈ¡Öµ·¶Î§¶¼ÊÇ0~255¡£
? ColorÀඨÒåÁ˶à¸ö¹¹Ôì·½·¨£¬³£ÓõÄÓУº
    public Color(int r, int g, int b)
    public Color(int r, int g, int b, int a) // a -- ͸Ã÷¶È²ÎÊý
    Color c = new Color(200, 170, 90);
    Color d = new Color(200, 170, 90, 120);
? ÔÚGUIÉè¼ÆÖÐʹÓÃColorÀà
    Button b = new Button(¡°Test¡±);
    Color c = new Color(200, 170, 90);
    b.setBackground(c)

More Related Content

[ʥ˼԰][Java SE]Swing