第八章图形界面设计

一、AWT与Swing
  1. 两者区别:
    1. AWT是最早开发出来的,比较粗略,对硬件依赖性强;
    2. Swing组件相对美观且对硬件依赖性弱;
    3. import javax.swing.*
  2. 两者联系:
    1. Swing 继承了 AWT
二、容器
  1. 顶层容器
    1. JFrame();
    2. JFeame(String title);
    3. 设置位置和宽高:setBounds(int x,int y,int width,int height)
    4. 设置尺寸:setSize(int width,int height)
    5. 设置背景颜色:setBackground(Color c)
    6. 设置可见:setVisible(boolean flag)
    7. 设置紧凑排列:pack()
    8. 设置标题:setTitle(String title)
    9. 获取内容窗格:getContentPane()
    10. 设置布局管理器:setLayout(LayoutManager m)
  2. 内容窗格
    //通过顶层容器设置内容窗格
    Container cp= frame.getContentPane();
    Button b = new Button("11")
    cp.add(b,BprderLayout.CENTER)
    //创建一个新的内容窗格
    Jpanel jp= new JPanel();//创建面板
    jp.setLayout(newBorderLayout)
    jp.add(b,BprderLayout.CENTER)
    frame.setContentPane(jp);//设置内容窗格
    
  3. 面板
    1. 普通面板:Jpanel()
    2. 滚动面板:JScrollPane();JScrollPane(Component view);
    3. 设置水平滚动策略: void setHorizontalScrollBarPolicy(int policy)Needed:需要时,Never:从不,Always:总是;
    4. 设置垂直滚动策略:void setVerticalScrollBarPolicy(int policy)
    5. 垂直滚动条:Scrollbar();指定方向<H:水平,V:垂直>:Scrollbar(int o);指定方向、初始值、可视量、最小值和最大值 ScrollBar(int o,int value,int visible,int mini,int max)
三、标签和按钮
  1. 标签:构造方法可设置文本、对齐方式和图标
  2. 按钮:构造方法可设置文本和图标
    1. 设置启用:void setEnabled(boolean b)
    2. 设置快捷键:void setMnemonic(int c)
    3. 注册点击事件:addActionListener(ActionListener a)
    4. 获取事件源:e.getSource()
    5. 获取事件源命令字符串:e.getActionCommand()
  3. 切换按钮JToggleButton
  4. 复选按钮JCheckBox、单选按钮JRadioButton
    1. 是否被选中:isSelected
    2. 注册点击事件:addActionListener(ActionListener a)
    3. 注册选中事件:addItemListener(ItemListener i)
    4. 获取事件源:e.getSource()e.getItem()
四、布局管理器
  1. 流式布局管理器FlowLayout(组件大小不随容器大小改变)
    1. new FlowLayout(FlowLayout.RIGHT,20,40)
    2. new FlowLayout(FlowLayout.LEFT)
    3. new FlowLayout()
  2. 边界布局管理器BorderLayoutJFrame默认布局)
    1. 分为五个部分North、West、Center、East、South
    2. 组件大小随容器大小改变
  3. 网格布局管理器GridLayout
    1. 指定横纵:new GridLayout(3,3)
    2. 组件大小随容器大小改变
  4. 卡片布局管理器CardLayout
    1. 第一张:first()
    2. 下一张:next()
    3. 前一张:previous()
    4. 最后一张:last()
    5. 指定:show(Container parent,String name)
  5. 盒式布局管理器BoxLayout
  6. 空布局,通过 setBounds(int x,int y,int width,int height)控制布局大小
五、事件处理(重要)
  1. 事件处理模型(授权处理模式)
  2. 事件的种类
事件类型组件接口名称适配器名称方法及说明
ActionEventJButton 、JCheckBox、JComboBox、JMenuItem、JRadioButtonActionListeneractionPerformed(ActionEvent e)点击时或回车时
AdjustmentEventJScrollBarAdjustmentListeneradjustmentValueChanged(AdjustmentEvent e)改变滑块位置
ComponentEventJComponent及其子类ComponentListenerComponentAdaptercomponentMoved(ComponentEvent e)组件移动时 componentHidden(ComponentEvent e)组件隐藏时 componentResized(ComponentEvent e)组件缩放时componentShown(ComponentEvent e)组件移动时
ContainEventJContainer及其子类ComtainerListenerComtainerAdaptercomponentAdded(ContainEvent e)添加组件时 componentRemoved(ContainEvent e)移除组件时
ItemEventJCheckBox、JCheckboxMenuItem、JComboBoxItemListeneritemStateChanged(ItemEvent e)
KeyEventJComponent及其子类KeyListenerKeyAdapterkeyPressed(KeyEvent e)键按下时 keyReleased(KeyEvent e)键释放时 keyTyped(KeyEvent e)击键时
MouseButtonEventJComponent及其子类MouseListenerMouseAdaptermousePressed(MouseEvent e)鼠标键按下时 mouseReleased(MouseEvent e)鼠标键释放时 mouseEntered(MouseEvent e)鼠标键进入时 mouseExited(MouseEvent e)鼠标键离开时 mouseClicked(MouseEvent e)鼠标键点击时
MouseMotionEventJComponent及其子类MouseMotionListenerMouseMotionAdaptermouseDragged(MouseEvent e) 鼠标拖放时 mouseMoved(MouseEvent e) 鼠标移动时
TextEventJTextField、JTextAreaTextListenertextValueChanged(TextEvent e)内容修改时
WindowEventJFrame、JWindow、JDialogWindowListenerWindowAdapterwindowClosing(WindowEvent e)窗口关闭时 windowOpened(WindowEvent e)窗口打开后 windowIconified(WindowEvent e)窗口最小化时 windowDeiconified(WindowEvent e)窗口还原时 windowClosed(WindowEvent e)窗口关闭后 windowActivated(WindowEvent e)窗口激活时 windowDeactivated(WindowEvent e) 窗口失焦时
六、绘图基础(会考)
  1. 颜色:java.awt.Color
  2. 字体:Font f = new Font("字体","风格","大小")
  3. Graphics的基本功能(相当于笔)
    1. 重写JcomponentpaintComponent()获取Graphics
  4. Graphics2D绘图
    1. java.awt.geom图形类
    2. Graphics2D g2d = (Graphics2D)g;g是从容器上获取的画笔
    3. g2d.draw(对象)绘画
七、程序实例
package com.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestSwingAndAwt extends JFrame {
    JButton button;//按钮
    JTextField textField;//文本框

    TestSwingAndAwt(){
        //创建组件
        button = new JButton("输出");
        button.addActionListener(new ButtonActionListener());//注册事件
        textField = new JTextField(20);
        FlowLayout flowLayout = new FlowLayout();
        Container current = getContentPane();//获取容器
        current.setLayout(flowLayout);//设置布局
        current.add(textField);
        current.add(button);
    }

    class ButtonActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            //事件处理
            String content = textField.getText();
            System.out.println(content);
        }
    }

    public static void main(String[] args) {
        TestSwingAndAwt test = new TestSwingAndAwt();
        test.setSize(400,200);//设置宽高
        test.setVisible(true);//设置可见
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭
    }

}

注意:单选题、简答题、程序填空题、写程序题(20-35分左右)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值