? ? é m??? !9 ?c m??? !9 ? ê ?c 7ym?¨?? ? ?c BQQMFU?ú)m?? ? ?c m??? !9 ? ê ) 3+BWBm??? !9 ? ) P¨ Oa ?e?6*Fq ) ?3? 5 ? ¥T¨ ) ?$PMPSa'POU ? ) P¨(SBQIJDT ??m ) ?+BWB Yq m??? !9 ?¥aQ (Swing) AWTEvent Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the javax.swing package 1 LayoutManager * JComponent JButton JMenuItem JCheckBoxMenuItem AbstractButton JComponent JMenu .JRadioButtonMenuItem .JToggleButton JCheckBox JRadioButton .JComboBox .JInternalFrame .JLayeredPane .JList .JMenuBar .JOptionPane .JPopupMenu .JProgressBar .JPane .JFileChooser .JScrollBar .JScrollPane .JSeparator .JSplitPane .JSlider .JTabbedPane .JTable .JTableHeader .JTextField .JTextComponent .JEditorPane .JTextArea .JToolBar .JToolTip .JTree .JRootPane .JPanel .JPasswordField .JColorChooser .JLabel AWT (Optional) AWTEvent Font FontMetrics Component Graphics Object Color Canvas Button TextComponent Label List CheckBoxGroup CheckBox Choice Container Panel Applet Frame Dialog FileDialog Window TextField TextArea MenuComponent MenuItem MenuBar Menu Scrollbar LayoutManager O )17yB?¨?? ?317yB? O applet ?ib¨?? ?Fqb Frame Pull-down Menus User Interface Components (UI) Panel Panel Panel UI Panel UI Panel UI Applet Panel User Interface Components Panel User Interface Components Panel User Interface Components Panel User Interface Components panel Pull-down Menus Creating Frames import javax.swing.*; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true); // frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } } Run O ?  a f ?/ O à ¥P ?A Ub1 ·?ê?A U O31 P¨ JFrame ? ?¥ setLocation(x, y)ZE . ?ZE ü O¥P ?b?ê? (x, y)) . 1ü Ob? à   ?¥ê?31? ? à ? O¥ú? z[L ?? OP  ?¥USb O ? screenH screenWidth frameHeight screenWidth (x, y) Frame Screen à  z à ú RunCenterFrame Adding Components into a Frame // Add a button into the frame frame.getContentPane().add( new JButton("OK")); Run MyFrameWithComponents Layout Managers )Java’s layout managers provide a level of abstraction to automatically map your user interface on all windowing systems. )The UI components are placed in containers. Each container has a layout manager to arrange the UI components within the container. Kinds of Layout Managers )FlowLayout )GridLayout )BorderLayout )CardLayout )GridBagLayout Example 8.1 Testing the FlowLayout Manager The components are arranged in the container from left to right in the order in which they were added. When one row becomes filled, a new row is started. RunShowFlowLayout FlowLayout Constructors ) public FlowLayout(int align, int hGap, int vGap) Constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances in pixel between components. ) public FlowLayout(int alignment) Constructs a new FlowLayout with a specified alignment and a default gap of five pixels for both horizontal and vertical. ) public FlowLayout() Constructs a new FlowLayout with a default center alignment and a default gap of five pixels for both horizontal and vertical. Example 8.2 Testing the GridLayout Manager The GridLayout manager arranges components in a grid (matrix) formation with the number of rows and columns defined by the constructor. The components are placed in the grid from left to right starting with the first row, then the second, and so on. RunShowGridLayout GridLayout Constructors ) public GridLayout(int rows, int columns) Constructs a new GridLayout with the specified number of rows and columns. ) public GridLayout(int rows, int columns, int hGap, int vGap) Constructs a new GridLayout with the specified number of rows and columns, along with specified horizontal and vertical gaps between components. Example 8.3 Testing the BorderLayout Manager add(Component, constraint), where constraint is BorderLayout.East, BorderLayout.South, BorderLayout.West", BorderLayout.North", or BorderLayout.Center. The BorderLayout manager divides the window into five areas: East, South, West, North, and Center. Components are added to a BorderLayout by using ShowBorderLayout Run Using Panels as Containers )Panels act as smaller containers for grouping user interface components. )It is recommended that you place the user interface components in panels and place the panels in a frame. You can also place panels in a panel. Example 8.4 Testing Panel ' è P¨ ?eF?Fqb ?7y B?± o V¨?? ?b RunTestPanels Drawing on Panels JPanel ^? Vn¥ V[¨TF?Fq¥l ? [r?1 p¥? rT . JPanel¥ 6B?×1 ¨o ^??3?1?m?b 1 ?e ?m ,317yB?? JPanel ?Z ¥? ?i O-? paintComponentZE . The Color Class Color c = new Color(r, g, b); r, g, and b specify a color by its red, green, and blue components. Example: Color c = new Color(128, 100, 100); Setting Colors You can use the following methods to set the component’s background and foreground colors: setBackground(Color c) setForeground(Color c) Example: setBackground(Color.yellow); setForeground(Color.red); The Font Class Font myFont = Font(name, style, size); Example: Font myFont = new Font("SansSerif ", Font.BOLD, 16); Font myFont = new Font("Serif", Font.BOLD+Font.ITALIC, 12); Setting Fonts public void paint(Graphics g) { Font myFont = new Font("Times", Font.BOLD, 16); g.setFont(myFont); g.drawString("Welcome to Java", 20, 40); //set a new font g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 12)); g.drawString("Welcome to Java", 20, 70); } The FontMetrics Class By Leading Ascent Descent seline Height Get FontMetrics ) g.getFontMetrics(Font f); or ) g.getFontMetrics(); ) public int getAscent() ) public int getDescent() ) public int getLeading() ) public int getHeight() ) public int stringWidth(String str) Example 8.5 Using FontMetrics ) Objective: Display “Welcome to Java” in SansSerif 20-point bold, centered in the frame. (0,0) (120, 100) x y (120, 0) (0, 100) TestFontMetrics Run Drawing Geometric Figures ) Drawing Lines ) Drawing Rectangles ) Drawing Ovals ) Drawing Arcs ) Drawing Polygons Drawing Lines drawLine(x1, y1, x2, y2); (x 1 , y 1 ) (x 2 , y 2 ) Drawing Rectangles )drawRect(x, y, w, h); )fillRect(x, y, w, h); (x, y) w h Drawing Rounded Rectangles )drawRoundRect(x, y, w, h, aw, ah); )fillRoundRect(x, y, w, h, aw, ah); (x, y) w h ah aw Drawing Ovals )drawOval(x, y, w, h); )fillOval(x, y, w, h); (x, y) w h Drawing Arcs ) drawArc(x, y, w, h, angle1, angle2); ) fillArc(x, y, w, h, angle1, angle2); (x, y) w h angle1 angle2 Drawing Polygons int x[] = {40, 70, 60, 45, 20}; int y[] = {20, 40, 80, 45, 60}; g.drawPolygon(x, y, x.length); g.fillPolygon(x, y, x.length); (x[0], y[0]) (x[1], y[1]) (x[2], y[2]) (x[3], y[3]) (x[4], y[4]) Example 8.6 Drawing a Clock ) Objective: Use drawing and trigonometric methods to draw a clock showing the specified hour, minute, and second in a frame. 9 3 12 6 (xCenter,yCenter) (xEnd,yEnd) DrawClock handLength , DisplayClock Run Yq z??? !9 )?C1?V¥?? ^ ?_`¥ ? ^[V?¥ ¨?3?b V[ P¨ ? ??ì?  ? e?3?¥ @?? ^ ?? %? 3?Q?b )Javam??? !9 ^ Yq z?¥b ) Yq z??? !9? , B? Yq H ü 7 S3?M?¥} ? Yq9? ^?  ? f9? ^M? ?Sb Yq ) Yq ( event ) V[?l1??? 3  t Y f¥ ?|b )??¨??1 ?M? ?Sa? ?S? f? ?/o ?o? V[?? Yqb?T"d ? Hò9 V[?? Yqb )B? Yq ^ Yq ?¥ L è Yq ?¥? ? ^ java.util.EventObjectb Yq ?¥aQ1" AWTEventEventObject AdjustmentEvent ComponentEvent TextEvent ItemEvent ActionEvent InputEvent WindowEvent MouseEvent KeyEvent ContainerEvent FocusEvent PaintEvent ListSelectionEvent Selected User Actions ¨??1 ÷` Yq ?? ? ? f JButton ActionEvent ?Mó' JTextComponent TextEvent ó'×?/íúo JTextField ActionEvent ê?B??[ JComboBox ItemEvent,ActionEvent ê?[ JList javax.swing.event.ListSelectionEvent ? ˉê  JCheckBox ItemEvent,ActionEvent ? ?ê? f JRadioButton ItemEvent,ActionEvent ê???[ JMenuItem ActionEvent M?O?H JScrollBar AdjustmentEvent 3 gu 7a1>? Window WindowEvent  ? ?@F ÷"Fq Container ContainerEvent FqM?a?Mvl? Component ComponentEvent Fq | > ??? Component FocusEvent db?/o Component KeyEvent M? ?S Component MouseEvent Yq?? ? (?B ? Yq 3?B ? Yq Y?SP  ¨??1 Yq` SP ` ÷` Yq) ?  ?¨B?SP ` Example 8.7 ) ?e??1 Yq )' è??3 g?A U ?? f “Ok”? “Canel”  e? ? A U?·?  'B?? fb Run TestActionEvent ? f` ^ Yq ActionEvent¥÷`b TestActionEvent ? ^ JFrame¥0 ?9 ^?1 YqS P byN TestActionEvent31 LC Actionlistener ¤ gb?  jbtOk.addActionListener(this); jbtCancel.addActionListener(this); ?¨ this(TestActionEvent) P SP? f ¥ ActionEvent. Example 8.8 ) ?3 g Yq ) Objective: Window ?¥ ??0 ?? ?? 3 ?/3 g Yqu 73 ga?1>3 ga1>3 ga 3 ga?e3 g? .?? 3?B? OSP 3 g YqiA U?· ü? -? 3¥ Yqb Run TestWindowEvent JFrame ^ Window ?¥B?0 ? ?[? 3 WindowEvent Yq LC WindowListener¤ gb ˉ5 ) í ? javam? ?¥aQ2b ) a ü AWT UI? SwingFq¥ uYb )1 I 131 P¨? 5 ? $ ) ??7y O$ !? Ovla_ O@FFq$ )? ?°La ?a??a???H?¥?Eb )3 d ???¨B?SP ` ?? LCSP ¤ g$ I?  )I??¨?mZE ?e?A UeEV b ) _ è 8.6?B? Hòl Hs¤÷%b