Presentation is loading. Please wait.

Presentation is loading. Please wait.

第十二章 JFC/Swing 概述  Swing组件  Swing应用程序结构  JComponent类.

Similar presentations


Presentation on theme: "第十二章 JFC/Swing 概述  Swing组件  Swing应用程序结构  JComponent类."— Presentation transcript:

1 第十二章 JFC/Swing 概述  Swing组件  Swing应用程序结构  JComponent类

2 JFC/Swing  Java Foundation Classes(JFC) 包含如下特征:  Swing 组件
 支持可插入的look&feel  Accessibility Java 2D  Drag and Drop

3 Swing介绍 Swing中的所有组件全部用Java语言实现。 可插入的look and feel
与AWT相比提供更丰富的GUI组件,引入新的特征, 并提供更丰富的功能。

4 Swing组件类层次 Java.awt.Container Javax.swing.Jcomponent JTextArea
JTextField JTextComponent AbstractButton Jpanel JComboBox JLabel Jtree JPasswordField JCheckBox JRadioButton JToggleButton JButton JMeneItem

5 Swing组件——顶层容器 Dialog Frame Applet

6 Swing组件——通用容器 Panel   Scroll pane Split pane Tabbed pane Tool bar

7 Swing组件——特殊用途的容器 Internal frame Layered pane Root pane

8 Swing组件——基本控制 List Buttons Combo box Slider Text fields Menu

9 Swing组件——信息显示 Progress bar Tool tip Label

10 Swing组件——可编辑格式化信息显示 Color chooser File chooser Table Text Tree

11 Swing程序举例 import javax.swing.*; public class HelloWorldSwing {
public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.pack(); frame.setVisible(true);

12 Swing应用程序基本结构 引入Swing 包: import javax.swing.*; 以及
import java.awt.*; import java.awt.event.*; 选择Look&Feel 设置窗口容器 设置Swing组件

13 Swing中的Look&Feel Java look and feel Windows look and feel
CDE/Motif look and feel

14 Swing中的Look&Feel 使用UIManager.setLookAndFeel( ) 方法进行设置。 在该方法中可以使用的参数:
UIManager.getCrossPlatformLookAndFeelClassName() -- Java Look & Feel. UIManager.getSystemLookAndFeelClassName() –当前平台的look&feel "javax.swing.plaf.metal.MetalLookAndFeel" --Java Look & Feel. "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" -- Windows Look & Feel. "com.sun.java.swing.plaf.motif.MotifLookAndFeel" -- CDE/Motif Look & Feel. "javax.swing.plaf.mac.MacLookAndFeel" -- Mac OS Look & Feel

15 设置窗口容器  JFrame类似于Frame ,Jframe是AWT中Frame的子类。
不能直接向JFrame中加入组件,而要向 Jframe的content pane或向另外提供的新的content pane中增加组件。 content pane中包含除了menu bar之外的所有组件。 getContentPan()– 获取Jframe的content pane; setContentPan()– 设置content pane

16 设置Swing组件 frame = new JFrame(...); button = new JButton(...);
label = new JLabel(...); pane = new JPanel(); pane.add(button); pane.add(label); frame.getContentPane().add(pane,BorderLayout.CENTER);

17 设置Swing组件边界 pane.setBorder(BorderFactory.createEmptyBorder( 30, //top
30, //left 10, //bottom 30) //right );

18 Swing中的事件处理 引起事件的动作 Listener类型 点击button,在文本域中敲回车,或选择一个菜单项
ActionListener 关闭frame(主窗口) WindowListener 在组件上点击鼠标 MouseListener 鼠标移过组件 MouseMotionListener 组件显示 ComponentListener 组件得到键盘聚焦 FocusListener Table 或list的选项改变 ListSelectionListener

19 建立Swing GUI  Swing包中定义了两类组件: 顶层容器(JFrame,Japplet,Jwindow,Jdialog)
轻型组件(‘J’开头的所有组件,如Jbutton,Jpanel,Jmenu等) 一般如果使用Swing组件,则程序中只使用Swing组件 和Swing容器。 Swing组件可以加入到一个与顶层容器相关的content pane 中,而不能直接加入到顶层容器中。

20 JComponent类 setBorder() – 设置组件边界;
setToolTipText() – 当鼠标移动到组件时,显示简短帮助信息 registerKeyboardAction()—使用键盘进行界面操作。 UIManager.setLookAndFeel() -- 应用程序范围的可插入Look&feel


Download ppt "第十二章 JFC/Swing 概述  Swing组件  Swing应用程序结构  JComponent类."

Similar presentations


Ads by Google