Java(六)-组件&事件处理(概括)

    xiaoxiao2021-03-25  84

    概述

    java.awt包提供设计GUI的组件类(包含Container类,Component类) javax.swing包提供更强大的组件类(包含JComponent类) Component类的子类/间接子类创建的对象是组件 Container类的子类/间接子类创建的对象是容器(本身也是组件) add()方法将组件添加到容器 remove(Component c)方法移除c组件 removeAll()方法移除容器中的全部组件 validate()方法保证组件的显示

    窗口

    实例类窗口JFrame菜单条JMenubar菜单JMenu菜单项JMenuItem图标ImageIcon

    注: 不允许将一个窗口添加到另一个容器中 窗口只能添加一个菜单条 菜单本身也是一个菜单项

    对话框

    有模式(激活时,只响应对话框内部事件) 无模式(激活时,能再激活其它窗口,也不阻塞其他线程)

    消息对话框(有模式) 用JOptionPane类的showMessageDialog方法创建

    输入对话框(有模式) 用JOptionPane类的showInputDialog方法创建

    确认对话框(有模式) 用JOptionPane类的showConfirmDialog方法创建

    颜色对话框(有模式) 用JColorChooser类的showDialog方法创建

    自定义对话框 用JDialog的子类来建立,默认布局是BorderLayout,可添加组件,不允许将对话框添加到另一个容器


    组件

    1.常用组件

    组件类组件类文本框JTextField标签JLabel文本区JTextArea选择框JCheckBox按钮JButton单选按钮JRadioButton下拉列表JComboBox密码框JPasswordField

    2.树组件

    构造: javax.swing.tree包的DefaultMutableTreeNode类可以构造结点(任何实现MutableTreeNode接口的类创建的对象都可以成为结点);JTree类构造根节点是root的树组件事件: 注册监视器:addTreeSelectionListener(TreeSelectionListener listen) TreeSelectionListener接口:有valueChanged(TreeSelectionEvent e)方法

    3.表格组件

    【最复杂,以后再补】

    4.打印组件

    调用getToolkit()返回系统提供的Toolkit对象的引用(当有组件时系统自动提供Toolkit子类的对象)让Toolkit对象调用getPrintJob()返回一个PrintJob对象让PrintJob对象调用getGraphics()获得Graphics对象让Graphics对象调用paintAll(g)/paint(g)打印组件(总是从左上角开始打印)可让Graphics对象调用translate(int x,int y)改变打印位置

    常用容器

    容器类用法备注面板JPanel默认布局FlowLayout滚动窗格JScrollPanenew JScrollPane(Component c)通过滚动条观看组件拆分窗格JSplitPaneJSplitPane(int a,Component b,Component c)可分水平拆分和垂直拆分分层窗格JLayeredPaneadd(Jcomponent com,int layer)重叠,分为5层

    常用布局

    布局备注FlowLayout是JPanel型容器的默认布局,按顺序从左向右,一行满后换行,间隙5像素,默认居中排列BorderLayout是Window型容器的默认布局,空间分为东西南北中,每个区域只能放置一个组件,可以嵌套CardLayout组件层叠放入,依次向下GridLayout把容器划分为m×n的网格区域BoxLayout在javax.swing.border包中,组件排列在一行/列,只有一行/列null可以准确定位容器的位置和大小

    处理事件

    监视器可用窗口,也可用匿名类

    1. ActionEvent事件

    事件源:文本框、按钮、菜单项、密码框、单选按钮 注册监视器:addActionListener(ActionListener listen)ActionListener接口:在java.awt.event包中,只有一个actionPerformed(ActionEvent e)方法

    2. ItemEvent事件

    事件源:选择框、下拉列表注册监视器:addItemListener(ItemListener listen)ItemListener接口:在java.awt.event包中,只有一个itemStateChanged(ItemEvent e)方法

    3. DocumentEvent事件

    事件源:文本区所维护的文档内容产生变化注册监视器:addDocumentListener(DocumentListener listen)DocumentListener接口:在java.swing.event包中,有三个方法: public void changedUpdate(DocumentEvent e)public void removeUpdate(DocumentEvent e)public void insertUpdate(DocumentEvent e)

    4. MouseEvent事件

    事件源:任何组件注册监视器:addMouseListener(MouseListenr listen)MouseListenr接口: 按下 mousePressed(MouseEvent)释放 mouseRelesed(MouseEvent)单击 mouseClick(MouseEvent)进入 mouseEntered(MouseEvent)退出 mouseExited(MouseEvent)MouseMotionListenr接口: 拖动 mouseDragged(MouseEvent)移动 mouseMoved(MouseEvent)

    5.焦点事件

    事件源:任何组件注册监视器:addFocusListener(FocusListener listen)FocusListener接口: focusGained(FocusEvent e)focusLost(FocusEvent e)

    6.键盘事件

    事件源:键盘操作注册监视器:addKeyListener(KeyListener listen)KeyListener接口: keyPressed(KeyEvent e)keyType(KeyEvent e)keyReleased(KeyEvent e)

    7.窗口事件

    事件源:所有Window子类创建的对象注册监视器:addWindowListener(WindowListener listen)WindowListenr接口: 激活 windowActivated非激活 windowDeactivated正关闭 windowClosing关闭后 windowClosed图标化 windowIconified撤销图标化 windowDeiconified打开时 windowOpenedWindowAdapter适配器:代替接口来处理,不用给出所有方法实现
    转载请注明原文地址: https://ju.6miu.com/read-10899.html

    最新回复(0)